2 #include <boost/endian/buffers.hpp>
20 template<
typename Stream,
typename Arg0,
typename... Args>
21 inline void pack( Stream& s,
const Arg0& a0, Args... args, uint32_t _max_depth ) {
24 pack( s, a0, _max_depth );
25 pack( s, args..., _max_depth );
28 template<
typename Stream>
29 inline void pack( Stream& s,
const uint128_t& v, uint32_t _max_depth )
31 boost::endian::little_uint64_buf_at hilo[2];
34 s.write( (
char*)hilo[0].data(),
sizeof(hilo) );
36 template<
typename Stream>
37 inline void unpack( Stream& s, uint128_t& v, uint32_t _max_depth )
39 boost::endian::little_uint64_buf_at hilo[2];
40 s.read( (
char*) hilo,
sizeof(hilo) );
41 v =
uint128( hilo[0].value(), hilo[1].value() );
44 template<
typename Stream>
54 template<
typename Stream>
60 std::string name, what;
71 template<
typename Stream>
78 template<
typename Stream>
88 template<
typename Stream>
89 inline void pack( Stream& s,
const fc::path& tp, uint32_t _max_depth )
95 template<
typename Stream>
104 template<
typename Stream>
110 template<
typename Stream>
114 unpack( s, sec, _max_depth );
118 template<
typename Stream>
124 template<
typename Stream>
128 unpack( s, usec, _max_depth );
132 template<
typename Stream>
138 template<
typename Stream>
141 uint64_t usec_as_int64;
142 unpack( s, usec_as_int64, _max_depth );
146 template<
typename Stream,
size_t N>
147 inline void pack( Stream& s,
const std::array<char,N>& v, uint32_t _max_depth ) {
148 s.write( v.data(), N );
150 template<
typename Stream,
size_t N>
151 inline void pack( Stream& s,
const std::array<unsigned char,N>& v, uint32_t _max_depth ) {
152 s.write( (
char*)v.data(), N );
155 template<
typename Stream,
size_t N>
156 inline void unpack( Stream& s, std::array<char,N>& v, uint32_t _max_depth ) {
try {
157 s.read( v.data(), N );
159 template<
typename Stream,
size_t N>
160 inline void unpack( Stream& s, std::array<unsigned char,N>& v, uint32_t _max_depth ) {
try {
161 s.read( (
char*)v.data(), N );
164 template<
typename Stream,
typename T>
165 inline void pack( Stream& s,
const std::shared_ptr<T>& v, uint32_t _max_depth )
171 template<
typename Stream,
typename T>
172 inline void unpack( Stream& s, std::shared_ptr<T>& v, uint32_t _max_depth )
175 v = std::make_shared<T>();
179 template<
typename Stream,
typename T>
180 inline void pack( Stream& s,
const std::shared_ptr<const T>& v, uint32_t _max_depth )
186 template<
typename Stream,
typename T>
187 inline void unpack( Stream& s, std::shared_ptr<const T>& v, uint32_t _max_depth )
192 v = std::make_shared<const T>(std::move(tmp));
195 template<
typename Stream>
inline void pack( Stream& s,
const unsigned_int& v, uint32_t _max_depth ) {
196 uint64_t val = v.
value;
198 uint8_t b = uint8_t(val) & 0x7f;
200 b |= ((val > 0) << 7);
201 s.write((
char*)&b,1);
206 uint64_t v = 0;
char b = 0; uint8_t by = 0;
209 if( by >= 64 || (by == 63 && uint8_t(b) > 1) )
211 v |= uint64_t(uint8_t(b) & 0x7f) << by;
213 }
while( uint8_t(b) & 0x80 );
214 vi.
value =
static_cast<uint64_t
>(v);
217 template<
typename Stream,
typename T>
inline void unpack( Stream& s,
const T& vi, uint32_t _max_depth )
225 template<
typename Stream>
inline void pack( Stream& s,
const char* v, uint32_t _max_depth )
231 template<
typename Stream,
typename T>
238 template<
typename Stream,
typename T>
245 template<
typename Stream,
typename T,
unsigned int S,
typename Align>
251 template<
typename Stream,
typename T,
unsigned int S,
typename Align>
258 template<
typename Stream,
typename T>
266 template<
typename Stream,
typename T>
276 template<
typename Stream>
inline void pack( Stream& s,
const std::vector<char>& value, uint32_t _max_depth ) {
280 s.write( &value.front(), value.size() );
282 template<
typename Stream>
inline void unpack( Stream& s, std::vector<char>& value, uint32_t _max_depth ) {
286 value.resize(size.
value);
288 s.read( value.data(), value.size() );
292 template<
typename Stream>
inline void pack( Stream& s,
const std::string& v, uint32_t _max_depth ) {
295 if( v.size() ) s.write( v.c_str(), v.size() );
298 template<
typename Stream>
inline void unpack( Stream& s, std::string& v, uint32_t _max_depth ) {
302 v = std::string( tmp.data(), tmp.data()+tmp.size() );
303 else v = std::string();
307 template<
typename Stream>
inline void pack( Stream& s,
const bool& v, uint32_t _max_depth )
310 fc::raw::pack( s, v ? uint8_t(1) : uint8_t(0), _max_depth - 1 );
312 template<
typename Stream>
inline void unpack( Stream& s,
bool& v, uint32_t _max_depth )
323 template<
typename Stream,
typename Class>
326 :c(_c),s(_s),max_depth(_max_depth - 1)
331 template<
typename T,
typename C, T(C::*p)>
338 const uint32_t max_depth;
341 template<
typename Stream,
typename Class>
348 template<
typename T,
typename C, T(C::*p)>
356 const uint32_t max_depth;
361 template<
typename T,
typename Dummy =
void>
366 template<
typename Stream>
367 static inline void pack( Stream& s,
const T v, uint32_t _max_depth ) =
delete;
368 template<
typename Stream>
369 static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) =
delete;
374 template<
typename Stream>
375 static inline void pack( Stream& s,
const int64_t v, uint32_t _max_depth ) {
376 boost::endian::little_int64_buf_t tmp;
378 s.write( (
char*)&tmp,
sizeof(tmp) );
380 template<
typename Stream>
381 static inline void unpack( Stream& s, int64_t& v, uint32_t _max_depth ) {
382 boost::endian::little_int64_buf_t tmp;
383 s.read( (
char*)&tmp,
sizeof(tmp) );
390 template<
typename Stream>
391 static inline void pack( Stream& s,
const uint64_t v, uint32_t _max_depth ) {
392 boost::endian::little_uint64_buf_t tmp;
394 s.write( (
char*)&tmp,
sizeof(tmp) );
396 template<
typename Stream>
397 static inline void unpack( Stream& s, uint64_t& v, uint32_t _max_depth ) {
398 boost::endian::little_uint64_buf_t tmp;
399 s.read( (
char*)&tmp,
sizeof(tmp) );
406 template<
typename Stream>
407 static inline void pack( Stream& s,
const int32_t v, uint32_t _max_depth ) {
408 boost::endian::little_int32_buf_t tmp;
410 s.write( (
char*)&tmp,
sizeof(tmp) );
412 template<
typename Stream>
413 static inline void unpack( Stream& s, int32_t& v, uint32_t _max_depth ) {
414 boost::endian::little_int32_buf_t tmp;
415 s.read( (
char*)&tmp,
sizeof(tmp) );
422 template<
typename Stream>
423 static inline void pack( Stream& s,
const uint32_t v, uint32_t _max_depth ) {
424 boost::endian::little_uint32_buf_t tmp;
426 s.write( (
char*)&tmp,
sizeof(tmp) );
428 template<
typename Stream>
429 static inline void unpack( Stream& s, uint32_t& v, uint32_t _max_depth ) {
430 boost::endian::little_uint32_buf_t tmp;
431 s.read( (
char*)&tmp,
sizeof(tmp) );
438 template<
typename Stream>
439 static inline void pack( Stream& s,
const int16_t v, uint32_t _max_depth ) {
440 boost::endian::little_int16_buf_t tmp;
442 s.write( (
char*)&tmp,
sizeof(tmp) );
444 template<
typename Stream>
445 static inline void unpack( Stream& s, int16_t& v, uint32_t _max_depth ) {
446 boost::endian::little_int16_buf_t tmp;
447 s.read( (
char*)&tmp,
sizeof(tmp) );
454 template<
typename Stream>
455 static inline void pack( Stream& s,
const uint16_t v, uint32_t _max_depth ) {
456 boost::endian::little_uint16_buf_t tmp;
458 s.write( (
char*)&tmp,
sizeof(tmp) );
460 template<
typename Stream>
461 static inline void unpack( Stream& s, uint16_t& v, uint32_t _max_depth ) {
462 boost::endian::little_uint16_buf_t tmp;
463 s.read( (
char*)&tmp,
sizeof(tmp) );
470 template<
typename Stream>
471 static inline void pack( Stream& s,
const int8_t v, uint32_t _max_depth ) {
472 s.write( (
char*)&v, 1 );
474 template<
typename Stream>
475 static inline void unpack( Stream& s, int8_t& v, uint32_t _max_depth ) {
476 s.read( (
char*)&v, 1 );
482 template<
typename Stream>
483 static inline void pack( Stream& s,
const uint8_t v, uint32_t _max_depth ) {
484 s.write( (
char*)&v, 1 );
486 template<
typename Stream>
487 static inline void unpack( Stream& s, uint8_t& v, uint32_t _max_depth ) {
488 s.read( (
char*)&v, 1 );
492 template<
typename T,
typename Dummy=
void>
495 struct if_enum<T,
std::enable_if_t<!std::is_enum<T>::value>> {
496 template<
typename Stream>
497 static inline void pack( Stream& s,
const T& v, uint32_t _max_depth ) {
501 template<
typename Stream>
502 static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) {
508 struct if_enum<T,
std::enable_if_t<std::is_enum<T>::value>> {
509 template<
typename Stream>
510 static inline void pack( Stream& s,
const T& v, uint32_t _max_depth ) {
514 template<
typename Stream>
515 static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) {
523 template<
typename IsReflected=std::false_type>
525 template<
typename Stream,
typename T>
526 static inline void pack( Stream& s,
const T& v, uint32_t _max_depth ) {
530 template<
typename Stream,
typename T>
531 static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) {
538 template<
typename Stream,
typename T>
539 static inline void pack( Stream& s,
const T& v, uint32_t _max_depth ) {
543 template<
typename Stream,
typename T>
544 static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) {
552 template<
typename Stream,
typename T>
553 inline void pack( Stream& s,
const std::unordered_set<T>& value, uint32_t _max_depth ) {
557 auto itr = value.begin();
558 auto end = value.end();
559 while( itr != end ) {
564 template<
typename Stream,
typename T>
565 inline void unpack( Stream& s, std::unordered_set<T>& value, uint32_t _max_depth ) {
571 for( uint32_t i = 0; i < size.
value; ++i )
575 value.insert( std::move(tmp) );
580 template<
typename Stream,
typename K,
typename V>
581 inline void pack( Stream& s,
const std::pair<K,V>& value, uint32_t _max_depth ) {
587 template<
typename Stream,
typename K,
typename V>
588 inline void unpack( Stream& s, std::pair<K,V>& value, uint32_t _max_depth )
596 template<
typename Stream,
typename K,
typename V>
597 inline void pack( Stream& s,
const std::unordered_map<K,V>& value, uint32_t _max_depth ) {
601 auto itr = value.begin();
602 auto end = value.end();
603 while( itr != end ) {
608 template<
typename Stream,
typename K,
typename V>
609 inline void unpack( Stream& s, std::unordered_map<K,V>& value, uint32_t _max_depth )
616 for( uint32_t i = 0; i < size.
value; ++i )
620 value.insert( std::move(tmp) );
623 template<
typename Stream,
typename K,
typename... V>
624 inline void pack( Stream& s,
const std::map<K, V...>& value, uint32_t _max_depth ) {
628 auto itr = value.begin();
629 auto end = value.end();
630 while( itr != end ) {
635 template<
typename Stream,
typename K,
typename V,
typename... A>
636 inline void unpack( Stream& s, std::map<K, V, A...>& value, uint32_t _max_depth )
642 for( uint32_t i = 0; i < size.
value; ++i )
646 value.insert( std::move(tmp) );
650 template<
typename Stream,
typename T>
651 inline void pack( Stream& s,
const std::deque<T>& value, uint32_t _max_depth ) {
655 auto itr = value.begin();
656 auto end = value.end();
657 while( itr != end ) {
663 template<
typename Stream,
typename T>
664 inline void unpack( Stream& s, std::deque<T>& value, uint32_t _max_depth ) {
669 for( uint64_t i = 0; i < size; i++ )
671 if( i >= value.size() )
672 value.resize( std::min(
static_cast<uint64_t
>(2*value.size()), size.value ) );
673 unpack( s, value[i], _max_depth );
677 template<
typename Stream,
typename T>
678 inline void pack( Stream& s,
const std::vector<T>& value, uint32_t _max_depth ) {
682 auto itr = value.begin();
683 auto end = value.end();
684 while( itr != end ) {
690 template<
typename Stream,
typename T>
691 inline void unpack( Stream& s, std::vector<T>& value, uint32_t _max_depth ) {
696 for( uint64_t i = 0; i < size; i++ )
698 if( i >= value.size() )
699 value.resize( std::min(
static_cast<uint64_t
>(2*value.size()), size.value ) );
700 unpack( s, value[i], _max_depth );
704 template<
typename Stream,
typename T>
705 inline void pack( Stream& s,
const std::set<T>& value, uint32_t _max_depth ) {
709 auto itr = value.begin();
710 auto end = value.end();
711 while( itr != end ) {
717 template<
typename Stream,
typename T>
718 inline void unpack( Stream& s, std::set<T>& value, uint32_t _max_depth ) {
722 for( uint64_t i = 0; i < size.
value; ++i )
726 value.insert( std::move(tmp) );
730 template<
typename Stream, boost::endian::order O,
class T, std::
size_t N, boost::endian::align A>
731 void pack( Stream& s,
const boost::endian::endian_buffer<O,T,N,A>& v, uint32_t _max_depth )
734 s.write( (
char*)v.data(),
sizeof(v) );
736 template<
typename Stream, boost::endian::order O,
class T, std::
size_t N, boost::endian::align A>
737 void unpack( Stream& s, boost::endian::endian_buffer<O,T,N,A>& v, uint32_t _max_depth )
740 s.read( (
char*)&v,
sizeof(v) );
744 template<
typename Stream,
typename T>
745 void pack( Stream& s,
const T& v, uint32_t _max_depth ) {
749 template<
typename Stream,
typename T>
750 void unpack( Stream& s, T& v, uint32_t _max_depth )
765 inline std::vector<char>
pack(
const T& v, uint32_t _max_depth ) {
770 std::vector<char> vec(ps.
tellp());
779 template<
typename T,
typename... Next>
780 inline std::vector<char>
pack(
const T& v, Next... next, uint32_t _max_depth ) {
785 std::vector<char> vec(ps.
tellp());
796 inline T
unpack(
const std::vector<char>& s, uint32_t _max_depth )
808 inline void unpack(
const std::vector<char>& s, T& tmp, uint32_t _max_depth )
818 inline void pack(
char* d, uint32_t s,
const T& v, uint32_t _max_depth ) {
825 inline T
unpack(
const char* d, uint32_t s, uint32_t _max_depth )
835 inline void unpack(
const char* d, uint32_t s, T& v, uint32_t _max_depth )
842 template<
typename Stream>
859 template<
typename Stream>
877 template<
typename Stream,
typename... T>