diff --git a/CMakeLists.txt b/CMakeLists.txt index b636d6e..ad7a161 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,7 +95,7 @@ set( sources setup_library( fc SOURCES ${sources} ) setup_executable( json_rpc_test SOURCES tests/json_rpc_test.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} ${Boost_DATE_TIME_LIBRARY}) -setup_executable( ssh_test SOURCES tests/ssh.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} ssh2 ${OPENSSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY} z ${Boost_DATE_TIME_LIBRARY}) +setup_executable( ssh_test SOURCES tests/ssh.cpp LIBRARIES fc ${pthread_library} ${rt_library} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${rt_library} ssh2 ${OPENSSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY} ${ZLIB_LIBRARY} ${ALL_OPENSSL_LIBRARIES} ${Boost_DATE_TIME_LIBRARY}) #add_executable( test_vec tests/vector_test.cpp ) #target_link_libraries( test_vec fc ${Boost_SYSTEM_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_THREAD_LIBRARY} ${Boost_CONTEXT_LIBRARY} ) diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index 67b9604..041c0bb 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -39,7 +39,7 @@ namespace fc { fc::string string()const; fc::string generic_string()const; private: - fwd _p; + fwd _p; }; class directory_iterator { diff --git a/include/fc/future.hpp b/include/fc/future.hpp index d3d2ee5..438c5b2 100644 --- a/include/fc/future.hpp +++ b/include/fc/future.hpp @@ -79,7 +79,7 @@ namespace fc { mutable spin_yield_lock _spin_yield; thread* _blocked_thread; time_point _timeout; - fc::exception_ptr _except; + fc::exception_ptr _exceptp; bool _canceled; const char* _desc; detail::completion_handler* _compl; diff --git a/include/fc/fwd.hpp b/include/fc/fwd.hpp index 43e15ec..ccf0828 100644 --- a/include/fc/fwd.hpp +++ b/include/fc/fwd.hpp @@ -1,5 +1,4 @@ -#ifndef FC_FWD_HPP_ -#define FC_FWD_HPP_ +#pragma once #include namespace fc { @@ -44,4 +43,3 @@ class fwd { } // namespace fc -#endif diff --git a/include/fc/interprocess/file_mapping.hpp b/include/fc/interprocess/file_mapping.hpp index 412bf09..8f01ab7 100644 --- a/include/fc/interprocess/file_mapping.hpp +++ b/include/fc/interprocess/file_mapping.hpp @@ -22,7 +22,7 @@ namespace fc { ~file_mapping(); private: friend class mapped_region; - fc::fwd my; + fc::fwd my; }; class mapped_region { public: diff --git a/include/fc/iostream.hpp b/include/fc/iostream.hpp index 0e3829a..ca626bf 100644 --- a/include/fc/iostream.hpp +++ b/include/fc/iostream.hpp @@ -59,14 +59,14 @@ namespace fc { template ostream& operator<<( ostream& o, const T& v ) { auto str = fc::lexical_cast(v); - o.write( str.c_str(), str.size() ); + o.write( str.c_str(), static_cast(str.size()) ); return o; } ostream& operator<<( ostream& o, const char* v ); template ostream& operator<<( ostream& o, const fc::string& str ) { - o.write( str.c_str(), str.size() ); + o.write( str.c_str(), static_cast(str.size()) ); return o; } template diff --git a/include/fc/json_rpc_client.hpp b/include/fc/json_rpc_client.hpp index 59b1787..fd286fa 100644 --- a/include/fc/json_rpc_client.hpp +++ b/include/fc/json_rpc_client.hpp @@ -12,7 +12,7 @@ namespace fc { namespace json { namespace detail { struct rpc_member { - #if BOOST_NO_VARIADIC_TEMPLATES + #ifdef BOOST_NO_VARIADIC_TEMPLATES #define RPC_MEMBER_FUNCTOR(z,n,IS_CONST) \ template \ static std::function( BOOST_PP_ENUM_PARAMS(n,A) ) > \ diff --git a/include/fc/lexical_cast.hpp b/include/fc/lexical_cast.hpp index b72d8c7..85cd860 100644 --- a/include/fc/lexical_cast.hpp +++ b/include/fc/lexical_cast.hpp @@ -11,11 +11,11 @@ namespace fc { inline double to_double( double d ) { return d; } int64_t to_int64( const fc::string& s ); - inline int64_t to_int64( double d ) { return d; } + inline int64_t to_int64( double d ) { return static_cast(d); } inline int64_t to_int64( int64_t d ) { return d; } uint64_t to_uint64( const fc::string& s ); - inline uint64_t to_uint64( double d ) { return d; } + inline uint64_t to_uint64( double d ) { return static_cast(d); } inline uint64_t to_uint64( uint64_t d ) { return d; } fc::string to_string( double d ); @@ -47,32 +47,33 @@ namespace fc { }; template - struct lexical_cast { static uint64_t cast( const R& v ) { return to_int64( v ); } }; + struct lexical_cast { static int64_t cast( const R& v ) { return static_cast(to_int64( v )); } }; template - struct lexical_cast { static uint32_t cast( const R& v ) { return to_int64( v ); } }; + struct lexical_cast { static int32_t cast( const R& v ) { return static_cast(to_int64( v )); } }; template - struct lexical_cast { static uint16_t cast( const R& v ) { return to_int64( v ); } }; + struct lexical_cast { static int16_t cast( const R& v ) { return static_cast(to_int64( v )); } }; template - struct lexical_cast { static uint8_t cast( const R& v ) { return to_int64( v ); } }; + struct lexical_cast { static int8_t cast( const R& v ) { return static_cast(to_int64( v )); } }; template - struct lexical_cast { static uint32_t cast( const R& v ) { return to_uint64( v ); } }; + struct lexical_cast { static uint32_t cast( const R& v ) { return static_cast(to_uint64( v )); } }; template - struct lexical_cast { static uint16_t cast( const R& v ) { return to_uint64( v ); } }; + struct lexical_cast { static uint16_t cast( const R& v ) { return static_cast(to_uint64( v )); } }; template - struct lexical_cast { static uint8_t cast( const R& v ) { return to_uint64( v ); } }; + struct lexical_cast { static uint8_t cast( const R& v ) { return static_cast(to_uint64( v )); } }; template - struct lexical_cast { static bool cast( const R& v ) { return v; } }; + struct lexical_cast { static bool cast( const R& v ) { return v != 0; } }; template<> - struct lexical_cast { static bool cast( const fc::string& v ) { return v[0]; } };// TODO: check string len + struct lexical_cast { static char cast( const fc::string& v ) + { return v[0]; } };// TODO: check string len template<> struct lexical_cast { static bool cast( const fc::string& v ) { return v == "true"; } }; @@ -81,7 +82,7 @@ namespace fc { struct lexical_cast { static fc::string cast( const bool& v ) { return v ? "true" : "false";} }; template - struct lexical_cast { static float cast( const R& v ) { return to_double( v ); } }; + struct lexical_cast { static float cast( const R& v ) { return static_cast(to_double( v )); } }; } diff --git a/include/fc/ptr.hpp b/include/fc/ptr.hpp index 80ec4ab..d8089b0 100644 --- a/include/fc/ptr.hpp +++ b/include/fc/ptr.hpp @@ -2,6 +2,7 @@ //#include #include #include +#include #include #include #include @@ -12,7 +13,7 @@ namespace fc { namespace detail { struct identity_member { - #if BOOST_NO_VARIADIC_TEMPLATES + #ifdef BOOST_NO_VARIADIC_TEMPLATES #define RPC_MEMBER_FUNCTOR(z,n,IS_CONST) \ template \ static std::function \ diff --git a/include/fc/reflect.hpp b/include/fc/reflect.hpp index fcd5bdd..9bede15 100644 --- a/include/fc/reflect.hpp +++ b/include/fc/reflect.hpp @@ -79,7 +79,8 @@ struct reflector{ #endif //#include #define FC_REFLECT_VISIT_MEMBER( r, visitor, elem ) \ - visitor.TEMPLATE operator()elem), type, &type::elem>( BOOST_PP_STRINGIZE(elem) ); + +// visitor.TEMPLATE operator()elem), type, &type::elem>( BOOST_PP_STRINGIZE(elem) ); #define FC_REFLECT_BASE_MEMBER_COUNT( r, OP, elem ) \ diff --git a/include/fc/shared_impl.cpp b/include/fc/shared_impl.cpp index 47e97b8..da73f9a 100644 --- a/include/fc/shared_impl.cpp +++ b/include/fc/shared_impl.cpp @@ -20,7 +20,7 @@ shared_impl::shared_impl( shared_impl&& u ):_impl(fc::move(u._impl)){} template shared_impl& shared_impl::operator=( shared_impl&& u ) { - fc::swap(_impl,u._impl); + fc_swap(_impl,u._impl); return *this; } template @@ -46,7 +46,7 @@ TYPE::TYPE( A1&& a1, A2&& a2 ) \ :my( new typename fc::shared_impl::impl( fc::forward(a1), fc::forward(a2) ) ){}\ template \ TYPE::TYPE( A1&& a1, A2&& a2, A3&& a3 ) \ -:my( new typename fc::shared_impl::impl( fc::forward(a1), fc::forward(a2), fc::forward(a3) ) ){}\ +:my( new fc::shared_impl::impl( fc::forward(a1), fc::forward(a2), fc::forward(a3) ) ){}\ TYPE::TYPE( shared_impl::impl* m ) \ :my(m){}\ TYPE::TYPE( TYPE* c )\ @@ -56,7 +56,7 @@ TYPE::TYPE( TYPE&& c )\ TYPE::TYPE( const TYPE& c )\ :my(c.my){}\ TYPE::TYPE() \ -:my( new typename fc::shared_impl::impl( ) ){}\ +:my( new fc::shared_impl::impl( ) ){}\ TYPE::~TYPE(){}\ bool TYPE::operator !()const { return !my; }\ TYPE& TYPE::operator = ( const TYPE& c ) {\ @@ -64,11 +64,11 @@ TYPE& TYPE::operator = ( const TYPE& c ) {\ return *this;\ }\ TYPE& TYPE::operator = ( TYPE&& c ) {\ - fc::swap( my._impl, c.my._impl );\ + fc_swap( my._impl, c.my._impl );\ return *this;\ }\ TYPE& TYPE::operator = ( TYPE* c ) {\ - fc::swap( my._impl, c->my._impl );\ + fc_swap( my._impl, c->my._impl );\ delete c;\ return *this;\ } \ diff --git a/include/fc/shared_impl.hpp b/include/fc/shared_impl.hpp index 342ff33..bb6e7c8 100644 --- a/include/fc/shared_impl.hpp +++ b/include/fc/shared_impl.hpp @@ -103,7 +103,7 @@ namespace fc { */ template struct shared_impl { - struct impl; + class impl; impl& operator* ()const; impl* operator-> ()const; @@ -119,7 +119,7 @@ namespace fc { ~shared_impl(); - fc::shared_ptr _impl; + fc::shared_ptr::impl> _impl; }; } diff --git a/include/fc/shared_ptr.hpp b/include/fc/shared_ptr.hpp index a2ffe42..859467a 100644 --- a/include/fc/shared_ptr.hpp +++ b/include/fc/shared_ptr.hpp @@ -56,11 +56,11 @@ namespace fc { shared_ptr& operator=(const shared_ptr& p ) { shared_ptr tmp(p); - fc::swap(tmp._ptr,_ptr); + fc_swap(tmp._ptr,_ptr); return *this; } shared_ptr& operator=(shared_ptr&& p ) { - fc::swap(_ptr,p._ptr); + fc_swap(_ptr,p._ptr); return *this; } T& operator* ()const { return *_ptr; } diff --git a/include/fc/string.hpp b/include/fc/string.hpp index 6b4018a..04a613b 100644 --- a/include/fc/string.hpp +++ b/include/fc/string.hpp @@ -1,6 +1,6 @@ -#ifndef _FC_STRING_HPP_ -#define _FC_STRING_HPP_ +#pragma once #include +#include /** @@ -13,10 +13,10 @@ namespace std { struct char_traits; template - struct allocator; + class allocator; template - struct basic_string; + class basic_string; typedef basic_string, allocator > string; } @@ -85,9 +85,8 @@ namespace fc { fc::string substr( int32_t start, int32_t len = 0x7fffffff ); private: - void* my; + fc::fwd my; }; } // namespace FC -#endif // _FC_STRING_HPP_ diff --git a/include/fc/tcp_socket.hpp b/include/fc/tcp_socket.hpp index ca55fbd..408fb82 100644 --- a/include/fc/tcp_socket.hpp +++ b/include/fc/tcp_socket.hpp @@ -32,7 +32,7 @@ namespace fc { private: friend class tcp_server; class impl; - fc::fwd my; + fc::fwd my; }; class tcp_server { diff --git a/include/fc/tuple.hpp b/include/fc/tuple.hpp index 3bc5f67..e527cd6 100644 --- a/include/fc/tuple.hpp +++ b/include/fc/tuple.hpp @@ -60,8 +60,8 @@ namespace fc { template void visit( V&& v)const{}; }; - template - auto call_fused( Functor f, Tuple&& t ) -> decltype( f( ) ) { + template + auto call_fused( Functor f, const tuple<>& t ) -> decltype( f( ) ) { return f(); } @@ -72,13 +72,13 @@ namespace fc { typedef fc::false_type type; }; - #define RREF_PARAMS(z,n,data) BOOST_PP_CAT(AA,n)&& BOOST_PP_CAT(a,n) - #define ILIST_PARAMS(z,n,data) BOOST_PP_CAT(a,n)( fc::forward( BOOST_PP_CAT(a,n) ) ) + #define RREF_PARAMS(z,n,data) BOOST_PP_CAT(AA,n)&& BOOST_PP_CAT(p,n) + #define ILIST_PARAMS(z,n,data) BOOST_PP_CAT(a,n)( fc::forward( BOOST_PP_CAT(p,n) ) ) #define ILIST_PARAMS_COPY(z,n,data) BOOST_PP_CAT(a,n)( t.BOOST_PP_CAT(a,n) ) #define VISIT_PARAMS(z,n,data) v(BOOST_PP_CAT(a,n)); #define LIST_MEMBERS_ON(z,n,data) data.BOOST_PP_CAT(a,n) #define DEDUCE_MEMBERS(z,n,data) typename fc::deduce::type - #define FORWARD_PARAMS(z,n,data) fc::forward(BOOST_PP_CAT(a,n)) + #define FORWARD_PARAMS(z,n,data) fc::forward(BOOST_PP_CAT(p,n)) #define MEM_PARAMS(z,n,data) BOOST_PP_CAT(A,n) BOOST_PP_CAT(a,n); #define TUPLE(z,n,unused) \ template \ @@ -87,6 +87,7 @@ namespace fc { template \ tuple( BOOST_PP_ENUM(n, RREF_PARAMS, unused ) )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS,unused){} \ tuple( const tuple& t )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS_COPY,unused){} \ + tuple( tuple& t )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS_COPY,unused){} \ tuple( tuple&& t )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS_COPY,unused){} \ tuple(){}\ template\ @@ -99,8 +100,13 @@ namespace fc { tuple make_tuple( BOOST_PP_ENUM( n, RREF_PARAMS, unused) ) { \ return tuple( BOOST_PP_ENUM( n, FORWARD_PARAMS,unused ) ); \ } \ - template \ - auto call_fused( Functor f, Tuple&& t ) \ + template \ + auto call_fused( Functor f, tuple& t ) \ + -> decltype( f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ) ) { \ + return f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ); \ + } \ + template \ + auto call_fused( Functor f, const tuple& t ) \ -> decltype( f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ) ) { \ return f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ); \ } \ @@ -113,7 +119,7 @@ namespace fc { typedef fc::tuple type; \ }; - BOOST_PP_REPEAT_FROM_TO( 1, 8, TUPLE, unused ) + BOOST_PP_REPEAT_FROM_TO( 1, 4, TUPLE, unused ) #undef FORWARD_PARAMS diff --git a/include/fc/utility.hpp b/include/fc/utility.hpp index 2fb5994..a65c9a0 100644 --- a/include/fc/utility.hpp +++ b/include/fc/utility.hpp @@ -41,11 +41,12 @@ namespace fc { template const T& min( const T& a, const T& b ) { return a < b ? a: b; } +} + // outside of namespace fc becuase of VC++ conflict with std::swap template - void swap( T& a, T& b ) { + void fc_swap( T& a, T& b ) { T tmp = fc::move(a); a = fc::move(b); b = fc::move(tmp); } -} #endif // _FC_UTILITY_HPP_ diff --git a/include/fc/value.hpp b/include/fc/value.hpp index 120d9f8..711f892 100644 --- a/include/fc/value.hpp +++ b/include/fc/value.hpp @@ -96,7 +96,7 @@ namespace fc { template value& operator=( T&& v ) { value tmp(fc::forward(v) ); - fc::swap(*this,tmp); + fc_swap(*this,tmp); return *this; } diff --git a/include/fc/value_cast.hpp b/include/fc/value_cast.hpp index 0a3ed29..be4cd4d 100644 --- a/include/fc/value_cast.hpp +++ b/include/fc/value_cast.hpp @@ -28,7 +28,7 @@ namespace fc { virtual void operator()( const uint64_t& v ){ m_out = fc::numeric_cast(v); } virtual void operator()( const float& v ){ m_out = fc::numeric_cast(v); } virtual void operator()( const double& v ){ m_out = fc::numeric_cast(v); } - virtual void operator()( const bool& v ){ m_out = fc::numeric_cast(v); } + virtual void operator()( const bool& v ){ m_out = v; } virtual void operator()( const fc::string& v ) { m_out = fc::lexical_cast(v); } virtual void operator()( const value::object& ) { FC_THROW_MSG("bad cast"); } virtual void operator()( const value::array& ) { FC_THROW_MSG("bad cast"); } @@ -36,6 +36,28 @@ namespace fc { private: T& m_out; }; + template<> + struct cast_visitor : value::const_visitor { + cast_visitor( bool& out ) + :m_out(out){} + virtual void operator()( const int8_t& v ){ m_out = v != 0; } + virtual void operator()( const int16_t& v ){ m_out = v != 0; } + virtual void operator()( const int32_t& v ){ m_out = v != 0; } + virtual void operator()( const int64_t& v ){ m_out = v != 0; } + virtual void operator()( const uint8_t& v ){ m_out = v != 0; } + virtual void operator()( const uint16_t& v ){ m_out = v != 0; } + virtual void operator()( const uint32_t& v ){ m_out = v != 0; } + virtual void operator()( const uint64_t& v ){ m_out = v != 0; } + virtual void operator()( const float& v ){ m_out = v != 0; } + virtual void operator()( const double& v ){ m_out = v != 0; } + virtual void operator()( const bool& v ){ m_out = v; } + virtual void operator()( const fc::string& v ) { m_out = !(v != "true"); } + virtual void operator()( const value::object& ) { FC_THROW_MSG("bad cast"); } + virtual void operator()( const value::array& ) { FC_THROW_MSG("bad cast"); } + virtual void operator()( ) { FC_THROW_MSG("bad cast"); } + private: + bool& m_out; + }; template<> struct cast_visitor : value::const_visitor { @@ -51,7 +73,7 @@ namespace fc { virtual void operator()( const uint64_t& v ){ m_out = fc::lexical_cast(v); } virtual void operator()( const float& v ){ m_out = fc::lexical_cast(v); } virtual void operator()( const double& v ){ m_out = fc::lexical_cast(v); } - virtual void operator()( const bool& v ){ m_out = fc::lexical_cast(v); } + virtual void operator()( const bool& v ){ m_out = v != 0 ? "true" : "false"; } virtual void operator()( const fc::string& v ){ m_out = v; } virtual void operator()( const value::object& ) { FC_THROW_MSG("bad cast"); } virtual void operator()( const value::array& ) { FC_THROW_MSG("bad cast"); } @@ -165,7 +187,7 @@ namespace fc { template<> - struct cast_visitor : value::value::const_visitor { + struct cast_visitor : value::const_visitor { virtual void operator()( const int8_t& v ) { FC_THROW_MSG("bad cast");} virtual void operator()( const int16_t& v ) { FC_THROW_MSG("bad cast");} virtual void operator()( const int32_t& v ) { FC_THROW_MSG("bad cast");} diff --git a/include/fc/vector.hpp b/include/fc/vector.hpp index 1d136f6..3664c0c 100644 --- a/include/fc/vector.hpp +++ b/include/fc/vector.hpp @@ -1,5 +1,4 @@ -#ifndef _FC_VECTOR_HPP_ -#define _FC_VECTOR_HPP_ +#pragma once #include #include #include @@ -7,15 +6,6 @@ #include -#if 0 -#include -namespace fc { - template - using vector = std::vector; -} - -#else - namespace fc { namespace detail { template @@ -27,8 +17,8 @@ namespace fc { static data* allocate( uint64_t cap ) { data* d = nullptr; if( cap ){ - d = (data*)malloc(sizeof(data) + sizeof(T)*(cap-1)); - d->capacity = cap; + d = (data*)malloc(sizeof(data) + sizeof(T)*(static_cast(cap)-1)); + d->capacity = static_cast(cap); } else { d = (data*)malloc(sizeof(data)); d->capacity = 1; @@ -38,8 +28,8 @@ namespace fc { } static data* reallocate( data* d, uint64_t cap ) { if( cap ){ - d = (data*)realloc(d,sizeof(data) + sizeof(T)*(cap-1)); - d->capacity = cap; + d = (data*)realloc(d,sizeof(data) + sizeof(T)*(static_cast(cap)-1)); + d->capacity = static_cast(cap); } else { d = (data*)realloc(d,sizeof(data)); d->capacity = 1; @@ -64,13 +54,13 @@ namespace fc { if( c.size() ) { _data = detail::data::allocate( c.size() ); _data->size = c.size(); - memcpy(begin(),c.begin(),c.size() ); + memcpy(begin(),c.begin(), static_cast(c.size()) ); } //slog( "copy: this.size %d", size() ); } vector_impl(const_iterator b, const_iterator e ):_data(nullptr) { resize(e-b); - if( size() ) memcpy( data(), b, size() ); + if( size() ) memcpy( data(), b, static_cast(size()) ); } vector_impl(uint64_t s):_data(nullptr){ resize(s); @@ -170,12 +160,12 @@ namespace fc { } vector_impl& operator=( vector_impl&& v ) { - fc::swap(_data,v._data); + fc_swap(_data,v._data); return *this; } vector_impl& operator=( const vector_impl& v ) { vector_impl tmp(v); - fc::swap(tmp._data,_data); + fc_swap(tmp._data,_data); return *this; } protected: @@ -270,7 +260,7 @@ namespace fc { ++c; ++nc; } - fc::swap( _ndata, this->_data ); + fc_swap( _ndata, this->_data ); if( _ndata ) free(_ndata); } @@ -344,12 +334,12 @@ namespace fc { return last; } vector_impl& operator=( vector_impl&& v ) { - fc::swap(_data,v._data); + fc_swap(_data,v._data); return *this; } vector_impl& operator=( const vector_impl& v ) { vector_impl tmp(v); - fc::swap(tmp._data,_data); + fc_swap(tmp._data,_data); return *this; } private: @@ -380,6 +370,4 @@ namespace fc { }; }; -#endif -#endif // _FC_VECTOR_HPP_ diff --git a/src/bigint.cpp b/src/bigint.cpp index 76b8864..ad8d5ce 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -69,7 +69,7 @@ namespace fc { bigint& bigint::operator = ( bigint&& a ) { - fc::swap( a.n, n ); + fc_swap( a.n, n ); return *this; } bigint& bigint::operator = ( const bigint& a ) { diff --git a/src/future.cpp b/src/future.cpp index 5a1c0fb..b022646 100644 --- a/src/future.cpp +++ b/src/future.cpp @@ -31,12 +31,12 @@ namespace fc { } bool promise_base::error()const { { synchronized(_spin_yield) - return _except; + return _exceptp; } } void promise_base::set_exception( const fc::exception_ptr& e ){ - _except = e; + _exceptp = e; _set_value(nullptr); } @@ -47,14 +47,14 @@ namespace fc { void promise_base::_wait_until( const time_point& timeout_us ){ { synchronized(_spin_yield) if( _ready ) { - if( _except ) fc::rethrow_exception( _except ); + if( _exceptp ) fc::rethrow_exception( _exceptp ); return; } _enqueue_thread(); } thread::current().wait_until( ptr(this,true), timeout_us ); if( _ready ) { - if( _except ) fc::rethrow_exception( _except ); + if( _exceptp ) fc::rethrow_exception( _exceptp ); return; } FC_THROW( future_wait_timeout() ); @@ -80,7 +80,7 @@ namespace fc { } _notify(); if( nullptr != _compl ) { - _compl->on_complete(s,_except); + _compl->on_complete(s,_exceptp); } } void promise_base::_on_complete( detail::completion_handler* c ) { diff --git a/src/iostream.cpp b/src/iostream.cpp index d4c0332..0206f99 100644 --- a/src/iostream.cpp +++ b/src/iostream.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace fc { ostream& operator<<( ostream& o, const char* v ) { @@ -86,7 +87,7 @@ namespace fc { void cerr_t::close() {}; void cerr_t::flush() { std::cerr.flush(); } - ostream& cerr_t::write( const fc::string& s ) { std::cerr<< *reinterpret_cast(&s); return *this; } + ostream& cerr_t::write( const fc::string& s ) { std::cerr<< static_cast(s); return *this; } size_t cin_t::readsome( char* buf, size_t len ) { cin_buffer& b = get_cin_buffer(); diff --git a/src/json.cpp b/src/json.cpp index e881ce1..2105a97 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -6,6 +6,7 @@ #include #include + namespace fc { namespace json { /** * Placeholder for unparsed json data. @@ -39,6 +40,11 @@ namespace fc { namespace json { fc::vector json_data; }; +} } + +typedef std::map jmap; + + namespace errors { enum error_type { unknown_error = 0x0001, // Other errors not specified below @@ -107,9 +113,9 @@ namespace fc { namespace json { } enum error_defaults { - default_report = json::errors::all, - default_recover = json::errors::all, - default_throw = json::errors::none, + default_report = errors::all, + default_recover = errors::all, + default_throw = errors::none, default_ignore = ~(default_report|default_recover|default_throw) }; @@ -181,7 +187,7 @@ namespace fc { namespace json { - +namespace fc { namespace json { fc::string escape_string( const fc::string& s ) { // calculate escape string size. uint32_t ecount = 0; @@ -246,9 +252,9 @@ namespace fc { namespace json { case '"' : out += '"'; break; case 'x' : { ++i; if( i == out.end() ) return out; - char c = from_hex(*i); + char c = fc::from_hex(*i); ++i; if( i == out.end() ) { out += c; return out; } - c = c<<4 | from_hex(*i); + c = c<<4 | fc::from_hex(*i); out += c; break; } @@ -290,9 +296,9 @@ namespace fc { namespace json { case '"' : *out = '"'; ++out; break; case 'x' : { ++i; if( *i == '\0' ){ *out = '\0'; return s; } - char c = from_hex(*i); + char c = fc::from_hex(*i); ++i; if( *i == '\0' ){ *out = c; ++out; *out = '\0'; return s; } - c = c<<4 | from_hex(*i); + c = c<<4 | fc::from_hex(*i); *out = c; ++out; break; @@ -308,6 +314,7 @@ namespace fc { namespace json { *out = '\0'; return s; } + } }// fc::json /** * Ignores leading white space. @@ -435,12 +442,12 @@ struct temp_set { * A,B,C * Warn on extra ',' or missing ',' */ -void read_values( value::array& a, char* in, char* end, fc::json::error_collector& ec ) { +void read_values( fc::value::array& a, char* in, char* end, error_collector& ec ) { char* ve = 0; - char* v = fc::json::read_value( in, end, ve ); + char* v = read_value( in, end, ve ); while( *v == ',' ) { wlog( "unexpected ','"); - v = fc::json::read_value( ve, end, ve ); + v = read_value( ve, end, ve ); } if( v == ve ) return; // no values @@ -451,7 +458,7 @@ void read_values( value::array& a, char* in, char* end, fc::json::error_collect do { // expect comma + value | '' // expect comma or '' - c = fc::json::read_value( ve, end, ce ); + c = read_value( ve, end, ce ); // '' means we are done, no errors if( c == ce ) return; @@ -465,12 +472,12 @@ void read_values( value::array& a, char* in, char* end, fc::json::error_collect } // expect value - v = fc::json::read_value( ce, end, ve ); + v = read_value( ce, end, ve ); while ( *v == ',' ) { // but got comma // expect value wlog( "extra comma at c->ce" ); c = v; ce = ve; - v = fc::json::read_value( ve, end, ve ); + v = read_value( ve, end, ve ); } if( v == ve ) { wlog( "trailing comma at c->ce" ); @@ -485,22 +492,22 @@ void read_values( value::array& a, char* in, char* end, fc::json::error_collect /** * Reads one level deep, does not recruse into sub objects. */ -char* read_key_val( std::map& obj, bool sc, char* in, char* end, fc::json::error_collector& ec ) { +char* read_key_val( std::map& obj, bool sc, char* in, char* end, error_collector& ec ) { char* name_end = 0; char* name = in; do { // read first char - name = fc::json::read_value( name, end, name_end ); + name = read_value( name, end, name_end ); if( sc ) { // if we expect a , if( *name != ',' ) { // but didn't get one wlog( "expected ',' but got %1%", name ); // warn and accept name } else { // we got the exepcted , read the expected name - name = fc::json::read_value( name_end, end, name_end ); + name = read_value( name_end, end, name_end ); } } else { // don't start with ',' while( *name == ',' ) { // while we don't have a name, keep looking wlog( "unexpected ',' " ); - name = fc::json::read_value( name_end, end, name_end ); + name = read_value( name_end, end, name_end ); } } } while( *name == ',' ); @@ -517,11 +524,11 @@ char* read_key_val( std::map& obj, bool sc, char* in, c wlog( "unquoted name '%1%'", name ); } else { temp_set ntemp(name_end,'\0'); - name = inplace_unescape_string(name); + name = fc::json::inplace_unescape_string(name); } char* col_end = 0; - char* col = fc::json::read_value( name_end, end, col_end ); + char* col = read_value( name_end, end, col_end ); char* val_end = 0; char* val = 0; @@ -555,7 +562,7 @@ char* read_key_val( std::map& obj, bool sc, char* in, c elog( "early end after reading name '%1%' and col '%2%'", name, col ); return name_end; } - val = fc::json::read_value( col_end, end, val_end ); + val = read_value( col_end, end, val_end ); if( val == end ) { wlog( "no value specified" ); } @@ -576,12 +583,12 @@ char* read_key_val( std::map& obj, bool sc, char* in, c * Reads an optional ',' followed by key : value, returning the next input position * @param sc - start with ',' */ -char* read_key_val( value::object& obj, bool sc, char* in, char* end, fc::json::error_collector& ec ) { +char* read_key_val( fc::value::object& obj, bool sc, char* in, char* end, error_collector& ec ) { char* name_end = 0; char* name = in; do { // read first char - name = fc::json::read_value( name, end, name_end ); + name = read_value( name, end, name_end ); if( name == name_end ) return name; if( sc ) { // if we expect a , @@ -589,12 +596,12 @@ char* read_key_val( value::object& obj, bool sc, char* in, char* end, fc::json:: if( *name != '}' ) wlog( "expected ',' or '}' but got '%s'", name ); // warn and accept name } else { // we got the exepcted , read the expected name - name = fc::json::read_value( name_end, end, name_end ); + name = read_value( name_end, end, name_end ); } } else { // don't start with ',' while( *name == ',' ) { // while we don't have a name, keep looking wlog( "unexpected ',' " ); - name = fc::json::read_value( name_end, end, name_end ); + name = read_value( name_end, end, name_end ); } } } while( *name == ',' ); @@ -611,11 +618,11 @@ char* read_key_val( value::object& obj, bool sc, char* in, char* end, fc::json:: wlog( "unquoted name '%1%'", name ); } else { temp_set ntemp(name_end,'\0'); - name = inplace_unescape_string(name); + name = fc::json::inplace_unescape_string(name); } char* col_end = 0; - char* col = fc::json::read_value( name_end, end, col_end ); + char* col = read_value( name_end, end, col_end ); char* val_end = 0; char* val = 0; @@ -649,7 +656,7 @@ char* read_key_val( value::object& obj, bool sc, char* in, char* end, fc::json:: elog( "early end after reading name '%1%' and col '%2%'", name, col ); return name_end; } - val = fc::json::read_value( col_end, end, val_end ); + val = read_value( col_end, end, val_end ); if( val == end ) { wlog( "no value specified" ); } @@ -657,13 +664,13 @@ char* read_key_val( value::object& obj, bool sc, char* in, char* end, fc::json:: temp_set ntemp(name_end,'\0'); temp_set vtemp(val_end,'\0'); //slog( "name: '%1%'", fc::string(name,name_end) ); - obj.fields.push_back( value::key_val( name, to_value( val, val_end, ec ) ) ); + obj.fields.push_back( fc::value::key_val( name, to_value( val, val_end, ec ) ) ); return val_end; } // first_key =:: '' | "name" : VALUE *list_key // list_key '' | ',' "name" : VALUE -void read_key_vals( value::object& obj, char* in, char* end, fc::json::error_collector& ec ) { +void read_key_vals( fc::value::object& obj, char* in, char* end, error_collector& ec ) { bool ex_c = false; char* kv_end = in; do { @@ -674,8 +681,8 @@ void read_key_vals( value::object& obj, char* in, char* end, fc::json::error_col } // first_key =:: '' | "name" : VALUE *list_key // list_key '' | ',' "name" : VALUE -std::map read_key_vals( char* in, char* end, fc::json::error_collector& ec ) { - std::map m; +std::map read_key_vals( char* in, char* end, error_collector& ec ) { + std::map m; bool ex_c = false; char* kv_end = in; do { @@ -692,7 +699,7 @@ std::map read_key_vals( char* in, char* end, fc::json:: * @brief adaptor for to_value( char*, char*, error_collector& ) */ fc::value to_value( fc::vector&& v, error_collector& ec ) { - if( v.size() == 0 ) return value(); + if( v.size() == 0 ) return fc::value(); return to_value( &v.front(), &v.front() + v.size(), ec ); } @@ -703,21 +710,21 @@ fc::value to_value( fc::vector&& v, error_collector& ec ) { * any errors that occur while parsing the string. */ fc::value to_value( char* start, char* end, error_collector& ec ) { - if( start == end ) return value(); + if( start == end ) return fc::value(); char* ve = 0; char* s = read_value( start, end, ve ); //slog( "'%1%'", fc::string(start,ve) ); switch( s[0] ) { case '[': { - value::array a; + fc::value::array a; read_values( a, s+1, ve -1, ec ); return a; } case '{': { - value::object o; + fc::value::object o; read_key_vals( o, s+1, ve -1, ec ); - value v = fc::move(o); + fc::value v = fc::move(o); return v; } case '0': @@ -753,11 +760,11 @@ fc::value to_value( char* start, char* end, error_collector& ec ) { } case '\"': { temp_set move_end(ve,'\0'); - return inplace_unescape_string( s ); + return fc::json::inplace_unescape_string( s ); } case 'n': { temp_set move_end(ve,'\0'); - if( strcmp(s,"null" ) == 0) return value(); + if( strcmp(s,"null" ) == 0) return fc::value(); } case 't': { temp_set move_end(ve,'\0'); @@ -770,9 +777,10 @@ fc::value to_value( char* start, char* end, error_collector& ec ) { default: wlog( "return unable to parse... return as string '%s'", fc::string(s,ve).c_str() ); - return value( fc::string( s, ve) ); + return fc::value( fc::string( s, ve) ); } } +namespace fc { namespace json { fc::string pretty_print( fc::vector&& v, uint8_t indent ) { int level = 0; @@ -871,8 +879,8 @@ fc::string pretty_print( fc::vector&& v, uint8_t indent ) { virtual void operator()( const float& v ){ os << v; } virtual void operator()( const double& v ){ os << v; } virtual void operator()( const bool& v ){ os << (v ? "true" : "false"); } - virtual void operator()( const fc::string& v ){ os << '"' << escape_string(v) <<'"'; } - virtual void operator()( const value::object& o ){ + virtual void operator()( const fc::string& v ){ os << '"' << fc::json::escape_string(v) <<'"'; } + virtual void operator()( const fc::value::object& o ){ os << '{'; for( uint32_t i = 0; i < o.fields.size(); ++i ) { if( i ) os <<','; @@ -918,7 +926,7 @@ fc::string pretty_print( fc::vector&& v, uint8_t indent ) { // memory map the file file_mapping fmap( local_path.string().c_str(), read_only ); - size_t fsize = file_size(local_path); + size_t fsize = static_cast(file_size(local_path)); mapped_region mr( fmap, fc::read_only, 0, fsize ); @@ -929,8 +937,8 @@ fc::string pretty_print( fc::vector&& v, uint8_t indent ) { // TODO: implement a const version of to_value fc::vector tmp(pos,end); - json::error_collector ec; - return fc::json::to_value(tmp.data(), tmp.data()+fsize,ec); + error_collector ec; + return to_value(tmp.data(), tmp.data()+fsize,ec); } value from_string( const fc::string& s ) { @@ -945,5 +953,4 @@ fc::string pretty_print( fc::vector&& v, uint8_t indent ) { return from_string( fc::vector(s,e) ); } - -}} // fc::json +} } diff --git a/src/json_rpc_connection.cpp b/src/json_rpc_connection.cpp index e9d908e..ebc8684 100644 --- a/src/json_rpc_connection.cpp +++ b/src/json_rpc_connection.cpp @@ -122,7 +122,7 @@ namespace fc { namespace json { rpc_connection::rpc_connection( const rpc_connection& c ) :my(c.my){ } rpc_connection::rpc_connection( rpc_connection&& c ) { - fc::swap(my,c.my); + fc_swap(my,c.my); } rpc_connection::~rpc_connection() { } @@ -132,7 +132,7 @@ namespace fc { namespace json { return *this; } rpc_connection& rpc_connection::operator=(rpc_connection&& m) { - fc::swap(m.my,my); + fc_swap(m.my,my); return *this; } diff --git a/src/log.cpp b/src/log.cpp index 6b03537..f731119 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -4,7 +4,9 @@ #include #include #include +#ifndef WIN32 #include +#endif #include @@ -32,8 +34,10 @@ namespace fc { void log( const char* color, const char* file_name, size_t line_num, const char* method_name, const char* format, ... ) { fc::unique_lock lock(log_mutex()); + #ifndef WIN32 if(isatty(fileno(stderr))) std::cerr<<"\r"< #include +#include #include -namespace detail { - void destroy( void* t ) { - using namespace std; - reinterpret_cast(t)->~string(); - } -} /** * Implemented with std::string for now. @@ -15,112 +10,48 @@ namespace detail { namespace fc { - string::string(const char* s, int l) { - static_assert( sizeof(*this) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(s,l); - } - string::string() { - static_assert( sizeof(*this) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(); - } - - string::string( const string& c ) { - static_assert( sizeof(my) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(reinterpret_cast(c)); - } - - - string::string( string&& m ) { - static_assert( sizeof(my) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(reinterpret_cast(m)); - } - - string::string( const char* c ){ - static_assert( sizeof(my) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(c); - } - - string::string( const_iterator b, const_iterator e ) { - static_assert( sizeof(my) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(b,e); - } - string::string( const std::string& s ) { - static_assert( sizeof(my) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(s); - } - string::string( std::string&& s ) { - static_assert( sizeof(my) >= sizeof(std::string), "failed to reserve enough space" ); - new (this) std::string(fc::move(s)); - } - - string::operator std::string&() { - return *reinterpret_cast(this); - } - string::operator const std::string&()const { - return *reinterpret_cast(this); - } - - string::~string() { - ::detail::destroy( this ); - } + string::string(const char* s, int l) :my(s,l){ } + string::string(){} + string::string( const string& c ):my(c.my) { } + string::string( string&& m ):my(fc::move(m.my)) {} + string::string( const char* c ):my(c){} + string::string( const_iterator b, const_iterator e ):my(b,e){} + string::string( const std::string& s ):my(s) {} + string::string( std::string&& s ):my(fc::move(s)) {} + string::~string() { } + string::operator std::string&() { return *my; } + string::operator const std::string&()const { return *my; } string::iterator string::begin() { return &(*this)[0]; } string::iterator string::end() { return &(*this)[size()]; } - string::const_iterator string::begin()const { return reinterpret_cast(this)->c_str(); } - string::const_iterator string::end()const { return reinterpret_cast(this)->c_str() + reinterpret_cast(this)->size(); } + string::const_iterator string::begin()const { return my->c_str(); } + string::const_iterator string::end()const { return my->c_str() + my->size(); } - char& string::operator[](uint64_t idx) { return reinterpret_cast(this)->at(idx); } - const char& string::operator[](uint64_t idx)const { return reinterpret_cast(this)->at(idx); } + char& string::operator[](uint64_t idx) { return (*my)[idx]; } + const char& string::operator[](uint64_t idx)const { return (*my)[idx]; } - void string::reserve(uint64_t r) { reinterpret_cast(this)->reserve(r); } - uint64_t string::size()const { return reinterpret_cast(this)->size(); } - uint64_t string::find(char c, uint64_t p)const { return reinterpret_cast(this)->find(c,p); } - void string::clear() { return reinterpret_cast(this)->clear(); } - void string::resize( uint64_t s ) { reinterpret_cast(this)->resize(s); } + void string::reserve(uint64_t r) { my->reserve(r); } + uint64_t string::size()const { return my->size(); } + uint64_t string::find(char c, uint64_t p)const { return my->find(c,p); } + void string::clear() { my->clear(); } + void string::resize( uint64_t s ) { my->resize(s); } + fc::string string::substr( int32_t start, int32_t len ) { return my->substr(start,len); } + const char* string::c_str()const { return my->c_str(); } - fc::string string::substr( int32_t start, int32_t len ) { - return reinterpret_cast(*this).substr(start,len).c_str(); - } - const char* string::c_str()const { return reinterpret_cast(this)->c_str(); } + bool string::operator == ( const char* s )const { return *my == s; } + bool string::operator == ( const string& s )const { return *my == *s.my; } + bool string::operator != ( const string& s )const { return *my != *s.my; } - bool string::operator == ( const char* s )const { - return reinterpret_cast(*this) == s; - } - bool string::operator == ( const string& s )const { - return reinterpret_cast(*this) == reinterpret_cast(s); - } - bool string::operator != ( const string& s )const { - return reinterpret_cast(*this) != reinterpret_cast(s); - } + string& string::operator =( const string& c ) { *my = *c.my; return *this; } + string& string::operator =( string&& c ) { *my = fc::move( *c.my ); return *this; } - string& string::operator =( const string& c ) { - reinterpret_cast(*this) = reinterpret_cast(c); - return *this; - } - string& string::operator =( string&& c ) { - reinterpret_cast(*this) = fc::move( reinterpret_cast(c) ); - return *this; - } + string& string::operator+=( const string& s ) { *my += *s.my; return *this; } + string& string::operator+=( char c ) { *my += c; return *this; } - string& string::operator+=( const string& s ) { - reinterpret_cast(*this) += reinterpret_cast(s); - return *this; - } - string& string::operator+=( char c ) { - reinterpret_cast(*this) += c; - return *this; - } - - bool operator < ( const string& a, const string& b ) { - return reinterpret_cast(a) < reinterpret_cast(b); - } - string operator + ( const string& s, const string& c ) { - return string(s) += c; - } - string operator + ( const string& s, char c ) { - return string(s) += c; - } + bool operator < ( const string& a, const string& b ) { return *a.my < *b.my; } + string operator + ( const string& s, const string& c ) { return string(s) += c; } + string operator + ( const string& s, char c ) { return string(s) += c; } } // namespace fc diff --git a/src/thread.cpp b/src/thread.cpp index 239a925..26b2b3b 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -50,7 +50,7 @@ namespace fc { } thread& thread::operator=(thread&& t ) { - fc::swap(t.my,my); + fc_swap(t.my,my); return *this; } diff --git a/src/value.cpp b/src/value.cpp index c29ac2a..783dc61 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -6,7 +6,8 @@ namespace fc { namespace detail { - struct value_visitor { + class value_visitor { + public: virtual void operator()( int8_t& v ){}; virtual void operator()( int16_t& v ){}; virtual void operator()( int32_t& v ){}; @@ -24,6 +25,8 @@ namespace fc { virtual void operator()( ){}; }; + void value_holder::visit( value::const_visitor&& v )const {v(); } + void value_holder::visit( value_visitor&& v ) {v(); } // fundamental values... template @@ -47,11 +50,11 @@ namespace fc { template<> struct value_holder_impl : value_holder { value_holder_impl(){}; + virtual void visit( value::const_visitor&& v )const{ v(); } + virtual void visit( value_visitor&& v ) { v(); } // typedef void_t T; /* virtual const char* type()const { return "void"; } - virtual void visit( value::const_visitor&& v )const{ v(); } - virtual void visit( value_visitor&& v ) { v(); } virtual void clear() { } virtual size_t size()const { return 0; } @@ -127,8 +130,6 @@ namespace fc { value_holder* value_holder::move_helper( char* c ) { return new(c) value_holder(); } value_holder* value_holder::copy_helper( char* c )const { return new(c) value_holder(); } - void value_holder::visit( value::const_visitor&& v )const { v(); } - void value_holder::visit( value_visitor&& v ) { v(); } void value_holder::clear() {} size_t value_holder::size()const { return 0; } diff --git a/tests/json_rpc_test.cpp b/tests/json_rpc_test.cpp index fc448c1..cc4d563 100644 --- a/tests/json_rpc_test.cpp +++ b/tests/json_rpc_test.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include