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"
24 #include "prevector.h"
26 static const unsigned int MAX_SIZE
= 0x02000000;
29 * Dummy data type to identify deserializing constructors.
31 * By convention, a constructor of a type T with signature
33 * template <typename Stream> T::T(deserialize_type, Stream& s)
35 * is a deserializing constructor, which builds the type by
36 * deserializing it from s. If T contains const fields, this
37 * is likely the only way to do so.
39 struct deserialize_type
{};
40 constexpr deserialize_type deserialize
{};
43 * Used to bypass the rule against non-const reference to temporary
44 * where it makes sense with wrappers such as CFlatData or CTxDB
47 inline T
& REF(const T
& val
)
49 return const_cast<T
&>(val
);
53 * Used to acquire a non-const pointer "this" to generate bodies
54 * of const serialization operations from a template
57 inline T
* NCONST_PTR(const T
* val
)
59 return const_cast<T
*>(val
);
63 * Important: Do not use the following functions in new code, but use v.data()
64 * and v.data() + v.size() respectively directly. They were once introduced to
65 * have a compatible, safe way to get the begin and end pointer of a vector.
66 * However with C++11 the language has built-in functionality for this and it's
67 * more readable to just use that.
70 inline typename
V::value_type
* begin_ptr(V
& v
)
75 inline const typename
V::value_type
* begin_ptr(const V
& v
)
80 inline typename
V::value_type
* end_ptr(V
& v
)
82 return v
.data() + v
.size();
85 inline const typename
V::value_type
* end_ptr(const V
& v
)
87 return v
.data() + v
.size();
91 * Lowest-level serialization and conversion.
92 * @note Sizes of these types are verified in the tests
94 template<typename Stream
> inline void ser_writedata8(Stream
&s
, uint8_t obj
)
96 s
.write((char*)&obj
, 1);
98 template<typename Stream
> inline void ser_writedata16(Stream
&s
, uint16_t obj
)
101 s
.write((char*)&obj
, 2);
103 template<typename Stream
> inline void ser_writedata32(Stream
&s
, uint32_t obj
)
106 s
.write((char*)&obj
, 4);
108 template<typename Stream
> inline void ser_writedata64(Stream
&s
, uint64_t obj
)
111 s
.write((char*)&obj
, 8);
113 template<typename Stream
> inline uint8_t ser_readdata8(Stream
&s
)
116 s
.read((char*)&obj
, 1);
119 template<typename Stream
> inline uint16_t ser_readdata16(Stream
&s
)
122 s
.read((char*)&obj
, 2);
125 template<typename Stream
> inline uint32_t ser_readdata32(Stream
&s
)
128 s
.read((char*)&obj
, 4);
131 template<typename Stream
> inline uint64_t ser_readdata64(Stream
&s
)
134 s
.read((char*)&obj
, 8);
137 inline uint64_t ser_double_to_uint64(double x
)
139 union { double x
; uint64_t y
; } tmp
;
143 inline uint32_t ser_float_to_uint32(float x
)
145 union { float x
; uint32_t y
; } tmp
;
149 inline double ser_uint64_to_double(uint64_t y
)
151 union { double x
; uint64_t y
; } tmp
;
155 inline float ser_uint32_to_float(uint32_t y
)
157 union { float x
; uint32_t y
; } tmp
;
163 /////////////////////////////////////////////////////////////////
165 // Templates for serializing to anything that looks like a stream,
166 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
174 SER_NETWORK
= (1 << 0),
176 SER_GETHASH
= (1 << 2),
179 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
180 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
183 * Implement three methods for serializable objects. These are actually wrappers over
184 * "SerializationOp" template, which implements the body of each class' serialization
185 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
188 #define ADD_SERIALIZE_METHODS \
189 template<typename Stream> \
190 void Serialize(Stream& s) const { \
191 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
193 template<typename Stream> \
194 void Unserialize(Stream& s) { \
195 SerializationOp(s, CSerActionUnserialize()); \
198 template<typename Stream
> inline void Serialize(Stream
& s
, char a
) { ser_writedata8(s
, a
); } // TODO Get rid of bare char
199 template<typename Stream
> inline void Serialize(Stream
& s
, int8_t a
) { ser_writedata8(s
, a
); }
200 template<typename Stream
> inline void Serialize(Stream
& s
, uint8_t a
) { ser_writedata8(s
, a
); }
201 template<typename Stream
> inline void Serialize(Stream
& s
, int16_t a
) { ser_writedata16(s
, a
); }
202 template<typename Stream
> inline void Serialize(Stream
& s
, uint16_t a
) { ser_writedata16(s
, a
); }
203 template<typename Stream
> inline void Serialize(Stream
& s
, int32_t a
) { ser_writedata32(s
, a
); }
204 template<typename Stream
> inline void Serialize(Stream
& s
, uint32_t a
) { ser_writedata32(s
, a
); }
205 template<typename Stream
> inline void Serialize(Stream
& s
, int64_t a
) { ser_writedata64(s
, a
); }
206 template<typename Stream
> inline void Serialize(Stream
& s
, uint64_t a
) { ser_writedata64(s
, a
); }
207 template<typename Stream
> inline void Serialize(Stream
& s
, float a
) { ser_writedata32(s
, ser_float_to_uint32(a
)); }
208 template<typename Stream
> inline void Serialize(Stream
& s
, double a
) { ser_writedata64(s
, ser_double_to_uint64(a
)); }
210 template<typename Stream
> inline void Unserialize(Stream
& s
, char& a
) { a
= ser_readdata8(s
); } // TODO Get rid of bare char
211 template<typename Stream
> inline void Unserialize(Stream
& s
, int8_t& a
) { a
= ser_readdata8(s
); }
212 template<typename Stream
> inline void Unserialize(Stream
& s
, uint8_t& a
) { a
= ser_readdata8(s
); }
213 template<typename Stream
> inline void Unserialize(Stream
& s
, int16_t& a
) { a
= ser_readdata16(s
); }
214 template<typename Stream
> inline void Unserialize(Stream
& s
, uint16_t& a
) { a
= ser_readdata16(s
); }
215 template<typename Stream
> inline void Unserialize(Stream
& s
, int32_t& a
) { a
= ser_readdata32(s
); }
216 template<typename Stream
> inline void Unserialize(Stream
& s
, uint32_t& a
) { a
= ser_readdata32(s
); }
217 template<typename Stream
> inline void Unserialize(Stream
& s
, int64_t& a
) { a
= ser_readdata64(s
); }
218 template<typename Stream
> inline void Unserialize(Stream
& s
, uint64_t& a
) { a
= ser_readdata64(s
); }
219 template<typename Stream
> inline void Unserialize(Stream
& s
, float& a
) { a
= ser_uint32_to_float(ser_readdata32(s
)); }
220 template<typename Stream
> inline void Unserialize(Stream
& s
, double& a
) { a
= ser_uint64_to_double(ser_readdata64(s
)); }
222 template<typename Stream
> inline void Serialize(Stream
& s
, bool a
) { char f
=a
; ser_writedata8(s
, f
); }
223 template<typename Stream
> inline void Unserialize(Stream
& s
, bool& a
) { char f
=ser_readdata8(s
); a
=f
; }
232 * size < 253 -- 1 byte
233 * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
234 * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
235 * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
237 inline unsigned int GetSizeOfCompactSize(uint64_t nSize
)
239 if (nSize
< 253) return sizeof(unsigned char);
240 else if (nSize
<= std::numeric_limits
<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
241 else if (nSize
<= std::numeric_limits
<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
242 else return sizeof(unsigned char) + sizeof(uint64_t);
245 inline void WriteCompactSize(CSizeComputer
& os
, uint64_t nSize
);
247 template<typename Stream
>
248 void WriteCompactSize(Stream
& os
, uint64_t nSize
)
252 ser_writedata8(os
, nSize
);
254 else if (nSize
<= std::numeric_limits
<unsigned short>::max())
256 ser_writedata8(os
, 253);
257 ser_writedata16(os
, nSize
);
259 else if (nSize
<= std::numeric_limits
<unsigned int>::max())
261 ser_writedata8(os
, 254);
262 ser_writedata32(os
, nSize
);
266 ser_writedata8(os
, 255);
267 ser_writedata64(os
, nSize
);
272 template<typename Stream
>
273 uint64_t ReadCompactSize(Stream
& is
)
275 uint8_t chSize
= ser_readdata8(is
);
276 uint64_t nSizeRet
= 0;
281 else if (chSize
== 253)
283 nSizeRet
= ser_readdata16(is
);
285 throw std::ios_base::failure("non-canonical ReadCompactSize()");
287 else if (chSize
== 254)
289 nSizeRet
= ser_readdata32(is
);
290 if (nSizeRet
< 0x10000u
)
291 throw std::ios_base::failure("non-canonical ReadCompactSize()");
295 nSizeRet
= ser_readdata64(is
);
296 if (nSizeRet
< 0x100000000ULL
)
297 throw std::ios_base::failure("non-canonical ReadCompactSize()");
299 if (nSizeRet
> (uint64_t)MAX_SIZE
)
300 throw std::ios_base::failure("ReadCompactSize(): size too large");
305 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
306 * The high bit in each byte signifies whether another digit follows. To make
307 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
308 * Thus, the byte sequence a[] with length len, where all but the last byte
309 * has bit 128 set, encodes the number:
311 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
314 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
315 * * Every integer has exactly one encoding
316 * * Encoding does not depend on size of original integer type
317 * * No redundancy: every (infinite) byte sequence corresponds to a list
318 * of encoded integers.
320 * 0: [0x00] 256: [0x81 0x00]
321 * 1: [0x01] 16383: [0xFE 0x7F]
322 * 127: [0x7F] 16384: [0xFF 0x00]
323 * 128: [0x80 0x00] 16511: [0xFF 0x7F]
324 * 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F]
325 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
329 inline unsigned int GetSizeOfVarInt(I n
)
342 inline void WriteVarInt(CSizeComputer
& os
, I n
);
344 template<typename Stream
, typename I
>
345 void WriteVarInt(Stream
& os
, I n
)
347 unsigned char tmp
[(sizeof(n
)*8+6)/7];
350 tmp
[len
] = (n
& 0x7F) | (len
? 0x80 : 0x00);
357 ser_writedata8(os
, tmp
[len
]);
361 template<typename Stream
, typename I
>
362 I
ReadVarInt(Stream
& is
)
366 unsigned char chData
= ser_readdata8(is
);
367 n
= (n
<< 7) | (chData
& 0x7F);
375 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
376 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
377 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
378 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
381 * Wrapper for serializing arrays and POD.
389 CFlatData(void* pbeginIn
, void* pendIn
) : pbegin((char*)pbeginIn
), pend((char*)pendIn
) { }
390 template <class T
, class TAl
>
391 explicit CFlatData(std::vector
<T
,TAl
> &v
)
393 pbegin
= (char*)begin_ptr(v
);
394 pend
= (char*)end_ptr(v
);
396 template <unsigned int N
, typename T
, typename S
, typename D
>
397 explicit CFlatData(prevector
<N
, T
, S
, D
> &v
)
399 pbegin
= (char*)begin_ptr(v
);
400 pend
= (char*)end_ptr(v
);
402 char* begin() { return pbegin
; }
403 const char* begin() const { return pbegin
; }
404 char* end() { return pend
; }
405 const char* end() const { return pend
; }
407 template<typename Stream
>
408 void Serialize(Stream
& s
) const
410 s
.write(pbegin
, pend
- pbegin
);
413 template<typename Stream
>
414 void Unserialize(Stream
& s
)
416 s
.read(pbegin
, pend
- pbegin
);
426 CVarInt(I
& nIn
) : n(nIn
) { }
428 template<typename Stream
>
429 void Serialize(Stream
&s
) const {
430 WriteVarInt
<Stream
,I
>(s
, n
);
433 template<typename Stream
>
434 void Unserialize(Stream
& s
) {
435 n
= ReadVarInt
<Stream
,I
>(s
);
444 CCompactSize(uint64_t& nIn
) : n(nIn
) { }
446 template<typename Stream
>
447 void Serialize(Stream
&s
) const {
448 WriteCompactSize
<Stream
>(s
, n
);
451 template<typename Stream
>
452 void Unserialize(Stream
& s
) {
453 n
= ReadCompactSize
<Stream
>(s
);
457 template<size_t Limit
>
463 LimitedString(std::string
& _string
) : string(_string
) {}
465 template<typename Stream
>
466 void Unserialize(Stream
& s
)
468 size_t size
= ReadCompactSize(s
);
470 throw std::ios_base::failure("String length limit exceeded");
474 s
.read((char*)&string
[0], size
);
477 template<typename Stream
>
478 void Serialize(Stream
& s
) const
480 WriteCompactSize(s
, string
.size());
482 s
.write((char*)&string
[0], string
.size());
487 CVarInt
<I
> WrapVarInt(I
& n
) { return CVarInt
<I
>(n
); }
490 * Forward declarations
496 template<typename Stream
, typename C
> void Serialize(Stream
& os
, const std::basic_string
<C
>& str
);
497 template<typename Stream
, typename C
> void Unserialize(Stream
& is
, std::basic_string
<C
>& str
);
501 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
503 template<typename Stream
, unsigned int N
, typename T
> void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const unsigned char&);
504 template<typename Stream
, unsigned int N
, typename T
, typename V
> void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const V
&);
505 template<typename Stream
, unsigned int N
, typename T
> inline void Serialize(Stream
& os
, const prevector
<N
, T
>& v
);
506 template<typename Stream
, unsigned int N
, typename T
> void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const unsigned char&);
507 template<typename Stream
, unsigned int N
, typename T
, typename V
> void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const V
&);
508 template<typename Stream
, unsigned int N
, typename T
> inline void Unserialize(Stream
& is
, prevector
<N
, T
>& v
);
512 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
514 template<typename Stream
, typename T
, typename A
> void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const unsigned char&);
515 template<typename Stream
, typename T
, typename A
, typename V
> void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const V
&);
516 template<typename Stream
, typename T
, typename A
> inline void Serialize(Stream
& os
, const std::vector
<T
, A
>& v
);
517 template<typename Stream
, typename T
, typename A
> void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const unsigned char&);
518 template<typename Stream
, typename T
, typename A
, typename V
> void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const V
&);
519 template<typename Stream
, typename T
, typename A
> inline void Unserialize(Stream
& is
, std::vector
<T
, A
>& v
);
524 template<typename Stream
, typename K
, typename T
> void Serialize(Stream
& os
, const std::pair
<K
, T
>& item
);
525 template<typename Stream
, typename K
, typename T
> void Unserialize(Stream
& is
, std::pair
<K
, T
>& item
);
530 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
> void Serialize(Stream
& os
, const std::map
<K
, T
, Pred
, A
>& m
);
531 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
> void Unserialize(Stream
& is
, std::map
<K
, T
, Pred
, A
>& m
);
536 template<typename Stream
, typename K
, typename Pred
, typename A
> void Serialize(Stream
& os
, const std::set
<K
, Pred
, A
>& m
);
537 template<typename Stream
, typename K
, typename Pred
, typename A
> void Unserialize(Stream
& is
, std::set
<K
, Pred
, A
>& m
);
542 template<typename Stream
, typename T
> void Serialize(Stream
& os
, const std::shared_ptr
<const T
>& p
);
543 template<typename Stream
, typename T
> void Unserialize(Stream
& os
, std::shared_ptr
<const T
>& p
);
548 template<typename Stream
, typename T
> void Serialize(Stream
& os
, const std::unique_ptr
<const T
>& p
);
549 template<typename Stream
, typename T
> void Unserialize(Stream
& os
, std::unique_ptr
<const T
>& p
);
554 * If none of the specialized versions above matched, default to calling member function.
556 template<typename Stream
, typename T
>
557 inline void Serialize(Stream
& os
, const T
& a
)
562 template<typename Stream
, typename T
>
563 inline void Unserialize(Stream
& is
, T
& a
)
575 template<typename Stream
, typename C
>
576 void Serialize(Stream
& os
, const std::basic_string
<C
>& str
)
578 WriteCompactSize(os
, str
.size());
580 os
.write((char*)&str
[0], str
.size() * sizeof(str
[0]));
583 template<typename Stream
, typename C
>
584 void Unserialize(Stream
& is
, std::basic_string
<C
>& str
)
586 unsigned int nSize
= ReadCompactSize(is
);
589 is
.read((char*)&str
[0], nSize
* sizeof(str
[0]));
597 template<typename Stream
, unsigned int N
, typename T
>
598 void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const unsigned char&)
600 WriteCompactSize(os
, v
.size());
602 os
.write((char*)&v
[0], v
.size() * sizeof(T
));
605 template<typename Stream
, unsigned int N
, typename T
, typename V
>
606 void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const V
&)
608 WriteCompactSize(os
, v
.size());
609 for (typename prevector
<N
, T
>::const_iterator vi
= v
.begin(); vi
!= v
.end(); ++vi
)
610 ::Serialize(os
, (*vi
));
613 template<typename Stream
, unsigned int N
, typename T
>
614 inline void Serialize(Stream
& os
, const prevector
<N
, T
>& v
)
616 Serialize_impl(os
, v
, T());
620 template<typename Stream
, unsigned int N
, typename T
>
621 void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const unsigned char&)
623 // Limit size per read so bogus size value won't cause out of memory
625 unsigned int nSize
= ReadCompactSize(is
);
629 unsigned int blk
= std::min(nSize
- i
, (unsigned int)(1 + 4999999 / sizeof(T
)));
631 is
.read((char*)&v
[i
], blk
* sizeof(T
));
636 template<typename Stream
, unsigned int N
, typename T
, typename V
>
637 void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const V
&)
640 unsigned int nSize
= ReadCompactSize(is
);
642 unsigned int nMid
= 0;
645 nMid
+= 5000000 / sizeof(T
);
649 for (; i
< nMid
; i
++)
650 Unserialize(is
, v
[i
]);
654 template<typename Stream
, unsigned int N
, typename T
>
655 inline void Unserialize(Stream
& is
, prevector
<N
, T
>& v
)
657 Unserialize_impl(is
, v
, T());
665 template<typename Stream
, typename T
, typename A
>
666 void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const unsigned char&)
668 WriteCompactSize(os
, v
.size());
670 os
.write((char*)&v
[0], v
.size() * sizeof(T
));
673 template<typename Stream
, typename T
, typename A
, typename V
>
674 void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const V
&)
676 WriteCompactSize(os
, v
.size());
677 for (typename
std::vector
<T
, A
>::const_iterator vi
= v
.begin(); vi
!= v
.end(); ++vi
)
678 ::Serialize(os
, (*vi
));
681 template<typename Stream
, typename T
, typename A
>
682 inline void Serialize(Stream
& os
, const std::vector
<T
, A
>& v
)
684 Serialize_impl(os
, v
, T());
688 template<typename Stream
, typename T
, typename A
>
689 void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const unsigned char&)
691 // Limit size per read so bogus size value won't cause out of memory
693 unsigned int nSize
= ReadCompactSize(is
);
697 unsigned int blk
= std::min(nSize
- i
, (unsigned int)(1 + 4999999 / sizeof(T
)));
699 is
.read((char*)&v
[i
], blk
* sizeof(T
));
704 template<typename Stream
, typename T
, typename A
, typename V
>
705 void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const V
&)
708 unsigned int nSize
= ReadCompactSize(is
);
710 unsigned int nMid
= 0;
713 nMid
+= 5000000 / sizeof(T
);
717 for (; i
< nMid
; i
++)
718 Unserialize(is
, v
[i
]);
722 template<typename Stream
, typename T
, typename A
>
723 inline void Unserialize(Stream
& is
, std::vector
<T
, A
>& v
)
725 Unserialize_impl(is
, v
, T());
733 template<typename Stream
, typename K
, typename T
>
734 void Serialize(Stream
& os
, const std::pair
<K
, T
>& item
)
736 Serialize(os
, item
.first
);
737 Serialize(os
, item
.second
);
740 template<typename Stream
, typename K
, typename T
>
741 void Unserialize(Stream
& is
, std::pair
<K
, T
>& item
)
743 Unserialize(is
, item
.first
);
744 Unserialize(is
, item
.second
);
752 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
>
753 void Serialize(Stream
& os
, const std::map
<K
, T
, Pred
, A
>& m
)
755 WriteCompactSize(os
, m
.size());
756 for (typename
std::map
<K
, T
, Pred
, A
>::const_iterator mi
= m
.begin(); mi
!= m
.end(); ++mi
)
757 Serialize(os
, (*mi
));
760 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
>
761 void Unserialize(Stream
& is
, std::map
<K
, T
, Pred
, A
>& m
)
764 unsigned int nSize
= ReadCompactSize(is
);
765 typename
std::map
<K
, T
, Pred
, A
>::iterator mi
= m
.begin();
766 for (unsigned int i
= 0; i
< nSize
; i
++)
768 std::pair
<K
, T
> item
;
769 Unserialize(is
, item
);
770 mi
= m
.insert(mi
, item
);
779 template<typename Stream
, typename K
, typename Pred
, typename A
>
780 void Serialize(Stream
& os
, const std::set
<K
, Pred
, A
>& m
)
782 WriteCompactSize(os
, m
.size());
783 for (typename
std::set
<K
, Pred
, A
>::const_iterator it
= m
.begin(); it
!= m
.end(); ++it
)
784 Serialize(os
, (*it
));
787 template<typename Stream
, typename K
, typename Pred
, typename A
>
788 void Unserialize(Stream
& is
, std::set
<K
, Pred
, A
>& m
)
791 unsigned int nSize
= ReadCompactSize(is
);
792 typename
std::set
<K
, Pred
, A
>::iterator it
= m
.begin();
793 for (unsigned int i
= 0; i
< nSize
; i
++)
796 Unserialize(is
, key
);
797 it
= m
.insert(it
, key
);
806 template<typename Stream
, typename T
> void
807 Serialize(Stream
& os
, const std::unique_ptr
<const T
>& p
)
812 template<typename Stream
, typename T
>
813 void Unserialize(Stream
& is
, std::unique_ptr
<const T
>& p
)
815 p
.reset(new T(deserialize
, is
));
823 template<typename Stream
, typename T
> void
824 Serialize(Stream
& os
, const std::shared_ptr
<const T
>& p
)
829 template<typename Stream
, typename T
>
830 void Unserialize(Stream
& is
, std::shared_ptr
<const T
>& p
)
832 p
= std::make_shared
<const T
>(deserialize
, is
);
838 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
840 struct CSerActionSerialize
842 constexpr bool ForRead() const { return false; }
844 struct CSerActionUnserialize
846 constexpr bool ForRead() const { return true; }
849 template<typename Stream
, typename T
>
850 inline void SerReadWrite(Stream
& s
, const T
& obj
, CSerActionSerialize ser_action
)
855 template<typename Stream
, typename T
>
856 inline void SerReadWrite(Stream
& s
, T
& obj
, CSerActionUnserialize ser_action
)
858 ::Unserialize(s
, obj
);
869 /* ::GetSerializeSize implementations
871 * Computing the serialized size of objects is done through a special stream
872 * object of type CSizeComputer, which only records the number of bytes written
875 * If your Serialize or SerializationOp method has non-trivial overhead for
876 * serialization, it may be worthwhile to implement a specialized version for
877 * CSizeComputer, which uses the s.seek() method to record bytes that would
878 * be written instead.
888 CSizeComputer(int nTypeIn
, int nVersionIn
) : nSize(0), nType(nTypeIn
), nVersion(nVersionIn
) {}
890 void write(const char *psz
, size_t _nSize
)
892 this->nSize
+= _nSize
;
895 /** Pretend _nSize bytes are written, without specifying them. */
896 void seek(size_t _nSize
)
898 this->nSize
+= _nSize
;
902 CSizeComputer
& operator<<(const T
& obj
)
904 ::Serialize(*this, obj
);
908 size_t size() const {
912 int GetVersion() const { return nVersion
; }
913 int GetType() const { return nType
; }
916 template<typename Stream
>
917 void SerializeMany(Stream
& s
)
921 template<typename Stream
, typename Arg
>
922 void SerializeMany(Stream
& s
, Arg
&& arg
)
924 ::Serialize(s
, std::forward
<Arg
>(arg
));
927 template<typename Stream
, typename Arg
, typename
... Args
>
928 void SerializeMany(Stream
& s
, Arg
&& arg
, Args
&&... args
)
930 ::Serialize(s
, std::forward
<Arg
>(arg
));
931 ::SerializeMany(s
, std::forward
<Args
>(args
)...);
934 template<typename Stream
>
935 inline void UnserializeMany(Stream
& s
)
939 template<typename Stream
, typename Arg
>
940 inline void UnserializeMany(Stream
& s
, Arg
& arg
)
942 ::Unserialize(s
, arg
);
945 template<typename Stream
, typename Arg
, typename
... Args
>
946 inline void UnserializeMany(Stream
& s
, Arg
& arg
, Args
&... args
)
948 ::Unserialize(s
, arg
);
949 ::UnserializeMany(s
, args
...);
952 template<typename Stream
, typename
... Args
>
953 inline void SerReadWriteMany(Stream
& s
, CSerActionSerialize ser_action
, Args
&&... args
)
955 ::SerializeMany(s
, std::forward
<Args
>(args
)...);
958 template<typename Stream
, typename
... Args
>
959 inline void SerReadWriteMany(Stream
& s
, CSerActionUnserialize ser_action
, Args
&... args
)
961 ::UnserializeMany(s
, args
...);
965 inline void WriteVarInt(CSizeComputer
&s
, I n
)
967 s
.seek(GetSizeOfVarInt
<I
>(n
));
970 inline void WriteCompactSize(CSizeComputer
&s
, uint64_t nSize
)
972 s
.seek(GetSizeOfCompactSize(nSize
));
975 template <typename T
>
976 size_t GetSerializeSize(const T
& t
, int nType
, int nVersion
= 0)
978 return (CSizeComputer(nType
, nVersion
) << t
).size();
981 template <typename S
, typename T
>
982 size_t GetSerializeSize(const S
& s
, const T
& t
)
984 return (CSizeComputer(s
.GetType(), s
.GetVersion()) << t
).size();
987 #endif // BITCOIN_SERIALIZE_H