Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

serialize.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
8 
9 #include <compat/endian.h>
10 
11 #include <algorithm>
12 #include <assert.h>
13 #include <ios>
14 #include <limits>
15 #include <list>
16 #include <map>
17 #include <memory>
18 #include <set>
19 #include <stdint.h>
20 #include <string>
21 #include <string.h>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <utility>
25 #include <vector>
26 
27 #include <prevector.h>
28 
29 static const unsigned int MAX_SIZE = 0x02000000;
30 
42 struct deserialize_type {};
44 
49 template<typename T>
50 inline T& REF(const T& val)
51 {
52  return const_cast<T&>(val);
53 }
54 
59 template<typename T>
60 inline T* NCONST_PTR(const T* val)
61 {
62  return const_cast<T*>(val);
63 }
64 
65 /*
66  * Lowest-level serialization and conversion.
67  * @note Sizes of these types are verified in the tests
68  */
69 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
70 {
71  s.write((char*)&obj, 1);
72 }
73 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
74 {
75  obj = htole16(obj);
76  s.write((char*)&obj, 2);
77 }
78 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
79 {
80  obj = htole32(obj);
81  s.write((char*)&obj, 4);
82 }
83 template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
84 {
85  obj = htobe32(obj);
86  s.write((char*)&obj, 4);
87 }
88 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
89 {
90  obj = htole64(obj);
91  s.write((char*)&obj, 8);
92 }
93 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
94 {
95  uint8_t obj;
96  s.read((char*)&obj, 1);
97  return obj;
98 }
99 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
100 {
101  uint16_t obj;
102  s.read((char*)&obj, 2);
103  return le16toh(obj);
104 }
105 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
106 {
107  uint32_t obj;
108  s.read((char*)&obj, 4);
109  return le32toh(obj);
110 }
111 template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
112 {
113  uint32_t obj;
114  s.read((char*)&obj, 4);
115  return be32toh(obj);
116 }
117 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
118 {
119  uint64_t obj;
120  s.read((char*)&obj, 8);
121  return le64toh(obj);
122 }
123 inline uint64_t ser_double_to_uint64(double x)
124 {
125  union { double x; uint64_t y; } tmp;
126  tmp.x = x;
127  return tmp.y;
128 }
129 inline uint32_t ser_float_to_uint32(float x)
130 {
131  union { float x; uint32_t y; } tmp;
132  tmp.x = x;
133  return tmp.y;
134 }
135 inline double ser_uint64_to_double(uint64_t y)
136 {
137  union { double x; uint64_t y; } tmp;
138  tmp.y = y;
139  return tmp.x;
140 }
141 inline float ser_uint32_to_float(uint32_t y)
142 {
143  union { float x; uint32_t y; } tmp;
144  tmp.y = y;
145  return tmp.x;
146 }
147 
148 
150 //
151 // Templates for serializing to anything that looks like a stream,
152 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
153 //
154 
155 class CSizeComputer;
156 
157 enum
158 {
159  // primary actions
160  SER_NETWORK = (1 << 0),
161  SER_DISK = (1 << 1),
162  SER_GETHASH = (1 << 2),
163 };
164 
165 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
166 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
167 
174 #define ADD_SERIALIZE_METHODS \
175  template<typename Stream> \
176  void Serialize(Stream& s) const { \
177  NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
178  } \
179  template<typename Stream> \
180  void Unserialize(Stream& s) { \
181  SerializationOp(s, CSerActionUnserialize()); \
182  }
183 
184 template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
185 template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
186 template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
187 template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
188 template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
189 template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
190 template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
191 template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
192 template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
193 template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
194 template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
195 
196 template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
197 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
198 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
199 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
200 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
201 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
202 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
203 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
204 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
205 template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
206 template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
207 
208 template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
209 template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
210 
211 template <typename T> size_t GetSerializeSize(const T& t, int nType, int nVersion = 0);
212 template <typename S, typename T> size_t GetSerializeSize(const S& s, const T& t);
213 
214 
215 
216 
217 
225 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
226 {
227  if (nSize < 253) return sizeof(unsigned char);
228  else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
229  else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
230  else return sizeof(unsigned char) + sizeof(uint64_t);
231 }
232 
233 inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
234 
235 template<typename Stream>
236 void WriteCompactSize(Stream& os, uint64_t nSize)
237 {
238  if (nSize < 253)
239  {
240  ser_writedata8(os, nSize);
241  }
242  else if (nSize <= std::numeric_limits<unsigned short>::max())
243  {
244  ser_writedata8(os, 253);
245  ser_writedata16(os, nSize);
246  }
247  else if (nSize <= std::numeric_limits<unsigned int>::max())
248  {
249  ser_writedata8(os, 254);
250  ser_writedata32(os, nSize);
251  }
252  else
253  {
254  ser_writedata8(os, 255);
255  ser_writedata64(os, nSize);
256  }
257  return;
258 }
259 
260 template<typename Stream>
261 uint64_t ReadCompactSize(Stream& is)
262 {
263  uint8_t chSize = ser_readdata8(is);
264  uint64_t nSizeRet = 0;
265  if (chSize < 253)
266  {
267  nSizeRet = chSize;
268  }
269  else if (chSize == 253)
270  {
271  nSizeRet = ser_readdata16(is);
272  if (nSizeRet < 253)
273  throw std::ios_base::failure("non-canonical ReadCompactSize()");
274  }
275  else if (chSize == 254)
276  {
277  nSizeRet = ser_readdata32(is);
278  if (nSizeRet < 0x10000u)
279  throw std::ios_base::failure("non-canonical ReadCompactSize()");
280  }
281  else
282  {
283  nSizeRet = ser_readdata64(is);
284  if (nSizeRet < 0x100000000ULL)
285  throw std::ios_base::failure("non-canonical ReadCompactSize()");
286  }
287  if (nSizeRet > (uint64_t)MAX_SIZE)
288  throw std::ios_base::failure("ReadCompactSize(): size too large");
289  return nSizeRet;
290 }
291 
316 template<typename I>
317 inline unsigned int GetSizeOfVarInt(I n)
318 {
319  int nRet = 0;
320  while(true) {
321  nRet++;
322  if (n <= 0x7F)
323  break;
324  n = (n >> 7) - 1;
325  }
326  return nRet;
327 }
328 
329 template<typename I>
330 inline void WriteVarInt(CSizeComputer& os, I n);
331 
332 template<typename Stream, typename I>
333 void WriteVarInt(Stream& os, I n)
334 {
335  unsigned char tmp[(sizeof(n)*8+6)/7];
336  int len=0;
337  while(true) {
338  tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
339  if (n <= 0x7F)
340  break;
341  n = (n >> 7) - 1;
342  len++;
343  }
344  do {
345  ser_writedata8(os, tmp[len]);
346  } while(len--);
347 }
348 
349 template<typename Stream, typename I>
350 I ReadVarInt(Stream& is)
351 {
352  I n = 0;
353  while(true) {
354  unsigned char chData = ser_readdata8(is);
355  if (n > (std::numeric_limits<I>::max() >> 7)) {
356  throw std::ios_base::failure("ReadVarInt(): size too large");
357  }
358  n = (n << 7) | (chData & 0x7F);
359  if (chData & 0x80) {
360  if (n == std::numeric_limits<I>::max()) {
361  throw std::ios_base::failure("ReadVarInt(): size too large");
362  }
363  n++;
364  } else {
365  return n;
366  }
367  }
368 }
369 
370 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
371 #define FIXEDBITSET(obj, size) REF(CFixedBitSet(REF(obj), (size)))
372 #define DYNBITSET(obj) REF(CDynamicBitSet(REF(obj)))
373 #define FIXEDVARINTSBITSET(obj, size) REF(CFixedVarIntsBitSet(REF(obj), (size)))
374 #define AUTOBITSET(obj, size) REF(CAutoBitSet(REF(obj), (size)))
375 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
376 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
377 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
378 
383 {
384 protected:
385  char* pbegin;
386  char* pend;
387 public:
388  CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
389  template <class T, class TAl>
390  explicit CFlatData(std::vector<T,TAl> &v)
391  {
392  pbegin = (char*)v.data();
393  pend = (char*)(v.data() + v.size());
394  }
395  template <unsigned int N, typename T, typename S, typename D>
397  {
398  pbegin = (char*)v.data();
399  pend = (char*)(v.data() + v.size());
400  }
401  char* begin() { return pbegin; }
402  const char* begin() const { return pbegin; }
403  char* end() { return pend; }
404  const char* end() const { return pend; }
405 
406  template<typename Stream>
407  void Serialize(Stream& s) const
408  {
409  s.write(pbegin, pend - pbegin);
410  }
411 
412  template<typename Stream>
413  void Unserialize(Stream& s)
414  {
415  s.read(pbegin, pend - pbegin);
416  }
417 };
418 
420 {
421 protected:
422  std::vector<bool>& vec;
423  size_t size;
424 
425 public:
426  CFixedBitSet(std::vector<bool>& vecIn, size_t sizeIn) : vec(vecIn), size(sizeIn) {}
427 
428  template<typename Stream>
429  void Serialize(Stream& s) const
430  {
431  std::vector<unsigned char> vBytes((size + 7) / 8);
432  size_t ms = std::min(size, vec.size());
433  for (size_t p = 0; p < ms; p++)
434  vBytes[p / 8] |= vec[p] << (p % 8);
435  s.write((char*)vBytes.data(), vBytes.size());
436  }
437 
438  template<typename Stream>
439  void Unserialize(Stream& s)
440  {
441  vec.resize(size);
442 
443  std::vector<unsigned char> vBytes((size + 7) / 8);
444  s.read((char*)vBytes.data(), vBytes.size());
445  for (size_t p = 0; p < size; p++)
446  vec[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0;
447  if (vBytes.size() * 8 != size) {
448  size_t rem = vBytes.size() * 8 - size;
449  uint8_t m = ~(uint8_t)(0xff >> rem);
450  if (vBytes[vBytes.size() - 1] & m) {
451  throw std::ios_base::failure("Out-of-range bits set");
452  }
453  }
454  }
455 };
456 
458 {
459 protected:
460  std::vector<bool>& vec;
461 
462 public:
463  explicit CDynamicBitSet(std::vector<bool>& vecIn) : vec(vecIn) {}
464 
465  template<typename Stream>
466  void Serialize(Stream& s) const
467  {
468  WriteCompactSize(s, vec.size());
469  CFixedBitSet(REF(vec), vec.size()).Serialize(s);
470  }
471 
472  template<typename Stream>
473  void Unserialize(Stream& s)
474  {
475  vec.resize(ReadCompactSize(s));
476  CFixedBitSet(vec, vec.size()).Unserialize(s);
477  }
478 };
479 
485 {
486 protected:
487  std::vector<bool>& vec;
488  size_t size;
489 
490 public:
491  CFixedVarIntsBitSet(std::vector<bool>& vecIn, size_t sizeIn) : vec(vecIn), size(sizeIn) {}
492 
493  template<typename Stream>
494  void Serialize(Stream& s) const
495  {
496  int32_t last = -1;
497  for (int32_t i = 0; i < (int32_t)vec.size(); i++) {
498  if (vec[i]) {
499  WriteVarInt<Stream, uint32_t>(s, (uint32_t)(i - last));
500  last = i;
501  }
502  }
503  WriteVarInt(s, 0); // stopper
504  }
505 
506  template<typename Stream>
507  void Unserialize(Stream& s)
508  {
509  vec.assign(size, false);
510 
511  int32_t last = -1;
512  while(true) {
513  uint32_t offset = ReadVarInt<Stream, uint32_t>(s);
514  if (offset == 0) {
515  break;
516  }
517  int32_t idx = last + offset;
518  if (idx >= size) {
519  throw std::ios_base::failure("out of bounds index");
520  }
521  if (last != -1 && idx <= last) {
522  throw std::ios_base::failure("offset overflow");
523  }
524  vec[idx] = true;
525  last = idx;
526  }
527  }
528 };
529 
534 {
535 protected:
536  std::vector<bool>& vec;
537  size_t size;
538 
539 public:
540  explicit CAutoBitSet(std::vector<bool>& vecIn, size_t sizeIn) : vec(vecIn), size(sizeIn) {}
541 
542  template<typename Stream>
543  void Serialize(Stream& s) const
544  {
545  assert(vec.size() == size);
546 
547  size_t size1 = ::GetSerializeSize(s, CFixedBitSet(vec, size));
548  size_t size2 = ::GetSerializeSize(s, CFixedVarIntsBitSet(vec, size));
549 
550  if (size1 < size2) {
551  ser_writedata8(s, 0);
552  s << FIXEDBITSET(vec, vec.size());
553  } else {
554  ser_writedata8(s, 1);
555  s << FIXEDVARINTSBITSET(vec, vec.size());
556  }
557  }
558 
559  template<typename Stream>
560  void Unserialize(Stream& s)
561  {
562  uint8_t isVarInts = ser_readdata8(s);
563  if (isVarInts != 0 && isVarInts != 1) {
564  throw std::ios_base::failure("invalid value for isVarInts byte");
565  }
566 
567  if (!isVarInts) {
568  s >> FIXEDBITSET(vec, size);
569  } else {
570  s >> FIXEDVARINTSBITSET(vec, size);
571  }
572  }
573 };
574 
575 template<typename I>
576 class CVarInt
577 {
578 protected:
579  I &n;
580 public:
581  explicit CVarInt(I& nIn) : n(nIn) { }
582 
583  template<typename Stream>
584  void Serialize(Stream &s) const {
585  WriteVarInt<Stream,I>(s, n);
586  }
587 
588  template<typename Stream>
589  void Unserialize(Stream& s) {
590  n = ReadVarInt<Stream,I>(s);
591  }
592 };
593 
595 {
596 protected:
597  uint64_t &n;
598 public:
599  explicit CCompactSize(uint64_t& nIn) : n(nIn) { }
600 
601  unsigned int GetSerializeSize() const {
602  return GetSizeOfCompactSize(n);
603  }
604 
605  template<typename Stream>
606  void Serialize(Stream &s) const {
607  WriteCompactSize<Stream>(s, n);
608  }
609 
610  template<typename Stream>
611  void Unserialize(Stream& s) {
612  n = ReadCompactSize<Stream>(s);
613  }
614 };
615 
616 template<size_t Limit>
618 {
619 protected:
620  std::string& string;
621 public:
622  explicit LimitedString(std::string& _string) : string(_string) {}
623 
624  template<typename Stream>
625  void Unserialize(Stream& s)
626  {
627  size_t size = ReadCompactSize(s);
628  if (size > Limit) {
629  throw std::ios_base::failure("String length limit exceeded");
630  }
631  string.resize(size);
632  if (size != 0)
633  s.read((char*)string.data(), size);
634  }
635 
636  template<typename Stream>
637  void Serialize(Stream& s) const
638  {
639  WriteCompactSize(s, string.size());
640  if (!string.empty())
641  s.write((char*)string.data(), string.size());
642  }
643 };
644 
645 template<typename I>
646 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
647 
655 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
656 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
657 
662 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
663 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
664 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
665 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
666 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
667 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
668 
673 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
674 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
675 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
676 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
677 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
678 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
679 
683 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
684 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
685 
689 template<typename Stream, typename... Elements> void Serialize(Stream& os, const std::tuple<Elements...>& item);
690 template<typename Stream, typename... Elements> void Unserialize(Stream& is, std::tuple<Elements...>& item);
691 
695 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
696 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
697 template<typename Stream, typename K, typename T, typename Hash, typename Pred, typename A> void Serialize(Stream& os, const std::unordered_map<K, T, Hash, Pred, A>& m);
698 template<typename Stream, typename K, typename T, typename Hash, typename Pred, typename A> void Unserialize(Stream& is, std::unordered_map<K, T, Hash, Pred, A>& m);
699 
703 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
704 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
705 template<typename Stream, typename K, typename Hash, typename Pred, typename A> void Serialize(Stream& os, const std::unordered_set<K, Hash, Pred, A>& m);
706 template<typename Stream, typename K, typename Hash, typename Pred, typename A> void Unserialize(Stream& is, std::unordered_set<K, Hash, Pred, A>& m);
707 
711 template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<T>& p);
712 template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<T>& p);
713 
717 template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
718 template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
719 
720 
721 
725 template<typename Stream, typename T, typename std::enable_if<std::is_class<T>::value>::type* = nullptr>
726 inline void Serialize(Stream& os, const T& a)
727 {
728  a.Serialize(os);
729 }
730 
731 template<typename Stream, typename T, typename std::enable_if<std::is_class<T>::value>::type* = nullptr>
732 inline void Unserialize(Stream& is, T& a)
733 {
734  a.Unserialize(is);
735 }
736 
744 template<typename T> struct is_serializable_enum;
745 template<typename T> struct is_serializable_enum : std::false_type {};
746 
747 template<typename Stream, typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
748 inline void Serialize(Stream& s, T a )
749 {
750  // If you ever get into this situation, it usaully means you forgot to declare is_serializable_enum for the desired enum type
751  static_assert(is_serializable_enum<T>::value, "Missing declararion of is_serializable_enum");
752 
753  typedef typename std::underlying_type<T>::type T2;
754  T2 b = (T2)a;
755  Serialize(s, b);
756 }
757 
758 template<typename Stream, typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
759 inline void Unserialize(Stream& s, T& a )
760 {
761  // If you ever get into this situation, it usaully means you forgot to declare is_serializable_enum for the desired enum type
762  static_assert(is_serializable_enum<T>::value, "Missing declararion of is_serializable_enum");
763 
764  typedef typename std::underlying_type<T>::type T2;
765  T2 b;
766  Unserialize(s, b);
767  a = (T)b;
768 }
769 
770 
771 
772 
776 template<typename Stream, typename C>
777 void Serialize(Stream& os, const std::basic_string<C>& str)
778 {
779  WriteCompactSize(os, str.size());
780  if (!str.empty())
781  os.write((char*)str.data(), str.size() * sizeof(C));
782 }
783 
784 template<typename Stream, typename C>
785 void Unserialize(Stream& is, std::basic_string<C>& str)
786 {
787  unsigned int nSize = ReadCompactSize(is);
788  str.resize(nSize);
789  if (nSize != 0)
790  is.read((char*)str.data(), nSize * sizeof(C));
791 }
792 
793 
794 
798 template<typename Stream, unsigned int N, typename T>
799 void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
800 {
801  WriteCompactSize(os, v.size());
802  if (!v.empty())
803  os.write((char*)v.data(), v.size() * sizeof(T));
804 }
805 
806 template<typename Stream, unsigned int N, typename T, typename V>
807 void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
808 {
809  WriteCompactSize(os, v.size());
810  for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
811  ::Serialize(os, (*vi));
812 }
813 
814 template<typename Stream, unsigned int N, typename T>
815 inline void Serialize(Stream& os, const prevector<N, T>& v)
816 {
817  Serialize_impl(os, v, T());
818 }
819 
820 
821 template<typename Stream, unsigned int N, typename T>
822 void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
823 {
824  // Limit size per read so bogus size value won't cause out of memory
825  v.clear();
826  unsigned int nSize = ReadCompactSize(is);
827  unsigned int i = 0;
828  while (i < nSize)
829  {
830  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
831  v.resize_uninitialized(i + blk);
832  is.read((char*)&v[i], blk * sizeof(T));
833  i += blk;
834  }
835 }
836 
837 template<typename Stream, unsigned int N, typename T, typename V>
838 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
839 {
840  v.clear();
841  unsigned int nSize = ReadCompactSize(is);
842  unsigned int i = 0;
843  unsigned int nMid = 0;
844  while (nMid < nSize)
845  {
846  nMid += 5000000 / sizeof(T);
847  if (nMid > nSize)
848  nMid = nSize;
849  v.resize_uninitialized(nMid);
850  for (; i < nMid; ++i)
851  Unserialize(is, v[i]);
852  }
853 }
854 
855 template<typename Stream, unsigned int N, typename T>
856 inline void Unserialize(Stream& is, prevector<N, T>& v)
857 {
858  Unserialize_impl(is, v, T());
859 }
860 
861 
862 
866 template<typename Stream, typename T, typename A>
867 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
868 {
869  WriteCompactSize(os, v.size());
870  if (!v.empty())
871  os.write((char*)v.data(), v.size() * sizeof(T));
872 }
873 
874 template<typename Stream, typename T, typename A, typename V>
875 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
876 {
877  WriteCompactSize(os, v.size());
878  for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
879  ::Serialize(os, (*vi));
880 }
881 
882 template<typename Stream, typename T, typename A>
883 inline void Serialize(Stream& os, const std::vector<T, A>& v)
884 {
885  Serialize_impl(os, v, T());
886 }
887 
888 
889 template<typename Stream, typename T, typename A>
890 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
891 {
892  // Limit size per read so bogus size value won't cause out of memory
893  v.clear();
894  unsigned int nSize = ReadCompactSize(is);
895  unsigned int i = 0;
896  while (i < nSize)
897  {
898  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
899  v.resize(i + blk);
900  is.read((char*)&v[i], blk * sizeof(T));
901  i += blk;
902  }
903 }
904 
905 template<typename Stream, typename T, typename A, typename V>
906 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
907 {
908  v.clear();
909  unsigned int nSize = ReadCompactSize(is);
910  unsigned int i = 0;
911  unsigned int nMid = 0;
912  while (nMid < nSize)
913  {
914  nMid += 5000000 / sizeof(T);
915  if (nMid > nSize)
916  nMid = nSize;
917  v.resize(nMid);
918  for (; i < nMid; i++)
919  Unserialize(is, v[i]);
920  }
921 }
922 
923 template<typename Stream, typename T, typename A>
924 inline void Unserialize(Stream& is, std::vector<T, A>& v)
925 {
926  Unserialize_impl(is, v, T());
927 }
928 
929 
930 
934 template<typename Stream, typename K, typename T>
935 void Serialize(Stream& os, const std::pair<K, T>& item)
936 {
937  Serialize(os, item.first);
938  Serialize(os, item.second);
939 }
940 
941 template<typename Stream, typename K, typename T>
942 void Unserialize(Stream& is, std::pair<K, T>& item)
943 {
944  Unserialize(is, item.first);
945  Unserialize(is, item.second);
946 }
947 
951 template<typename Stream, int index, typename... Ts>
953  void operator() (Stream&s, std::tuple<Ts...>& t) {
954  SerializeTuple<Stream, index - 1, Ts...>{}(s, t);
955  s << std::get<index>(t);
956  }
957 };
958 
959 template<typename Stream, typename... Ts>
960 struct SerializeTuple<Stream, 0, Ts...> {
961  void operator() (Stream&s, std::tuple<Ts...>& t) {
962  s << std::get<0>(t);
963  }
964 };
965 
966 template<typename Stream, int index, typename... Ts>
968  void operator() (Stream&s, std::tuple<Ts...>& t) {
969  DeserializeTuple<Stream, index - 1, Ts...>{}(s, t);
970  s >> std::get<index>(t);
971  }
972 };
973 
974 template<typename Stream, typename... Ts>
975 struct DeserializeTuple<Stream, 0, Ts...> {
976  void operator() (Stream&s, std::tuple<Ts...>& t) {
977  s >> std::get<0>(t);
978  }
979 };
980 
981 
982 template<typename Stream, typename... Elements>
983 void Serialize(Stream& os, const std::tuple<Elements...>& item)
984 {
985  const auto size = std::tuple_size<std::tuple<Elements...>>::value;
986  SerializeTuple<Stream, size - 1, Elements...>{}(os, const_cast<std::tuple<Elements...>&>(item));
987 }
988 
989 template<typename Stream, typename... Elements>
990 void Unserialize(Stream& is, std::tuple<Elements...>& item)
991 {
992  const auto size = std::tuple_size<std::tuple<Elements...>>::value;
993  DeserializeTuple<Stream, size - 1, Elements...>{}(is, item);
994 }
995 
996 
1000 template<typename Stream, typename Map>
1001 void SerializeMap(Stream& os, const Map& m)
1002 {
1003  WriteCompactSize(os, m.size());
1004  for (const auto& entry : m)
1005  Serialize(os, entry);
1006 }
1007 
1008 template<typename Stream, typename Map>
1009 void UnserializeMap(Stream& is, Map& m)
1010 {
1011  m.clear();
1012  unsigned int nSize = ReadCompactSize(is);
1013  auto mi = m.begin();
1014  for (unsigned int i = 0; i < nSize; i++)
1015  {
1016  std::pair<typename std::remove_const<typename Map::key_type>::type, typename std::remove_const<typename Map::mapped_type>::type> item;
1017  Unserialize(is, item);
1018  mi = m.insert(mi, item);
1019  }
1020 }
1021 
1022 template<typename Stream, typename K, typename T, typename Pred, typename A>
1023 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
1024 {
1025  SerializeMap(os, m);
1026 }
1027 
1028 template<typename Stream, typename K, typename T, typename Pred, typename A>
1029 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
1030 {
1031  UnserializeMap(is, m);
1032 }
1033 
1034 template<typename Stream, typename K, typename T, typename Hash, typename Pred, typename A>
1035 void Serialize(Stream& os, const std::unordered_map<K, T, Hash, Pred, A>& m)
1036 {
1037  SerializeMap(os, m);
1038 }
1039 
1040 template<typename Stream, typename K, typename T, typename Hash, typename Pred, typename A>
1041 void Unserialize(Stream& is, std::unordered_map<K, T, Hash, Pred, A>& m)
1042 {
1043  UnserializeMap(is, m);
1044 }
1045 
1050 template<typename Stream, typename Set>
1051 void SerializeSet(Stream& os, const Set& m)
1052 {
1053  WriteCompactSize(os, m.size());
1054  for (auto it = m.begin(); it != m.end(); ++it)
1055  Serialize(os, (*it));
1056 }
1057 
1058 template<typename Stream, typename Set>
1059 void UnserializeSet(Stream& is, Set& m)
1060 {
1061  m.clear();
1062  unsigned int nSize = ReadCompactSize(is);
1063  auto it = m.begin();
1064  for (unsigned int i = 0; i < nSize; i++)
1065  {
1066  typename std::remove_const<typename Set::key_type>::type key;
1067  Unserialize(is, key);
1068  it = m.insert(it, key);
1069  }
1070 }
1071 
1072 template<typename Stream, typename K, typename Pred, typename A>
1073 void Serialize(Stream& os, const std::set<K, Pred, A>& m)
1074 {
1075  SerializeSet(os, m);
1076 }
1077 
1078 template<typename Stream, typename K, typename Pred, typename A>
1079 void Unserialize(Stream& is, std::set<K, Pred, A>& m)
1080 {
1081  UnserializeSet(is, m);
1082 }
1083 
1084 template<typename Stream, typename K, typename Hash, typename Pred, typename A>
1085 void Serialize(Stream& os, const std::unordered_set<K, Hash, Pred, A>& m)
1086 {
1087  SerializeSet(os, m);
1088 }
1089 
1090 template<typename Stream, typename K, typename Hash, typename Pred, typename A>
1091 void Unserialize(Stream& is, std::unordered_set<K, Hash, Pred, A>& m)
1092 {
1093  UnserializeSet(is, m);
1094 }
1095 
1099 template<typename Stream, typename T, typename A>
1100 void Serialize(Stream& os, const std::list<T, A>& l)
1101 {
1102  WriteCompactSize(os, l.size());
1103  for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
1104  Serialize(os, (*it));
1105 }
1106 
1107 template<typename Stream, typename T, typename A>
1108 void Unserialize(Stream& is, std::list<T, A>& l)
1109 {
1110  l.clear();
1111  unsigned int nSize = ReadCompactSize(is);
1112  for (unsigned int i = 0; i < nSize; i++)
1113  {
1114  T val;
1115  Unserialize(is, val);
1116  l.push_back(val);
1117  }
1118 }
1119 
1120 
1121 
1125 template<typename Stream, typename T> void
1126 Serialize(Stream& os, const std::unique_ptr<const T>& p)
1127 {
1128  Serialize(os, *p);
1129 }
1130 
1131 template<typename Stream, typename T>
1132 void Unserialize(Stream& is, std::unique_ptr<const T>& p)
1133 {
1134  p.reset(new T(deserialize, is));
1135 }
1136 
1137 
1138 
1142 template<typename Stream, typename T> void
1143 Serialize(Stream& os, const std::shared_ptr<T>& p)
1144 {
1145  Serialize(os, *p);
1146 }
1147 
1148 template<typename Stream, typename T>
1149 void Unserialize(Stream& is, std::shared_ptr<T>& p)
1150 {
1151  p = std::make_shared<T>(deserialize, is);
1152 }
1153 
1154 
1155 
1160 {
1161  constexpr bool ForRead() const { return false; }
1162 };
1164 {
1165  constexpr bool ForRead() const { return true; }
1166 };
1167 
1168 template<typename Stream, typename T>
1169 inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
1170 {
1171  ::Serialize(s, obj);
1172 }
1173 
1174 template<typename Stream, typename T>
1175 inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
1176 {
1177  ::Unserialize(s, obj);
1178 }
1179 
1180 
1181 
1182 
1183 
1184 
1185 
1186 
1187 
1188 /* ::GetSerializeSize implementations
1189  *
1190  * Computing the serialized size of objects is done through a special stream
1191  * object of type CSizeComputer, which only records the number of bytes written
1192  * to it.
1193  *
1194  * If your Serialize or SerializationOp method has non-trivial overhead for
1195  * serialization, it may be worthwhile to implement a specialized version for
1196  * CSizeComputer, which uses the s.seek() method to record bytes that would
1197  * be written instead.
1198  */
1200 {
1201 protected:
1202  size_t nSize;
1203 
1204  const int nType;
1205  const int nVersion;
1206 public:
1207  CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
1208 
1209  void write(const char *psz, size_t _nSize)
1210  {
1211  this->nSize += _nSize;
1212  }
1213 
1215  void seek(size_t _nSize)
1216  {
1217  this->nSize += _nSize;
1218  }
1219 
1220  template<typename T>
1221  CSizeComputer& operator<<(const T& obj)
1222  {
1223  ::Serialize(*this, obj);
1224  return (*this);
1225  }
1226 
1227  size_t size() const {
1228  return nSize;
1229  }
1230 
1231  int GetVersion() const { return nVersion; }
1232  int GetType() const { return nType; }
1233 };
1234 
1235 template<typename Stream>
1236 void SerializeMany(Stream& s)
1237 {
1238 }
1239 
1240 template<typename Stream, typename Arg>
1241 void SerializeMany(Stream& s, Arg&& arg)
1242 {
1243  ::Serialize(s, std::forward<Arg>(arg));
1244 }
1245 
1246 template<typename Stream, typename Arg, typename... Args>
1247 void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
1248 {
1249  ::Serialize(s, std::forward<Arg>(arg));
1250  ::SerializeMany(s, std::forward<Args>(args)...);
1251 }
1252 
1253 template<typename Stream>
1254 inline void UnserializeMany(Stream& s)
1255 {
1256 }
1257 
1258 template<typename Stream, typename Arg>
1259 inline void UnserializeMany(Stream& s, Arg& arg)
1260 {
1261  ::Unserialize(s, arg);
1262 }
1263 
1264 template<typename Stream, typename Arg, typename... Args>
1265 inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
1266 {
1267  ::Unserialize(s, arg);
1268  ::UnserializeMany(s, args...);
1269 }
1270 
1271 template<typename Stream, typename... Args>
1272 inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
1273 {
1274  ::SerializeMany(s, std::forward<Args>(args)...);
1275 }
1276 
1277 template<typename Stream, typename... Args>
1278 inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
1279 {
1280  ::UnserializeMany(s, args...);
1281 }
1282 
1283 template<typename I>
1284 inline void WriteVarInt(CSizeComputer &s, I n)
1285 {
1286  s.seek(GetSizeOfVarInt<I>(n));
1287 }
1288 
1289 inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
1290 {
1291  s.seek(GetSizeOfCompactSize(nSize));
1292 }
1293 
1294 template <typename T>
1295 size_t GetSerializeSize(const T& t, int nType, int nVersion)
1296 {
1297  return (CSizeComputer(nType, nVersion) << t).size();
1298 }
1299 
1300 template <typename S, typename T>
1301 size_t GetSerializeSize(const S& s, const T& t)
1302 {
1303  return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
1304 }
1305 
1306 #endif // BITCOIN_SERIALIZE_H
void Serialize(Stream &s) const
Definition: serialize.h:494
uint64_t ser_double_to_uint64(double x)
Definition: serialize.h:123
std::string & string
Definition: serialize.h:620
static const sph_u32 C[]
Definition: jh.c:370
void Unserialize(Stream &s)
Definition: serialize.h:473
void operator()(Stream &s, std::tuple< Ts... > &t)
Definition: serialize.h:953
uint32_t ser_float_to_uint32(float x)
Definition: serialize.h:129
size_t size
Definition: serialize.h:423
I & n
Definition: serialize.h:579
void Serialize(Stream &s) const
Definition: serialize.h:407
CFixedBitSet(std::vector< bool > &vecIn, size_t sizeIn)
Definition: serialize.h:426
uint8_t ser_readdata8(Stream &s)
Definition: serialize.h:93
void Serialize(Stream &s) const
Definition: serialize.h:466
void clear()
Definition: prevector.h:371
void Unserialize(Stream &s)
Definition: serialize.h:439
If none of the specialized versions above matched and T is an enum, default to calling Serialize/Unse...
Definition: params.h:197
void ser_writedata64(Stream &s, uint64_t obj)
Definition: serialize.h:88
uint64_t & n
Definition: serialize.h:597
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:261
void WriteVarInt(CSizeComputer &os, I n)
Definition: serialize.h:1284
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1289
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, Args &&... args)
Definition: serialize.h:1272
#define FIXEDBITSET(obj, size)
Definition: serialize.h:371
std::vector< bool > & vec
Definition: serialize.h:422
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:1295
size_t size() const
Definition: serialize.h:1227
void ser_writedata32(Stream &s, uint32_t obj)
Definition: serialize.h:78
constexpr deserialize_type deserialize
Definition: serialize.h:43
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 ...
Definition: serialize.h:225
CCompactSize(uint64_t &nIn)
Definition: serialize.h:599
CSizeComputer(int nTypeIn, int nVersionIn)
Definition: serialize.h:1207
char * end()
Definition: serialize.h:403
void Unserialize(Stream &s)
Definition: serialize.h:611
std::vector< bool > & vec
Definition: serialize.h:460
uint32_t be32toh(uint32_t big_endian_32bits)
Definition: endian.h:198
std::vector< bool > & vec
Definition: serialize.h:536
const char * end() const
Definition: serialize.h:404
void resize_uninitialized(size_type new_size)
Definition: prevector.h:414
char * begin()
Definition: serialize.h:401
const int nVersion
Definition: serialize.h:1205
void UnserializeMany(Stream &s)
Definition: serialize.h:1254
uint32_t htole32(uint32_t host_32bits)
Definition: endian.h:191
Dummy data type to identify deserializing constructors.
Definition: serialize.h:42
void Serialize(Stream &s) const
Definition: serialize.h:584
value_type * data()
Definition: prevector.h:551
void Serialize(Stream &s, char a)
Definition: serialize.h:184
const impl< T, B, MP > empty
void Serialize(Stream &s) const
Definition: serialize.h:543
uint32_t htobe32(uint32_t host_32bits)
Definition: endian.h:184
void Unserialize(Stream &s)
Definition: serialize.h:589
void UnserializeSet(Stream &is, Set &m)
Definition: serialize.h:1059
I ReadVarInt(Stream &is)
Definition: serialize.h:350
void Unserialize(Stream &s)
Definition: serialize.h:625
CSizeComputer & operator<<(const T &obj)
Definition: serialize.h:1221
iterator end()
Definition: prevector.h:320
void Serialize(Stream &s) const
Definition: serialize.h:637
uint16_t ser_readdata16(Stream &s)
Definition: serialize.h:99
Serializes either as a CFixedBitSet or CFixedVarIntsBitSet, depending on which would give a smaller s...
Definition: serialize.h:533
uint32_t ser_readdata32(Stream &s)
Definition: serialize.h:105
void Serialize(Stream &s) const
Definition: serialize.h:606
const int nType
Definition: serialize.h:1204
void ser_writedata16(Stream &s, uint16_t obj)
Definition: serialize.h:73
void SerializeSet(Stream &os, const Set &m)
set
Definition: serialize.h:1051
void SerReadWrite(Stream &s, const T &obj, CSerActionSerialize ser_action)
Definition: serialize.h:1169
void Unserialize(Stream &s)
Definition: serialize.h:413
void Serialize(Stream &s) const
Definition: serialize.h:429
#define S(x0, x1, x2, x3, cb, r)
Definition: jh.c:494
CVarInt(I &nIn)
Definition: serialize.h:581
constexpr bool ForRead() const
Definition: serialize.h:1165
char * pend
Definition: serialize.h:386
int GetType() const
Definition: serialize.h:1232
uint16_t le16toh(uint16_t little_endian_16bits)
Definition: endian.h:177
void ser_writedata32be(Stream &s, uint32_t obj)
Definition: serialize.h:83
double ser_uint64_to_double(uint64_t y)
Definition: serialize.h:135
unsigned int GetSizeOfVarInt(I n)
Variable-length integers: bytes are a MSB base-128 encoding of the number.
Definition: serialize.h:317
Support for ADD_SERIALIZE_METHODS and READWRITE macro.
Definition: serialize.h:1159
constexpr bool ForRead() const
Definition: serialize.h:1161
void SerializeMany(Stream &s)
Definition: serialize.h:1236
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:39
uint64_t ser_readdata64(Stream &s)
Definition: serialize.h:117
const char * begin() const
Definition: serialize.h:402
CFixedVarIntsBitSet(std::vector< bool > &vecIn, size_t sizeIn)
Definition: serialize.h:491
CFlatData(void *pbeginIn, void *pendIn)
Definition: serialize.h:388
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:1215
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:163
void Unserialize(Stream &s)
Definition: serialize.h:560
void SerializeMap(Stream &os, const Map &m)
map
Definition: serialize.h:1001
void UnserializeMap(Stream &is, Map &m)
Definition: serialize.h:1009
static const unsigned int MAX_SIZE
Definition: serialize.h:29
uint64_t le64toh(uint64_t little_endian_64bits)
Definition: endian.h:233
Stores a fixed size bitset as a series of VarInts.
Definition: serialize.h:484
#define FIXEDVARINTSBITSET(obj, size)
Definition: serialize.h:373
int GetVersion() const
Definition: serialize.h:1231
CAutoBitSet(std::vector< bool > &vecIn, size_t sizeIn)
Definition: serialize.h:540
CFlatData(prevector< N, T, S, D > &v)
Definition: serialize.h:396
bool empty() const
Definition: prevector.h:314
void operator()(Stream &s, std::tuple< Ts... > &t)
Definition: serialize.h:968
void Unserialize(Stream &s)
Definition: serialize.h:507
uint64_t htole64(uint64_t host_64bits)
Definition: endian.h:219
void write(const char *psz, size_t _nSize)
Definition: serialize.h:1209
size_t size
Definition: serialize.h:537
uint32_t ser_readdata32be(Stream &s)
Definition: serialize.h:111
void Unserialize(Stream &s, char &a)
Definition: serialize.h:196
iterator begin()
Definition: prevector.h:318
size_type size() const
Definition: prevector.h:310
char * pbegin
Definition: serialize.h:385
CFlatData(std::vector< T, TAl > &v)
Definition: serialize.h:390
void Serialize_impl(Stream &os, const prevector< N, T > &v, const unsigned char &)
prevector prevectors of unsigned char are a special case and are intended to be serialized as a singl...
Definition: serialize.h:799
T * NCONST_PTR(const T *val)
Used to acquire a non-const pointer "this" to generate bodies of const serialization operations from ...
Definition: serialize.h:60
CDynamicBitSet(std::vector< bool > &vecIn)
Definition: serialize.h:463
void Unserialize_impl(Stream &is, prevector< N, T > &v, const unsigned char &)
Definition: serialize.h:822
uint32_t le32toh(uint32_t little_endian_32bits)
Definition: endian.h:205
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
Definition: serialize.h:50
std::vector< bool > & vec
Definition: serialize.h:487
size_t nSize
Definition: serialize.h:1202
LimitedString(std::string &_string)
Definition: serialize.h:622
CVarInt< I > WrapVarInt(I &n)
Definition: serialize.h:646
void ser_writedata8(Stream &s, uint8_t obj)
Definition: serialize.h:69
Wrapper for serializing arrays and POD.
Definition: serialize.h:382
unsigned int GetSerializeSize() const
Definition: serialize.h:601
float ser_uint32_to_float(uint32_t y)
Definition: serialize.h:141
Released under the MIT license