Make GetSerializeSize a wrapper on top of CSizeComputer
[bitcoinplatinum.git] / src / serialize.h
blob80289a365ae6fa291a2de76c0825534ca56d2dcb
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.
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
9 #include "compat/endian.h"
11 #include <algorithm>
12 #include <assert.h>
13 #include <ios>
14 #include <limits>
15 #include <map>
16 #include <set>
17 #include <stdint.h>
18 #include <string>
19 #include <string.h>
20 #include <utility>
21 #include <vector>
23 #include "prevector.h"
25 static const unsigned int MAX_SIZE = 0x02000000;
27 /**
28 * Used to bypass the rule against non-const reference to temporary
29 * where it makes sense with wrappers such as CFlatData or CTxDB
31 template<typename T>
32 inline T& REF(const T& val)
34 return const_cast<T&>(val);
37 /**
38 * Used to acquire a non-const pointer "this" to generate bodies
39 * of const serialization operations from a template
41 template<typename T>
42 inline T* NCONST_PTR(const T* val)
44 return const_cast<T*>(val);
47 /**
48 * Important: Do not use the following functions in new code, but use v.data()
49 * and v.data() + v.size() respectively directly. They were once introduced to
50 * have a compatible, safe way to get the begin and end pointer of a vector.
51 * However with C++11 the language has built-in functionality for this and it's
52 * more readable to just use that.
54 template <typename V>
55 inline typename V::value_type* begin_ptr(V& v)
57 return v.data();
59 template <typename V>
60 inline const typename V::value_type* begin_ptr(const V& v)
62 return v.data();
64 template <typename V>
65 inline typename V::value_type* end_ptr(V& v)
67 return v.data() + v.size();
69 template <typename V>
70 inline const typename V::value_type* end_ptr(const V& v)
72 return v.data() + v.size();
76 * Lowest-level serialization and conversion.
77 * @note Sizes of these types are verified in the tests
79 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
81 s.write((char*)&obj, 1);
83 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
85 obj = htole16(obj);
86 s.write((char*)&obj, 2);
88 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
90 obj = htole32(obj);
91 s.write((char*)&obj, 4);
93 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
95 obj = htole64(obj);
96 s.write((char*)&obj, 8);
98 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
100 uint8_t obj;
101 s.read((char*)&obj, 1);
102 return obj;
104 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
106 uint16_t obj;
107 s.read((char*)&obj, 2);
108 return le16toh(obj);
110 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
112 uint32_t obj;
113 s.read((char*)&obj, 4);
114 return le32toh(obj);
116 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
118 uint64_t obj;
119 s.read((char*)&obj, 8);
120 return le64toh(obj);
122 inline uint64_t ser_double_to_uint64(double x)
124 union { double x; uint64_t y; } tmp;
125 tmp.x = x;
126 return tmp.y;
128 inline uint32_t ser_float_to_uint32(float x)
130 union { float x; uint32_t y; } tmp;
131 tmp.x = x;
132 return tmp.y;
134 inline double ser_uint64_to_double(uint64_t y)
136 union { double x; uint64_t y; } tmp;
137 tmp.y = y;
138 return tmp.x;
140 inline float ser_uint32_to_float(uint32_t y)
142 union { float x; uint32_t y; } tmp;
143 tmp.y = y;
144 return tmp.x;
148 /////////////////////////////////////////////////////////////////
150 // Templates for serializing to anything that looks like a stream,
151 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
154 enum
156 // primary actions
157 SER_NETWORK = (1 << 0),
158 SER_DISK = (1 << 1),
159 SER_GETHASH = (1 << 2),
162 #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
163 #define READWRITEMANY(...) (::SerReadWriteMany(s, nType, nVersion, ser_action, __VA_ARGS__))
165 /**
166 * Implement three methods for serializable objects. These are actually wrappers over
167 * "SerializationOp" template, which implements the body of each class' serialization
168 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
169 * added as members.
171 #define ADD_SERIALIZE_METHODS \
172 template<typename Stream> \
173 void Serialize(Stream& s, int nType, int nVersion) const { \
174 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
176 template<typename Stream> \
177 void Unserialize(Stream& s, int nType, int nVersion) { \
178 SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
182 * Basic Types
184 template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
185 template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
186 template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
187 template<typename Stream> inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); }
188 template<typename Stream> inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); }
189 template<typename Stream> inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); }
190 template<typename Stream> inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); }
191 template<typename Stream> inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); }
192 template<typename Stream> inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); }
193 template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
194 template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
196 template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
197 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); }
198 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); }
199 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); }
200 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); }
201 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); }
202 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); }
203 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); }
204 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); }
205 template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
206 template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
208 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
209 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
217 * Compact Size
218 * size < 253 -- 1 byte
219 * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
220 * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
221 * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
223 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
225 if (nSize < 253) return sizeof(unsigned char);
226 else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
227 else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
228 else return sizeof(unsigned char) + sizeof(uint64_t);
231 template<typename Stream>
232 void WriteCompactSize(Stream& os, uint64_t nSize)
234 if (nSize < 253)
236 ser_writedata8(os, nSize);
238 else if (nSize <= std::numeric_limits<unsigned short>::max())
240 ser_writedata8(os, 253);
241 ser_writedata16(os, nSize);
243 else if (nSize <= std::numeric_limits<unsigned int>::max())
245 ser_writedata8(os, 254);
246 ser_writedata32(os, nSize);
248 else
250 ser_writedata8(os, 255);
251 ser_writedata64(os, nSize);
253 return;
256 template<typename Stream>
257 uint64_t ReadCompactSize(Stream& is)
259 uint8_t chSize = ser_readdata8(is);
260 uint64_t nSizeRet = 0;
261 if (chSize < 253)
263 nSizeRet = chSize;
265 else if (chSize == 253)
267 nSizeRet = ser_readdata16(is);
268 if (nSizeRet < 253)
269 throw std::ios_base::failure("non-canonical ReadCompactSize()");
271 else if (chSize == 254)
273 nSizeRet = ser_readdata32(is);
274 if (nSizeRet < 0x10000u)
275 throw std::ios_base::failure("non-canonical ReadCompactSize()");
277 else
279 nSizeRet = ser_readdata64(is);
280 if (nSizeRet < 0x100000000ULL)
281 throw std::ios_base::failure("non-canonical ReadCompactSize()");
283 if (nSizeRet > (uint64_t)MAX_SIZE)
284 throw std::ios_base::failure("ReadCompactSize(): size too large");
285 return nSizeRet;
289 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
290 * The high bit in each byte signifies whether another digit follows. To make
291 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
292 * Thus, the byte sequence a[] with length len, where all but the last byte
293 * has bit 128 set, encodes the number:
295 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
297 * Properties:
298 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
299 * * Every integer has exactly one encoding
300 * * Encoding does not depend on size of original integer type
301 * * No redundancy: every (infinite) byte sequence corresponds to a list
302 * of encoded integers.
304 * 0: [0x00] 256: [0x81 0x00]
305 * 1: [0x01] 16383: [0xFE 0x7F]
306 * 127: [0x7F] 16384: [0xFF 0x00]
307 * 128: [0x80 0x00] 16511: [0xFF 0x7F]
308 * 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F]
309 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
312 template<typename I>
313 inline unsigned int GetSizeOfVarInt(I n)
315 int nRet = 0;
316 while(true) {
317 nRet++;
318 if (n <= 0x7F)
319 break;
320 n = (n >> 7) - 1;
322 return nRet;
325 template<typename Stream, typename I>
326 void WriteVarInt(Stream& os, I n)
328 unsigned char tmp[(sizeof(n)*8+6)/7];
329 int len=0;
330 while(true) {
331 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
332 if (n <= 0x7F)
333 break;
334 n = (n >> 7) - 1;
335 len++;
337 do {
338 ser_writedata8(os, tmp[len]);
339 } while(len--);
342 template<typename Stream, typename I>
343 I ReadVarInt(Stream& is)
345 I n = 0;
346 while(true) {
347 unsigned char chData = ser_readdata8(is);
348 n = (n << 7) | (chData & 0x7F);
349 if (chData & 0x80)
350 n++;
351 else
352 return n;
356 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
357 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
358 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
359 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
361 /**
362 * Wrapper for serializing arrays and POD.
364 class CFlatData
366 protected:
367 char* pbegin;
368 char* pend;
369 public:
370 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
371 template <class T, class TAl>
372 explicit CFlatData(std::vector<T,TAl> &v)
374 pbegin = (char*)begin_ptr(v);
375 pend = (char*)end_ptr(v);
377 template <unsigned int N, typename T, typename S, typename D>
378 explicit CFlatData(prevector<N, T, S, D> &v)
380 pbegin = (char*)begin_ptr(v);
381 pend = (char*)end_ptr(v);
383 char* begin() { return pbegin; }
384 const char* begin() const { return pbegin; }
385 char* end() { return pend; }
386 const char* end() const { return pend; }
388 template<typename Stream>
389 void Serialize(Stream& s, int, int=0) const
391 s.write(pbegin, pend - pbegin);
394 template<typename Stream>
395 void Unserialize(Stream& s, int, int=0)
397 s.read(pbegin, pend - pbegin);
401 template<typename I>
402 class CVarInt
404 protected:
405 I &n;
406 public:
407 CVarInt(I& nIn) : n(nIn) { }
409 template<typename Stream>
410 void Serialize(Stream &s, int, int) const {
411 WriteVarInt<Stream,I>(s, n);
414 template<typename Stream>
415 void Unserialize(Stream& s, int, int) {
416 n = ReadVarInt<Stream,I>(s);
420 class CCompactSize
422 protected:
423 uint64_t &n;
424 public:
425 CCompactSize(uint64_t& nIn) : n(nIn) { }
427 template<typename Stream>
428 void Serialize(Stream &s, int, int) const {
429 WriteCompactSize<Stream>(s, n);
432 template<typename Stream>
433 void Unserialize(Stream& s, int, int) {
434 n = ReadCompactSize<Stream>(s);
438 template<size_t Limit>
439 class LimitedString
441 protected:
442 std::string& string;
443 public:
444 LimitedString(std::string& string) : string(string) {}
446 template<typename Stream>
447 void Unserialize(Stream& s, int, int=0)
449 size_t size = ReadCompactSize(s);
450 if (size > Limit) {
451 throw std::ios_base::failure("String length limit exceeded");
453 string.resize(size);
454 if (size != 0)
455 s.read((char*)&string[0], size);
458 template<typename Stream>
459 void Serialize(Stream& s, int, int=0) const
461 WriteCompactSize(s, string.size());
462 if (!string.empty())
463 s.write((char*)&string[0], string.size());
467 template<typename I>
468 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
471 * Forward declarations
475 * string
477 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
478 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
481 * prevector
482 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
484 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
485 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&);
486 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion);
487 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
488 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&);
489 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion);
492 * vector
493 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
495 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
496 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&);
497 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
498 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
499 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&);
500 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
503 * pair
505 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
506 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
509 * map
511 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion);
512 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion);
515 * set
517 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
518 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
525 * If none of the specialized versions above matched, default to calling member function.
526 * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
527 * The compiler will only cast int to long if none of the other templates matched.
528 * Thanks to Boost serialization for this idea.
530 template<typename Stream, typename T>
531 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
533 a.Serialize(os, (int)nType, nVersion);
536 template<typename Stream, typename T>
537 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
539 a.Unserialize(is, (int)nType, nVersion);
547 * string
549 template<typename Stream, typename C>
550 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
552 WriteCompactSize(os, str.size());
553 if (!str.empty())
554 os.write((char*)&str[0], str.size() * sizeof(str[0]));
557 template<typename Stream, typename C>
558 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
560 unsigned int nSize = ReadCompactSize(is);
561 str.resize(nSize);
562 if (nSize != 0)
563 is.read((char*)&str[0], nSize * sizeof(str[0]));
569 * prevector
571 template<typename Stream, unsigned int N, typename T>
572 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
574 WriteCompactSize(os, v.size());
575 if (!v.empty())
576 os.write((char*)&v[0], v.size() * sizeof(T));
579 template<typename Stream, unsigned int N, typename T, typename V>
580 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&)
582 WriteCompactSize(os, v.size());
583 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
584 ::Serialize(os, (*vi), nType, nVersion);
587 template<typename Stream, unsigned int N, typename T>
588 inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion)
590 Serialize_impl(os, v, nType, nVersion, T());
594 template<typename Stream, unsigned int N, typename T>
595 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
597 // Limit size per read so bogus size value won't cause out of memory
598 v.clear();
599 unsigned int nSize = ReadCompactSize(is);
600 unsigned int i = 0;
601 while (i < nSize)
603 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
604 v.resize(i + blk);
605 is.read((char*)&v[i], blk * sizeof(T));
606 i += blk;
610 template<typename Stream, unsigned int N, typename T, typename V>
611 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&)
613 v.clear();
614 unsigned int nSize = ReadCompactSize(is);
615 unsigned int i = 0;
616 unsigned int nMid = 0;
617 while (nMid < nSize)
619 nMid += 5000000 / sizeof(T);
620 if (nMid > nSize)
621 nMid = nSize;
622 v.resize(nMid);
623 for (; i < nMid; i++)
624 Unserialize(is, v[i], nType, nVersion);
628 template<typename Stream, unsigned int N, typename T>
629 inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
631 Unserialize_impl(is, v, nType, nVersion, T());
637 * vector
639 template<typename Stream, typename T, typename A>
640 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
642 WriteCompactSize(os, v.size());
643 if (!v.empty())
644 os.write((char*)&v[0], v.size() * sizeof(T));
647 template<typename Stream, typename T, typename A, typename V>
648 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
650 WriteCompactSize(os, v.size());
651 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
652 ::Serialize(os, (*vi), nType, nVersion);
655 template<typename Stream, typename T, typename A>
656 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
658 Serialize_impl(os, v, nType, nVersion, T());
662 template<typename Stream, typename T, typename A>
663 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
665 // Limit size per read so bogus size value won't cause out of memory
666 v.clear();
667 unsigned int nSize = ReadCompactSize(is);
668 unsigned int i = 0;
669 while (i < nSize)
671 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
672 v.resize(i + blk);
673 is.read((char*)&v[i], blk * sizeof(T));
674 i += blk;
678 template<typename Stream, typename T, typename A, typename V>
679 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
681 v.clear();
682 unsigned int nSize = ReadCompactSize(is);
683 unsigned int i = 0;
684 unsigned int nMid = 0;
685 while (nMid < nSize)
687 nMid += 5000000 / sizeof(T);
688 if (nMid > nSize)
689 nMid = nSize;
690 v.resize(nMid);
691 for (; i < nMid; i++)
692 Unserialize(is, v[i], nType, nVersion);
696 template<typename Stream, typename T, typename A>
697 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
699 Unserialize_impl(is, v, nType, nVersion, T());
705 * pair
707 template<typename Stream, typename K, typename T>
708 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
710 Serialize(os, item.first, nType, nVersion);
711 Serialize(os, item.second, nType, nVersion);
714 template<typename Stream, typename K, typename T>
715 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
717 Unserialize(is, item.first, nType, nVersion);
718 Unserialize(is, item.second, nType, nVersion);
724 * map
726 template<typename Stream, typename K, typename T, typename Pred, typename A>
727 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
729 WriteCompactSize(os, m.size());
730 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
731 Serialize(os, (*mi), nType, nVersion);
734 template<typename Stream, typename K, typename T, typename Pred, typename A>
735 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
737 m.clear();
738 unsigned int nSize = ReadCompactSize(is);
739 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
740 for (unsigned int i = 0; i < nSize; i++)
742 std::pair<K, T> item;
743 Unserialize(is, item, nType, nVersion);
744 mi = m.insert(mi, item);
751 * set
753 template<typename Stream, typename K, typename Pred, typename A>
754 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
756 WriteCompactSize(os, m.size());
757 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
758 Serialize(os, (*it), nType, nVersion);
761 template<typename Stream, typename K, typename Pred, typename A>
762 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
764 m.clear();
765 unsigned int nSize = ReadCompactSize(is);
766 typename std::set<K, Pred, A>::iterator it = m.begin();
767 for (unsigned int i = 0; i < nSize; i++)
769 K key;
770 Unserialize(is, key, nType, nVersion);
771 it = m.insert(it, key);
778 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
780 struct CSerActionSerialize
782 bool ForRead() const { return false; }
784 struct CSerActionUnserialize
786 bool ForRead() const { return true; }
789 template<typename Stream, typename T>
790 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
792 ::Serialize(s, obj, nType, nVersion);
795 template<typename Stream, typename T>
796 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
798 ::Unserialize(s, obj, nType, nVersion);
809 class CSizeComputer
811 protected:
812 size_t nSize;
814 const int nType;
815 const int nVersion;
816 public:
818 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
820 void write(const char *psz, size_t nSize)
822 this->nSize += nSize;
825 template<typename T>
826 CSizeComputer& operator<<(const T& obj)
828 ::Serialize(*this, obj, nType, nVersion);
829 return (*this);
832 size_t size() const {
833 return nSize;
837 template<typename Stream>
838 void SerializeMany(Stream& s, int nType, int nVersion)
842 template<typename Stream, typename Arg>
843 void SerializeMany(Stream& s, int nType, int nVersion, Arg&& arg)
845 ::Serialize(s, std::forward<Arg>(arg), nType, nVersion);
848 template<typename Stream, typename Arg, typename... Args>
849 void SerializeMany(Stream& s, int nType, int nVersion, Arg&& arg, Args&&... args)
851 ::Serialize(s, std::forward<Arg>(arg), nType, nVersion);
852 ::SerializeMany(s, nType, nVersion, std::forward<Args>(args)...);
855 template<typename Stream>
856 inline void UnserializeMany(Stream& s, int nType, int nVersion)
860 template<typename Stream, typename Arg>
861 inline void UnserializeMany(Stream& s, int nType, int nVersion, Arg& arg)
863 ::Unserialize(s, arg, nType, nVersion);
866 template<typename Stream, typename Arg, typename... Args>
867 inline void UnserializeMany(Stream& s, int nType, int nVersion, Arg& arg, Args&... args)
869 ::Unserialize(s, arg, nType, nVersion);
870 ::UnserializeMany(s, nType, nVersion, args...);
873 template<typename Stream, typename... Args>
874 inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionSerialize ser_action, Args&&... args)
876 ::SerializeMany(s, nType, nVersion, std::forward<Args>(args)...);
879 template<typename Stream, typename... Args>
880 inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionUnserialize ser_action, Args&... args)
882 ::UnserializeMany(s, nType, nVersion, args...);
885 template <typename T>
886 size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
888 return (CSizeComputer(nType, nVersion) << t).size();
891 template <typename S, typename T>
892 size_t GetSerializeSize(const S& s, const T& t)
894 return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
897 #endif // BITCOIN_SERIALIZE_H