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"
23 #include "prevector.h"
25 static const unsigned int MAX_SIZE
= 0x02000000;
28 * Used to bypass the rule against non-const reference to temporary
29 * where it makes sense with wrappers such as CFlatData or CTxDB
32 inline T
& REF(const T
& val
)
34 return const_cast<T
&>(val
);
38 * Used to acquire a non-const pointer "this" to generate bodies
39 * of const serialization operations from a template
42 inline T
* NCONST_PTR(const T
* val
)
44 return const_cast<T
*>(val
);
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.
55 inline typename
V::value_type
* begin_ptr(V
& v
)
60 inline const typename
V::value_type
* begin_ptr(const V
& v
)
65 inline typename
V::value_type
* end_ptr(V
& v
)
67 return v
.data() + v
.size();
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
)
86 s
.write((char*)&obj
, 2);
88 template<typename Stream
> inline void ser_writedata32(Stream
&s
, uint32_t obj
)
91 s
.write((char*)&obj
, 4);
93 template<typename Stream
> inline void ser_writedata64(Stream
&s
, uint64_t obj
)
96 s
.write((char*)&obj
, 8);
98 template<typename Stream
> inline uint8_t ser_readdata8(Stream
&s
)
101 s
.read((char*)&obj
, 1);
104 template<typename Stream
> inline uint16_t ser_readdata16(Stream
&s
)
107 s
.read((char*)&obj
, 2);
110 template<typename Stream
> inline uint32_t ser_readdata32(Stream
&s
)
113 s
.read((char*)&obj
, 4);
116 template<typename Stream
> inline uint64_t ser_readdata64(Stream
&s
)
119 s
.read((char*)&obj
, 8);
122 inline uint64_t ser_double_to_uint64(double x
)
124 union { double x
; uint64_t y
; } tmp
;
128 inline uint32_t ser_float_to_uint32(float x
)
130 union { float x
; uint32_t y
; } tmp
;
134 inline double ser_uint64_to_double(uint64_t y
)
136 union { double x
; uint64_t y
; } tmp
;
140 inline float ser_uint32_to_float(uint32_t y
)
142 union { float x
; uint32_t y
; } tmp
;
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)
157 SER_NETWORK
= (1 << 0),
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__))
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
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); \
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
; }
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
)
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
);
250 ser_writedata8(os
, 255);
251 ser_writedata64(os
, nSize
);
256 template<typename Stream
>
257 uint64_t ReadCompactSize(Stream
& is
)
259 uint8_t chSize
= ser_readdata8(is
);
260 uint64_t nSizeRet
= 0;
265 else if (chSize
== 253)
267 nSizeRet
= ser_readdata16(is
);
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()");
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");
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))
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]
313 inline unsigned int GetSizeOfVarInt(I n
)
325 template<typename Stream
, typename I
>
326 void WriteVarInt(Stream
& os
, I n
)
328 unsigned char tmp
[(sizeof(n
)*8+6)/7];
331 tmp
[len
] = (n
& 0x7F) | (len
? 0x80 : 0x00);
338 ser_writedata8(os
, tmp
[len
]);
342 template<typename Stream
, typename I
>
343 I
ReadVarInt(Stream
& is
)
347 unsigned char chData
= ser_readdata8(is
);
348 n
= (n
<< 7) | (chData
& 0x7F);
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)))
362 * Wrapper for serializing arrays and POD.
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
);
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
);
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
>
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
);
451 throw std::ios_base::failure("String length limit exceeded");
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());
463 s
.write((char*)&string
[0], string
.size());
468 CVarInt
<I
> WrapVarInt(I
& n
) { return CVarInt
<I
>(n
); }
471 * Forward declarations
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);
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
);
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
);
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
);
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
);
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
);
549 template<typename Stream
, typename C
>
550 void Serialize(Stream
& os
, const std::basic_string
<C
>& str
, int, int)
552 WriteCompactSize(os
, str
.size());
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
);
563 is
.read((char*)&str
[0], nSize
* sizeof(str
[0]));
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());
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
599 unsigned int nSize
= ReadCompactSize(is
);
603 unsigned int blk
= std::min(nSize
- i
, (unsigned int)(1 + 4999999 / sizeof(T
)));
605 is
.read((char*)&v
[i
], blk
* sizeof(T
));
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
&)
614 unsigned int nSize
= ReadCompactSize(is
);
616 unsigned int nMid
= 0;
619 nMid
+= 5000000 / sizeof(T
);
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());
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());
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
667 unsigned int nSize
= ReadCompactSize(is
);
671 unsigned int blk
= std::min(nSize
- i
, (unsigned int)(1 + 4999999 / sizeof(T
)));
673 is
.read((char*)&v
[i
], blk
* sizeof(T
));
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
&)
682 unsigned int nSize
= ReadCompactSize(is
);
684 unsigned int nMid
= 0;
687 nMid
+= 5000000 / sizeof(T
);
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());
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
);
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
)
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
);
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
)
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
++)
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
);
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
;
826 CSizeComputer
& operator<<(const T
& obj
)
828 ::Serialize(*this, obj
, nType
, nVersion
);
832 size_t size() const {
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