Get rid of nType and nVersion
[bitcoinplatinum.git] / src / serialize.h
blobfc4291fe95ca0cb8c3bfd15fefbf2f31b54a7961
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), ser_action))
163 #define READWRITEMANY(...) (::SerReadWriteMany(s, 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) const { \
174 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
176 template<typename Stream> \
177 void Unserialize(Stream& s) { \
178 SerializationOp(s, CSerActionUnserialize()); \
181 template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
182 template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
183 template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
184 template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
185 template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
186 template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
187 template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
188 template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
189 template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
190 template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
191 template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
193 template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
194 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
195 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
196 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
197 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
198 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
199 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
200 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
201 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
202 template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
203 template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
205 template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
206 template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
214 * Compact Size
215 * size < 253 -- 1 byte
216 * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
217 * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
218 * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
220 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
222 if (nSize < 253) return sizeof(unsigned char);
223 else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
224 else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
225 else return sizeof(unsigned char) + sizeof(uint64_t);
228 template<typename Stream>
229 void WriteCompactSize(Stream& os, uint64_t nSize)
231 if (nSize < 253)
233 ser_writedata8(os, nSize);
235 else if (nSize <= std::numeric_limits<unsigned short>::max())
237 ser_writedata8(os, 253);
238 ser_writedata16(os, nSize);
240 else if (nSize <= std::numeric_limits<unsigned int>::max())
242 ser_writedata8(os, 254);
243 ser_writedata32(os, nSize);
245 else
247 ser_writedata8(os, 255);
248 ser_writedata64(os, nSize);
250 return;
253 template<typename Stream>
254 uint64_t ReadCompactSize(Stream& is)
256 uint8_t chSize = ser_readdata8(is);
257 uint64_t nSizeRet = 0;
258 if (chSize < 253)
260 nSizeRet = chSize;
262 else if (chSize == 253)
264 nSizeRet = ser_readdata16(is);
265 if (nSizeRet < 253)
266 throw std::ios_base::failure("non-canonical ReadCompactSize()");
268 else if (chSize == 254)
270 nSizeRet = ser_readdata32(is);
271 if (nSizeRet < 0x10000u)
272 throw std::ios_base::failure("non-canonical ReadCompactSize()");
274 else
276 nSizeRet = ser_readdata64(is);
277 if (nSizeRet < 0x100000000ULL)
278 throw std::ios_base::failure("non-canonical ReadCompactSize()");
280 if (nSizeRet > (uint64_t)MAX_SIZE)
281 throw std::ios_base::failure("ReadCompactSize(): size too large");
282 return nSizeRet;
286 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
287 * The high bit in each byte signifies whether another digit follows. To make
288 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
289 * Thus, the byte sequence a[] with length len, where all but the last byte
290 * has bit 128 set, encodes the number:
292 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
294 * Properties:
295 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
296 * * Every integer has exactly one encoding
297 * * Encoding does not depend on size of original integer type
298 * * No redundancy: every (infinite) byte sequence corresponds to a list
299 * of encoded integers.
301 * 0: [0x00] 256: [0x81 0x00]
302 * 1: [0x01] 16383: [0xFE 0x7F]
303 * 127: [0x7F] 16384: [0xFF 0x00]
304 * 128: [0x80 0x00] 16511: [0xFF 0x7F]
305 * 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F]
306 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
309 template<typename I>
310 inline unsigned int GetSizeOfVarInt(I n)
312 int nRet = 0;
313 while(true) {
314 nRet++;
315 if (n <= 0x7F)
316 break;
317 n = (n >> 7) - 1;
319 return nRet;
322 template<typename Stream, typename I>
323 void WriteVarInt(Stream& os, I n)
325 unsigned char tmp[(sizeof(n)*8+6)/7];
326 int len=0;
327 while(true) {
328 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
329 if (n <= 0x7F)
330 break;
331 n = (n >> 7) - 1;
332 len++;
334 do {
335 ser_writedata8(os, tmp[len]);
336 } while(len--);
339 template<typename Stream, typename I>
340 I ReadVarInt(Stream& is)
342 I n = 0;
343 while(true) {
344 unsigned char chData = ser_readdata8(is);
345 n = (n << 7) | (chData & 0x7F);
346 if (chData & 0x80)
347 n++;
348 else
349 return n;
353 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
354 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
355 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
356 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
358 /**
359 * Wrapper for serializing arrays and POD.
361 class CFlatData
363 protected:
364 char* pbegin;
365 char* pend;
366 public:
367 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
368 template <class T, class TAl>
369 explicit CFlatData(std::vector<T,TAl> &v)
371 pbegin = (char*)begin_ptr(v);
372 pend = (char*)end_ptr(v);
374 template <unsigned int N, typename T, typename S, typename D>
375 explicit CFlatData(prevector<N, T, S, D> &v)
377 pbegin = (char*)begin_ptr(v);
378 pend = (char*)end_ptr(v);
380 char* begin() { return pbegin; }
381 const char* begin() const { return pbegin; }
382 char* end() { return pend; }
383 const char* end() const { return pend; }
385 template<typename Stream>
386 void Serialize(Stream& s) const
388 s.write(pbegin, pend - pbegin);
391 template<typename Stream>
392 void Unserialize(Stream& s)
394 s.read(pbegin, pend - pbegin);
398 template<typename I>
399 class CVarInt
401 protected:
402 I &n;
403 public:
404 CVarInt(I& nIn) : n(nIn) { }
406 template<typename Stream>
407 void Serialize(Stream &s) const {
408 WriteVarInt<Stream,I>(s, n);
411 template<typename Stream>
412 void Unserialize(Stream& s) {
413 n = ReadVarInt<Stream,I>(s);
417 class CCompactSize
419 protected:
420 uint64_t &n;
421 public:
422 CCompactSize(uint64_t& nIn) : n(nIn) { }
424 template<typename Stream>
425 void Serialize(Stream &s) const {
426 WriteCompactSize<Stream>(s, n);
429 template<typename Stream>
430 void Unserialize(Stream& s) {
431 n = ReadCompactSize<Stream>(s);
435 template<size_t Limit>
436 class LimitedString
438 protected:
439 std::string& string;
440 public:
441 LimitedString(std::string& string) : string(string) {}
443 template<typename Stream>
444 void Unserialize(Stream& s)
446 size_t size = ReadCompactSize(s);
447 if (size > Limit) {
448 throw std::ios_base::failure("String length limit exceeded");
450 string.resize(size);
451 if (size != 0)
452 s.read((char*)&string[0], size);
455 template<typename Stream>
456 void Serialize(Stream& s) const
458 WriteCompactSize(s, string.size());
459 if (!string.empty())
460 s.write((char*)&string[0], string.size());
464 template<typename I>
465 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
468 * Forward declarations
472 * string
474 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
475 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
478 * prevector
479 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
481 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
482 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
483 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
484 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
485 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
486 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
489 * vector
490 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
492 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
493 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
494 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
495 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
496 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
497 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
500 * pair
502 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
503 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
506 * map
508 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
509 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
512 * set
514 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
515 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
522 * If none of the specialized versions above matched, default to calling member function.
524 template<typename Stream, typename T>
525 inline void Serialize(Stream& os, const T& a)
527 a.Serialize(os);
530 template<typename Stream, typename T>
531 inline void Unserialize(Stream& is, T& a)
533 a.Unserialize(is);
541 * string
543 template<typename Stream, typename C>
544 void Serialize(Stream& os, const std::basic_string<C>& str)
546 WriteCompactSize(os, str.size());
547 if (!str.empty())
548 os.write((char*)&str[0], str.size() * sizeof(str[0]));
551 template<typename Stream, typename C>
552 void Unserialize(Stream& is, std::basic_string<C>& str)
554 unsigned int nSize = ReadCompactSize(is);
555 str.resize(nSize);
556 if (nSize != 0)
557 is.read((char*)&str[0], nSize * sizeof(str[0]));
563 * prevector
565 template<typename Stream, unsigned int N, typename T>
566 void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
568 WriteCompactSize(os, v.size());
569 if (!v.empty())
570 os.write((char*)&v[0], v.size() * sizeof(T));
573 template<typename Stream, unsigned int N, typename T, typename V>
574 void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
576 WriteCompactSize(os, v.size());
577 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
578 ::Serialize(os, (*vi));
581 template<typename Stream, unsigned int N, typename T>
582 inline void Serialize(Stream& os, const prevector<N, T>& v)
584 Serialize_impl(os, v, T());
588 template<typename Stream, unsigned int N, typename T>
589 void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
591 // Limit size per read so bogus size value won't cause out of memory
592 v.clear();
593 unsigned int nSize = ReadCompactSize(is);
594 unsigned int i = 0;
595 while (i < nSize)
597 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
598 v.resize(i + blk);
599 is.read((char*)&v[i], blk * sizeof(T));
600 i += blk;
604 template<typename Stream, unsigned int N, typename T, typename V>
605 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
607 v.clear();
608 unsigned int nSize = ReadCompactSize(is);
609 unsigned int i = 0;
610 unsigned int nMid = 0;
611 while (nMid < nSize)
613 nMid += 5000000 / sizeof(T);
614 if (nMid > nSize)
615 nMid = nSize;
616 v.resize(nMid);
617 for (; i < nMid; i++)
618 Unserialize(is, v[i]);
622 template<typename Stream, unsigned int N, typename T>
623 inline void Unserialize(Stream& is, prevector<N, T>& v)
625 Unserialize_impl(is, v, T());
631 * vector
633 template<typename Stream, typename T, typename A>
634 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
636 WriteCompactSize(os, v.size());
637 if (!v.empty())
638 os.write((char*)&v[0], v.size() * sizeof(T));
641 template<typename Stream, typename T, typename A, typename V>
642 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
644 WriteCompactSize(os, v.size());
645 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
646 ::Serialize(os, (*vi));
649 template<typename Stream, typename T, typename A>
650 inline void Serialize(Stream& os, const std::vector<T, A>& v)
652 Serialize_impl(os, v, T());
656 template<typename Stream, typename T, typename A>
657 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
659 // Limit size per read so bogus size value won't cause out of memory
660 v.clear();
661 unsigned int nSize = ReadCompactSize(is);
662 unsigned int i = 0;
663 while (i < nSize)
665 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
666 v.resize(i + blk);
667 is.read((char*)&v[i], blk * sizeof(T));
668 i += blk;
672 template<typename Stream, typename T, typename A, typename V>
673 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
675 v.clear();
676 unsigned int nSize = ReadCompactSize(is);
677 unsigned int i = 0;
678 unsigned int nMid = 0;
679 while (nMid < nSize)
681 nMid += 5000000 / sizeof(T);
682 if (nMid > nSize)
683 nMid = nSize;
684 v.resize(nMid);
685 for (; i < nMid; i++)
686 Unserialize(is, v[i]);
690 template<typename Stream, typename T, typename A>
691 inline void Unserialize(Stream& is, std::vector<T, A>& v)
693 Unserialize_impl(is, v, T());
699 * pair
701 template<typename Stream, typename K, typename T>
702 void Serialize(Stream& os, const std::pair<K, T>& item)
704 Serialize(os, item.first);
705 Serialize(os, item.second);
708 template<typename Stream, typename K, typename T>
709 void Unserialize(Stream& is, std::pair<K, T>& item)
711 Unserialize(is, item.first);
712 Unserialize(is, item.second);
718 * map
720 template<typename Stream, typename K, typename T, typename Pred, typename A>
721 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
723 WriteCompactSize(os, m.size());
724 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
725 Serialize(os, (*mi));
728 template<typename Stream, typename K, typename T, typename Pred, typename A>
729 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
731 m.clear();
732 unsigned int nSize = ReadCompactSize(is);
733 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
734 for (unsigned int i = 0; i < nSize; i++)
736 std::pair<K, T> item;
737 Unserialize(is, item);
738 mi = m.insert(mi, item);
745 * set
747 template<typename Stream, typename K, typename Pred, typename A>
748 void Serialize(Stream& os, const std::set<K, Pred, A>& m)
750 WriteCompactSize(os, m.size());
751 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
752 Serialize(os, (*it));
755 template<typename Stream, typename K, typename Pred, typename A>
756 void Unserialize(Stream& is, std::set<K, Pred, A>& m)
758 m.clear();
759 unsigned int nSize = ReadCompactSize(is);
760 typename std::set<K, Pred, A>::iterator it = m.begin();
761 for (unsigned int i = 0; i < nSize; i++)
763 K key;
764 Unserialize(is, key);
765 it = m.insert(it, key);
772 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
774 struct CSerActionSerialize
776 bool ForRead() const { return false; }
778 struct CSerActionUnserialize
780 bool ForRead() const { return true; }
783 template<typename Stream, typename T>
784 inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
786 ::Serialize(s, obj);
789 template<typename Stream, typename T>
790 inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
792 ::Unserialize(s, obj);
803 class CSizeComputer
805 protected:
806 size_t nSize;
808 const int nType;
809 const int nVersion;
810 public:
811 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
813 void write(const char *psz, size_t nSize)
815 this->nSize += nSize;
818 template<typename T>
819 CSizeComputer& operator<<(const T& obj)
821 ::Serialize(*this, obj);
822 return (*this);
825 size_t size() const {
826 return nSize;
829 int GetVersion() const { return nVersion; }
830 int GetType() const { return nType; }
833 template<typename Stream>
834 void SerializeMany(Stream& s)
838 template<typename Stream, typename Arg>
839 void SerializeMany(Stream& s, Arg&& arg)
841 ::Serialize(s, std::forward<Arg>(arg));
844 template<typename Stream, typename Arg, typename... Args>
845 void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
847 ::Serialize(s, std::forward<Arg>(arg));
848 ::SerializeMany(s, std::forward<Args>(args)...);
851 template<typename Stream>
852 inline void UnserializeMany(Stream& s)
856 template<typename Stream, typename Arg>
857 inline void UnserializeMany(Stream& s, Arg& arg)
859 ::Unserialize(s, arg);
862 template<typename Stream, typename Arg, typename... Args>
863 inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
865 ::Unserialize(s, arg);
866 ::UnserializeMany(s, args...);
869 template<typename Stream, typename... Args>
870 inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
872 ::SerializeMany(s, std::forward<Args>(args)...);
875 template<typename Stream, typename... Args>
876 inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
878 ::UnserializeMany(s, args...);
881 template <typename T>
882 size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
884 return (CSizeComputer(nType, nVersion) << t).size();
887 template <typename S, typename T>
888 size_t GetSerializeSize(const S& s, const T& t)
890 return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
893 #endif // BITCOIN_SERIALIZE_H