#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fc { namespace raw { template inline void pack( Stream& s, const Arg0& a0, Args... args ) { pack( s, a0 ); pack( s, args... ); } template inline void pack( Stream& s, const fc::exception& e ) { fc::raw::pack( s, e.code() ); fc::raw::pack( s, std::string(e.name()) ); fc::raw::pack( s, std::string(e.what()) ); fc::raw::pack( s, e.get_log() ); } template inline void unpack( Stream& s, fc::exception& e ) { int64_t code; std::string name, what; log_messages msgs; fc::raw::unpack( s, code ); fc::raw::unpack( s, name ); fc::raw::unpack( s, what ); fc::raw::unpack( s, msgs ); e = fc::exception( fc::move(msgs), code, name, what ); } template inline void pack( Stream& s, const fc::log_message& msg ) { fc::raw::pack( s, variant(msg) ); } template inline void unpack( Stream& s, fc::log_message& msg ) { fc::variant vmsg; fc::raw::unpack( s, vmsg ); msg = vmsg.as(); } template inline void pack( Stream& s, const fc::path& tp ) { fc::raw::pack( s, tp.generic_string() ); } template inline void unpack( Stream& s, fc::path& tp ) { std::string p; fc::raw::unpack( s, p ); tp = p; } template inline void pack( Stream& s, const fc::time_point_sec& tp ) { uint32_t usec = tp.sec_since_epoch(); s.write( (const char*)&usec, sizeof(usec) ); } template inline void unpack( Stream& s, fc::time_point_sec& tp ) { try { uint32_t sec; s.read( (char*)&sec, sizeof(sec) ); tp = fc::time_point() + fc::seconds(sec); } FC_RETHROW_EXCEPTIONS( warn, "" ) } template inline void pack( Stream& s, const fc::time_point& tp ) { uint64_t usec = tp.time_since_epoch().count(); s.write( (const char*)&usec, sizeof(usec) ); } template inline void unpack( Stream& s, fc::time_point& tp ) { try { uint64_t usec; s.read( (char*)&usec, sizeof(usec) ); tp = fc::time_point() + fc::microseconds(usec); } FC_RETHROW_EXCEPTIONS( warn, "" ) } template inline void pack( Stream& s, const fc::microseconds& usec ) { uint64_t usec_as_int64 = usec.count(); s.write( (const char*)&usec_as_int64, sizeof(usec_as_int64) ); } template inline void unpack( Stream& s, fc::microseconds& usec ) { try { uint64_t usec_as_int64; s.read( (char*)&usec_as_int64, sizeof(usec_as_int64) ); usec = fc::microseconds(usec_as_int64); } FC_RETHROW_EXCEPTIONS( warn, "" ) } template inline void pack( Stream& s, const fc::array& v) { s.write((const char*)&v.data[0],N*sizeof(T)); } template inline void pack( Stream& s, const std::shared_ptr& v) { fc::raw::pack( s, *v ); } template inline void unpack( Stream& s, fc::array& v) { try { s.read((char*)&v.data[0],N*sizeof(T)); } FC_RETHROW_EXCEPTIONS( warn, "fc::array", ("type",fc::get_typename::name())("length",N) ) } template inline void unpack( Stream& s, std::shared_ptr& v) { try { v = std::make_shared(); fc::raw::unpack( s, *v ); } FC_RETHROW_EXCEPTIONS( warn, "std::shared_ptr", ("type",fc::get_typename::name()) ) } template inline void pack( Stream& s, const signed_int& v ) { uint32_t val = (v.value<<1) ^ (v.value>>31); do { uint8_t b = uint8_t(val) & 0x7f; val >>= 7; b |= ((val > 0) << 7); s.write((char*)&b,1);//.put(b); } while( val ); } template inline void pack( Stream& s, const unsigned_int& v ) { uint64_t val = v.value; do { uint8_t b = uint8_t(val) & 0x7f; val >>= 7; b |= ((val > 0) << 7); s.write((char*)&b,1);//.put(b); }while( val ); } template inline void unpack( Stream& s, signed_int& vi ) { uint32_t v = 0; char b = 0; int by = 0; do { s.get(b); v |= uint32_t(uint8_t(b) & 0x7f) << by; by += 7; } while( uint8_t(b) & 0x80 ); vi.value = ((v>>1) ^ (v>>31)) + (v&0x01); vi.value = v&0x01 ? vi.value : -vi.value; vi.value = -vi.value; } template inline void unpack( Stream& s, unsigned_int& vi ) { uint64_t v = 0; char b = 0; uint8_t by = 0; do { s.get(b); v |= uint32_t(uint8_t(b) & 0x7f) << by; by += 7; } while( uint8_t(b) & 0x80 ); vi.value = static_cast(v); } template inline void unpack( Stream& s, const T& vi ) { T tmp; fc::raw::unpack( s, tmp ); FC_ASSERT( vi == tmp ); } template inline void pack( Stream& s, const char* v ) { fc::raw::pack( s, fc::string(v) ); } template void pack( Stream& s, const safe& v ) { fc::raw::pack( s, v.value ); } template void unpack( Stream& s, fc::safe& v ) { fc::raw::unpack( s, v.value ); } template void pack( Stream& s, const fc::fwd& v ) { fc::raw::pack( *v ); } template void unpack( Stream& s, fc::fwd& v ) { fc::raw::unpack( *v ); } template void pack( Stream& s, const fc::smart_ref& v ) { fc::raw::pack( s, *v ); } template void unpack( Stream& s, fc::smart_ref& v ) { fc::raw::unpack( s, *v ); } // optional template void pack( Stream& s, const fc::optional& v ) { fc::raw::pack( s, bool(!!v) ); if( !!v ) fc::raw::pack( s, *v ); } template void unpack( Stream& s, fc::optional& v ) { try { bool b; fc::raw::unpack( s, b ); if( b ) { v = T(); fc::raw::unpack( s, *v ); } } FC_RETHROW_EXCEPTIONS( warn, "optional<${type}>", ("type",fc::get_typename::name() ) ) } // std::vector template inline void pack( Stream& s, const std::vector& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); if( value.size() ) s.write( &value.front(), (uint32_t)value.size() ); } template inline void unpack( Stream& s, std::vector& value ) { unsigned_int size; fc::raw::unpack( s, size ); FC_ASSERT( size.value < MAX_ARRAY_ALLOC_SIZE ); value.resize(size.value); if( value.size() ) s.read( value.data(), value.size() ); } // fc::string template inline void pack( Stream& s, const fc::string& v ) { fc::raw::pack( s, unsigned_int((uint32_t)v.size())); if( v.size() ) s.write( v.c_str(), v.size() ); } template inline void unpack( Stream& s, fc::string& v ) { std::vector tmp; fc::raw::unpack(s,tmp); if( tmp.size() ) v = fc::string(tmp.data(),tmp.data()+tmp.size()); else v = fc::string(); } // bool template inline void pack( Stream& s, const bool& v ) { fc::raw::pack( s, uint8_t(v) ); } template inline void unpack( Stream& s, bool& v ) { uint8_t b; fc::raw::unpack( s, b ); FC_ASSERT( (b & ~1) == 0 ); v=(b!=0); } namespace detail { template struct pack_object_visitor { pack_object_visitor(const Class& _c, Stream& _s) :c(_c),s(_s){} template void operator()( const char* name )const { fc::raw::pack( s, c.*p ); } private: const Class& c; Stream& s; }; template struct unpack_object_visitor { unpack_object_visitor(Class& _c, Stream& _s) :c(_c),s(_s){} template inline void operator()( const char* name )const { try { fc::raw::unpack( s, c.*p ); } FC_RETHROW_EXCEPTIONS( warn, "Error unpacking field ${field}", ("field",name) ) } private: Class& c; Stream& s; }; template struct if_class{ template static inline void pack( Stream& s, const T& v ) { s << v; } template static inline void unpack( Stream& s, T& v ) { s >> v; } }; template<> struct if_class { template static inline void pack( Stream& s, const T& v ) { s.write( (char*)&v, sizeof(v) ); } template static inline void unpack( Stream& s, T& v ) { s.read( (char*)&v, sizeof(v) ); } }; template struct if_enum { template static inline void pack( Stream& s, const T& v ) { fc::reflector::visit( pack_object_visitor( v, s ) ); } template static inline void unpack( Stream& s, T& v ) { fc::reflector::visit( unpack_object_visitor( v, s ) ); } }; template<> struct if_enum { template static inline void pack( Stream& s, const T& v ) { fc::raw::pack(s, signed_int((int32_t)v)); } template static inline void unpack( Stream& s, T& v ) { signed_int temp; fc::raw::unpack(s, temp); v = (T)temp.value; } }; template struct if_reflected { template static inline void pack( Stream& s, const T& v ) { if_class::type>::pack(s,v); } template static inline void unpack( Stream& s, T& v ) { if_class::type>::unpack(s,v); } }; template<> struct if_reflected { template static inline void pack( Stream& s, const T& v ) { if_enum< typename fc::reflector::is_enum >::pack(s,v); } template static inline void unpack( Stream& s, T& v ) { if_enum< typename fc::reflector::is_enum >::unpack(s,v); } }; } // namesapce detail template inline void pack( Stream& s, const std::unordered_set& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::pack( s, *itr ); ++itr; } } template inline void unpack( Stream& s, std::unordered_set& value ) { unsigned_int size; fc::raw::unpack( s, size ); value.clear(); FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE ); value.reserve(size.value); for( uint32_t i = 0; i < size.value; ++i ) { T tmp; fc::raw::unpack( s, tmp ); value.insert( std::move(tmp) ); } } template inline void pack( Stream& s, const std::pair& value ) { fc::raw::pack( s, value.first ); fc::raw::pack( s, value.second ); } template inline void unpack( Stream& s, std::pair& value ) { fc::raw::unpack( s, value.first ); fc::raw::unpack( s, value.second ); } template inline void pack( Stream& s, const std::unordered_map& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::pack( s, *itr ); ++itr; } } template inline void unpack( Stream& s, std::unordered_map& value ) { unsigned_int size; fc::raw::unpack( s, size ); value.clear(); FC_ASSERT( size.value*(sizeof(K)+sizeof(V)) < MAX_ARRAY_ALLOC_SIZE ); value.reserve(size.value); for( uint32_t i = 0; i < size.value; ++i ) { std::pair tmp; fc::raw::unpack( s, tmp ); value.insert( std::move(tmp) ); } } template inline void pack( Stream& s, const std::map& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::pack( s, *itr ); ++itr; } } template inline void unpack( Stream& s, std::map& value ) { unsigned_int size; fc::raw::unpack( s, size ); value.clear(); FC_ASSERT( size.value*(sizeof(K)+sizeof(V)) < MAX_ARRAY_ALLOC_SIZE ); for( uint32_t i = 0; i < size.value; ++i ) { std::pair tmp; fc::raw::unpack( s, tmp ); value.insert( std::move(tmp) ); } } template inline void pack( Stream& s, const std::deque& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::pack( s, *itr ); ++itr; } } template inline void unpack( Stream& s, std::deque& value ) { unsigned_int size; fc::raw::unpack( s, size ); FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE ); value.resize(size.value); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::unpack( s, *itr ); ++itr; } } template inline void pack( Stream& s, const std::vector& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::pack( s, *itr ); ++itr; } } template inline void unpack( Stream& s, std::vector& value ) { unsigned_int size; fc::raw::unpack( s, size ); FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE ); value.resize(size.value); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::unpack( s, *itr ); ++itr; } } template inline void pack( Stream& s, const std::set& value ) { fc::raw::pack( s, unsigned_int((uint32_t)value.size()) ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { fc::raw::pack( s, *itr ); ++itr; } } template inline void unpack( Stream& s, std::set& value ) { unsigned_int size; fc::raw::unpack( s, size ); for( uint64_t i = 0; i < size.value; ++i ) { T tmp; fc::raw::unpack( s, tmp ); value.insert( std::move(tmp) ); } } template inline void pack( Stream& s, const T& v ) { fc::raw::detail::if_reflected< typename fc::reflector::is_defined >::pack(s,v); } template inline void unpack( Stream& s, T& v ) { try { fc::raw::detail::if_reflected< typename fc::reflector::is_defined >::unpack(s,v); } FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename::name() ) ) } template inline size_t pack_size( const T& v ) { datastream ps; fc::raw::pack(ps,v ); return ps.tellp(); } template inline std::vector pack( const T& v ) { datastream ps; fc::raw::pack(ps,v ); std::vector vec(ps.tellp()); if( vec.size() ) { datastream ds( vec.data(), size_t(vec.size()) ); fc::raw::pack(ds,v); } return vec; } template inline std::vector pack( const T& v, Next... next ) { datastream ps; fc::raw::pack(ps,v,next...); std::vector vec(ps.tellp()); if( vec.size() ) { datastream ds( vec.data(), size_t(vec.size()) ); fc::raw::pack(ds,v,next...); } return vec; } template inline T unpack( const std::vector& s ) { try { T tmp; if( s.size() ) { datastream ds( s.data(), size_t(s.size()) ); fc::raw::unpack(ds,tmp); } return tmp; } FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename::name() ) ) } template inline void unpack( const std::vector& s, T& tmp ) { try { if( s.size() ) { datastream ds( s.data(), size_t(s.size()) ); fc::raw::unpack(ds,tmp); } } FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename::name() ) ) } template inline void pack( char* d, uint32_t s, const T& v ) { datastream ds(d,s); fc::raw::pack(ds,v ); } template inline T unpack( const char* d, uint32_t s ) { try { T v; datastream ds( d, s ); fc::raw::unpack(ds,v); return v; } FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename::name() ) ) } template inline void unpack( const char* d, uint32_t s, T& v ) { try { datastream ds( d, s ); fc::raw::unpack(ds,v); return v; } FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename::name() ) ) } template struct pack_static_variant { Stream& stream; pack_static_variant( Stream& s ):stream(s){} typedef void result_type; template void operator()( const T& v )const { fc::raw::pack( stream, v ); } }; template struct unpack_static_variant { Stream& stream; unpack_static_variant( Stream& s ):stream(s){} typedef void result_type; template void operator()( T& v )const { fc::raw::unpack( stream, v ); } }; template void pack( Stream& s, const static_variant& sv ) { fc::raw::pack( s, unsigned_int(sv.which()) ); sv.visit( pack_static_variant(s) ); } template void unpack( Stream& s, static_variant& sv ) { unsigned_int w; fc::raw::unpack( s, w ); sv.set_which(w.value); sv.visit( unpack_static_variant(s) ); } } } // namespace fc::raw