1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 * Lowest-level serialization and conversion.
64 * @note Sizes of these types are verified in the tests
66 template<typename Stream
> inline void ser_writedata8(Stream
&s
, uint8_t obj
)
68 s
.write((char*)&obj
, 1);
70 template<typename Stream
> inline void ser_writedata16(Stream
&s
, uint16_t obj
)
73 s
.write((char*)&obj
, 2);
75 template<typename Stream
> inline void ser_writedata32(Stream
&s
, uint32_t obj
)
78 s
.write((char*)&obj
, 4);
80 template<typename Stream
> inline void ser_writedata64(Stream
&s
, uint64_t obj
)
83 s
.write((char*)&obj
, 8);
85 template<typename Stream
> inline uint8_t ser_readdata8(Stream
&s
)
88 s
.read((char*)&obj
, 1);
91 template<typename Stream
> inline uint16_t ser_readdata16(Stream
&s
)
94 s
.read((char*)&obj
, 2);
97 template<typename Stream
> inline uint32_t ser_readdata32(Stream
&s
)
100 s
.read((char*)&obj
, 4);
103 template<typename Stream
> inline uint64_t ser_readdata64(Stream
&s
)
106 s
.read((char*)&obj
, 8);
109 inline uint64_t ser_double_to_uint64(double x
)
111 union { double x
; uint64_t y
; } tmp
;
115 inline uint32_t ser_float_to_uint32(float x
)
117 union { float x
; uint32_t y
; } tmp
;
121 inline double ser_uint64_to_double(uint64_t y
)
123 union { double x
; uint64_t y
; } tmp
;
127 inline float ser_uint32_to_float(uint32_t y
)
129 union { float x
; uint32_t y
; } tmp
;
135 /////////////////////////////////////////////////////////////////
137 // Templates for serializing to anything that looks like a stream,
138 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
146 SER_NETWORK
= (1 << 0),
148 SER_GETHASH
= (1 << 2),
151 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
152 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
155 * Implement three methods for serializable objects. These are actually wrappers over
156 * "SerializationOp" template, which implements the body of each class' serialization
157 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
160 #define ADD_SERIALIZE_METHODS \
161 template<typename Stream> \
162 void Serialize(Stream& s) const { \
163 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
165 template<typename Stream> \
166 void Unserialize(Stream& s) { \
167 SerializationOp(s, CSerActionUnserialize()); \
170 template<typename Stream
> inline void Serialize(Stream
& s
, char a
) { ser_writedata8(s
, a
); } // TODO Get rid of bare char
171 template<typename Stream
> inline void Serialize(Stream
& s
, int8_t a
) { ser_writedata8(s
, a
); }
172 template<typename Stream
> inline void Serialize(Stream
& s
, uint8_t a
) { ser_writedata8(s
, a
); }
173 template<typename Stream
> inline void Serialize(Stream
& s
, int16_t a
) { ser_writedata16(s
, a
); }
174 template<typename Stream
> inline void Serialize(Stream
& s
, uint16_t a
) { ser_writedata16(s
, a
); }
175 template<typename Stream
> inline void Serialize(Stream
& s
, int32_t a
) { ser_writedata32(s
, a
); }
176 template<typename Stream
> inline void Serialize(Stream
& s
, uint32_t a
) { ser_writedata32(s
, a
); }
177 template<typename Stream
> inline void Serialize(Stream
& s
, int64_t a
) { ser_writedata64(s
, a
); }
178 template<typename Stream
> inline void Serialize(Stream
& s
, uint64_t a
) { ser_writedata64(s
, a
); }
179 template<typename Stream
> inline void Serialize(Stream
& s
, float a
) { ser_writedata32(s
, ser_float_to_uint32(a
)); }
180 template<typename Stream
> inline void Serialize(Stream
& s
, double a
) { ser_writedata64(s
, ser_double_to_uint64(a
)); }
182 template<typename Stream
> inline void Unserialize(Stream
& s
, char& a
) { a
= ser_readdata8(s
); } // TODO Get rid of bare char
183 template<typename Stream
> inline void Unserialize(Stream
& s
, int8_t& a
) { a
= ser_readdata8(s
); }
184 template<typename Stream
> inline void Unserialize(Stream
& s
, uint8_t& a
) { a
= ser_readdata8(s
); }
185 template<typename Stream
> inline void Unserialize(Stream
& s
, int16_t& a
) { a
= ser_readdata16(s
); }
186 template<typename Stream
> inline void Unserialize(Stream
& s
, uint16_t& a
) { a
= ser_readdata16(s
); }
187 template<typename Stream
> inline void Unserialize(Stream
& s
, int32_t& a
) { a
= ser_readdata32(s
); }
188 template<typename Stream
> inline void Unserialize(Stream
& s
, uint32_t& a
) { a
= ser_readdata32(s
); }
189 template<typename Stream
> inline void Unserialize(Stream
& s
, int64_t& a
) { a
= ser_readdata64(s
); }
190 template<typename Stream
> inline void Unserialize(Stream
& s
, uint64_t& a
) { a
= ser_readdata64(s
); }
191 template<typename Stream
> inline void Unserialize(Stream
& s
, float& a
) { a
= ser_uint32_to_float(ser_readdata32(s
)); }
192 template<typename Stream
> inline void Unserialize(Stream
& s
, double& a
) { a
= ser_uint64_to_double(ser_readdata64(s
)); }
194 template<typename Stream
> inline void Serialize(Stream
& s
, bool a
) { char f
=a
; ser_writedata8(s
, f
); }
195 template<typename Stream
> inline void Unserialize(Stream
& s
, bool& a
) { char f
=ser_readdata8(s
); a
=f
; }
204 * size < 253 -- 1 byte
205 * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
206 * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
207 * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
209 inline unsigned int GetSizeOfCompactSize(uint64_t nSize
)
211 if (nSize
< 253) return sizeof(unsigned char);
212 else if (nSize
<= std::numeric_limits
<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
213 else if (nSize
<= std::numeric_limits
<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
214 else return sizeof(unsigned char) + sizeof(uint64_t);
217 inline void WriteCompactSize(CSizeComputer
& os
, uint64_t nSize
);
219 template<typename Stream
>
220 void WriteCompactSize(Stream
& os
, uint64_t nSize
)
224 ser_writedata8(os
, nSize
);
226 else if (nSize
<= std::numeric_limits
<unsigned short>::max())
228 ser_writedata8(os
, 253);
229 ser_writedata16(os
, nSize
);
231 else if (nSize
<= std::numeric_limits
<unsigned int>::max())
233 ser_writedata8(os
, 254);
234 ser_writedata32(os
, nSize
);
238 ser_writedata8(os
, 255);
239 ser_writedata64(os
, nSize
);
244 template<typename Stream
>
245 uint64_t ReadCompactSize(Stream
& is
)
247 uint8_t chSize
= ser_readdata8(is
);
248 uint64_t nSizeRet
= 0;
253 else if (chSize
== 253)
255 nSizeRet
= ser_readdata16(is
);
257 throw std::ios_base::failure("non-canonical ReadCompactSize()");
259 else if (chSize
== 254)
261 nSizeRet
= ser_readdata32(is
);
262 if (nSizeRet
< 0x10000u
)
263 throw std::ios_base::failure("non-canonical ReadCompactSize()");
267 nSizeRet
= ser_readdata64(is
);
268 if (nSizeRet
< 0x100000000ULL
)
269 throw std::ios_base::failure("non-canonical ReadCompactSize()");
271 if (nSizeRet
> (uint64_t)MAX_SIZE
)
272 throw std::ios_base::failure("ReadCompactSize(): size too large");
277 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
278 * The high bit in each byte signifies whether another digit follows. To make
279 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
280 * Thus, the byte sequence a[] with length len, where all but the last byte
281 * has bit 128 set, encodes the number:
283 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
286 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
287 * * Every integer has exactly one encoding
288 * * Encoding does not depend on size of original integer type
289 * * No redundancy: every (infinite) byte sequence corresponds to a list
290 * of encoded integers.
292 * 0: [0x00] 256: [0x81 0x00]
293 * 1: [0x01] 16383: [0xFE 0x7F]
294 * 127: [0x7F] 16384: [0xFF 0x00]
295 * 128: [0x80 0x00] 16511: [0xFF 0x7F]
296 * 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F]
297 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
301 inline unsigned int GetSizeOfVarInt(I n
)
314 inline void WriteVarInt(CSizeComputer
& os
, I n
);
316 template<typename Stream
, typename I
>
317 void WriteVarInt(Stream
& os
, I n
)
319 unsigned char tmp
[(sizeof(n
)*8+6)/7];
322 tmp
[len
] = (n
& 0x7F) | (len
? 0x80 : 0x00);
329 ser_writedata8(os
, tmp
[len
]);
333 template<typename Stream
, typename I
>
334 I
ReadVarInt(Stream
& is
)
338 unsigned char chData
= ser_readdata8(is
);
339 n
= (n
<< 7) | (chData
& 0x7F);
347 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
348 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
349 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
350 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
353 * Wrapper for serializing arrays and POD.
361 CFlatData(void* pbeginIn
, void* pendIn
) : pbegin((char*)pbeginIn
), pend((char*)pendIn
) { }
362 template <class T
, class TAl
>
363 explicit CFlatData(std::vector
<T
,TAl
> &v
)
365 pbegin
= (char*)v
.data();
366 pend
= (char*)(v
.data() + v
.size());
368 template <unsigned int N
, typename T
, typename S
, typename D
>
369 explicit CFlatData(prevector
<N
, T
, S
, D
> &v
)
371 pbegin
= (char*)v
.data();
372 pend
= (char*)(v
.data() + v
.size());
374 char* begin() { return pbegin
; }
375 const char* begin() const { return pbegin
; }
376 char* end() { return pend
; }
377 const char* end() const { return pend
; }
379 template<typename Stream
>
380 void Serialize(Stream
& s
) const
382 s
.write(pbegin
, pend
- pbegin
);
385 template<typename Stream
>
386 void Unserialize(Stream
& s
)
388 s
.read(pbegin
, pend
- pbegin
);
398 CVarInt(I
& nIn
) : n(nIn
) { }
400 template<typename Stream
>
401 void Serialize(Stream
&s
) const {
402 WriteVarInt
<Stream
,I
>(s
, n
);
405 template<typename Stream
>
406 void Unserialize(Stream
& s
) {
407 n
= ReadVarInt
<Stream
,I
>(s
);
416 CCompactSize(uint64_t& nIn
) : n(nIn
) { }
418 template<typename Stream
>
419 void Serialize(Stream
&s
) const {
420 WriteCompactSize
<Stream
>(s
, n
);
423 template<typename Stream
>
424 void Unserialize(Stream
& s
) {
425 n
= ReadCompactSize
<Stream
>(s
);
429 template<size_t Limit
>
435 LimitedString(std::string
& _string
) : string(_string
) {}
437 template<typename Stream
>
438 void Unserialize(Stream
& s
)
440 size_t size
= ReadCompactSize(s
);
442 throw std::ios_base::failure("String length limit exceeded");
446 s
.read((char*)&string
[0], size
);
449 template<typename Stream
>
450 void Serialize(Stream
& s
) const
452 WriteCompactSize(s
, string
.size());
454 s
.write((char*)&string
[0], string
.size());
459 CVarInt
<I
> WrapVarInt(I
& n
) { return CVarInt
<I
>(n
); }
462 * Forward declarations
468 template<typename Stream
, typename C
> void Serialize(Stream
& os
, const std::basic_string
<C
>& str
);
469 template<typename Stream
, typename C
> void Unserialize(Stream
& is
, std::basic_string
<C
>& str
);
473 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
475 template<typename Stream
, unsigned int N
, typename T
> void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const unsigned char&);
476 template<typename Stream
, unsigned int N
, typename T
, typename V
> void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const V
&);
477 template<typename Stream
, unsigned int N
, typename T
> inline void Serialize(Stream
& os
, const prevector
<N
, T
>& v
);
478 template<typename Stream
, unsigned int N
, typename T
> void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const unsigned char&);
479 template<typename Stream
, unsigned int N
, typename T
, typename V
> void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const V
&);
480 template<typename Stream
, unsigned int N
, typename T
> inline void Unserialize(Stream
& is
, prevector
<N
, T
>& v
);
484 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
486 template<typename Stream
, typename T
, typename A
> void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const unsigned char&);
487 template<typename Stream
, typename T
, typename A
, typename V
> void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const V
&);
488 template<typename Stream
, typename T
, typename A
> inline void Serialize(Stream
& os
, const std::vector
<T
, A
>& v
);
489 template<typename Stream
, typename T
, typename A
> void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const unsigned char&);
490 template<typename Stream
, typename T
, typename A
, typename V
> void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const V
&);
491 template<typename Stream
, typename T
, typename A
> inline void Unserialize(Stream
& is
, std::vector
<T
, A
>& v
);
496 template<typename Stream
, typename K
, typename T
> void Serialize(Stream
& os
, const std::pair
<K
, T
>& item
);
497 template<typename Stream
, typename K
, typename T
> void Unserialize(Stream
& is
, std::pair
<K
, T
>& item
);
502 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
> void Serialize(Stream
& os
, const std::map
<K
, T
, Pred
, A
>& m
);
503 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
> void Unserialize(Stream
& is
, std::map
<K
, T
, Pred
, A
>& m
);
508 template<typename Stream
, typename K
, typename Pred
, typename A
> void Serialize(Stream
& os
, const std::set
<K
, Pred
, A
>& m
);
509 template<typename Stream
, typename K
, typename Pred
, typename A
> void Unserialize(Stream
& is
, std::set
<K
, Pred
, A
>& m
);
514 template<typename Stream
, typename T
> void Serialize(Stream
& os
, const std::shared_ptr
<const T
>& p
);
515 template<typename Stream
, typename T
> void Unserialize(Stream
& os
, std::shared_ptr
<const T
>& p
);
520 template<typename Stream
, typename T
> void Serialize(Stream
& os
, const std::unique_ptr
<const T
>& p
);
521 template<typename Stream
, typename T
> void Unserialize(Stream
& os
, std::unique_ptr
<const T
>& p
);
526 * If none of the specialized versions above matched, default to calling member function.
528 template<typename Stream
, typename T
>
529 inline void Serialize(Stream
& os
, const T
& a
)
534 template<typename Stream
, typename T
>
535 inline void Unserialize(Stream
& is
, T
& a
)
547 template<typename Stream
, typename C
>
548 void Serialize(Stream
& os
, const std::basic_string
<C
>& str
)
550 WriteCompactSize(os
, str
.size());
552 os
.write((char*)&str
[0], str
.size() * sizeof(str
[0]));
555 template<typename Stream
, typename C
>
556 void Unserialize(Stream
& is
, std::basic_string
<C
>& str
)
558 unsigned int nSize
= ReadCompactSize(is
);
561 is
.read((char*)&str
[0], nSize
* sizeof(str
[0]));
569 template<typename Stream
, unsigned int N
, typename T
>
570 void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const unsigned char&)
572 WriteCompactSize(os
, v
.size());
574 os
.write((char*)&v
[0], v
.size() * sizeof(T
));
577 template<typename Stream
, unsigned int N
, typename T
, typename V
>
578 void Serialize_impl(Stream
& os
, const prevector
<N
, T
>& v
, const V
&)
580 WriteCompactSize(os
, v
.size());
581 for (typename prevector
<N
, T
>::const_iterator vi
= v
.begin(); vi
!= v
.end(); ++vi
)
582 ::Serialize(os
, (*vi
));
585 template<typename Stream
, unsigned int N
, typename T
>
586 inline void Serialize(Stream
& os
, const prevector
<N
, T
>& v
)
588 Serialize_impl(os
, v
, T());
592 template<typename Stream
, unsigned int N
, typename T
>
593 void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const unsigned char&)
595 // Limit size per read so bogus size value won't cause out of memory
597 unsigned int nSize
= ReadCompactSize(is
);
601 unsigned int blk
= std::min(nSize
- i
, (unsigned int)(1 + 4999999 / sizeof(T
)));
603 is
.read((char*)&v
[i
], blk
* sizeof(T
));
608 template<typename Stream
, unsigned int N
, typename T
, typename V
>
609 void Unserialize_impl(Stream
& is
, prevector
<N
, T
>& v
, const V
&)
612 unsigned int nSize
= ReadCompactSize(is
);
614 unsigned int nMid
= 0;
617 nMid
+= 5000000 / sizeof(T
);
621 for (; i
< nMid
; i
++)
622 Unserialize(is
, v
[i
]);
626 template<typename Stream
, unsigned int N
, typename T
>
627 inline void Unserialize(Stream
& is
, prevector
<N
, T
>& v
)
629 Unserialize_impl(is
, v
, T());
637 template<typename Stream
, typename T
, typename A
>
638 void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const unsigned char&)
640 WriteCompactSize(os
, v
.size());
642 os
.write((char*)&v
[0], v
.size() * sizeof(T
));
645 template<typename Stream
, typename T
, typename A
, typename V
>
646 void Serialize_impl(Stream
& os
, const std::vector
<T
, A
>& v
, const V
&)
648 WriteCompactSize(os
, v
.size());
649 for (typename
std::vector
<T
, A
>::const_iterator vi
= v
.begin(); vi
!= v
.end(); ++vi
)
650 ::Serialize(os
, (*vi
));
653 template<typename Stream
, typename T
, typename A
>
654 inline void Serialize(Stream
& os
, const std::vector
<T
, A
>& v
)
656 Serialize_impl(os
, v
, T());
660 template<typename Stream
, typename T
, typename A
>
661 void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const unsigned char&)
663 // Limit size per read so bogus size value won't cause out of memory
665 unsigned int nSize
= ReadCompactSize(is
);
669 unsigned int blk
= std::min(nSize
- i
, (unsigned int)(1 + 4999999 / sizeof(T
)));
671 is
.read((char*)&v
[i
], blk
* sizeof(T
));
676 template<typename Stream
, typename T
, typename A
, typename V
>
677 void Unserialize_impl(Stream
& is
, std::vector
<T
, A
>& v
, const V
&)
680 unsigned int nSize
= ReadCompactSize(is
);
682 unsigned int nMid
= 0;
685 nMid
+= 5000000 / sizeof(T
);
689 for (; i
< nMid
; i
++)
690 Unserialize(is
, v
[i
]);
694 template<typename Stream
, typename T
, typename A
>
695 inline void Unserialize(Stream
& is
, std::vector
<T
, A
>& v
)
697 Unserialize_impl(is
, v
, T());
705 template<typename Stream
, typename K
, typename T
>
706 void Serialize(Stream
& os
, const std::pair
<K
, T
>& item
)
708 Serialize(os
, item
.first
);
709 Serialize(os
, item
.second
);
712 template<typename Stream
, typename K
, typename T
>
713 void Unserialize(Stream
& is
, std::pair
<K
, T
>& item
)
715 Unserialize(is
, item
.first
);
716 Unserialize(is
, item
.second
);
724 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
>
725 void Serialize(Stream
& os
, const std::map
<K
, T
, Pred
, A
>& m
)
727 WriteCompactSize(os
, m
.size());
728 for (typename
std::map
<K
, T
, Pred
, A
>::const_iterator mi
= m
.begin(); mi
!= m
.end(); ++mi
)
729 Serialize(os
, (*mi
));
732 template<typename Stream
, typename K
, typename T
, typename Pred
, typename A
>
733 void Unserialize(Stream
& is
, std::map
<K
, T
, Pred
, A
>& m
)
736 unsigned int nSize
= ReadCompactSize(is
);
737 typename
std::map
<K
, T
, Pred
, A
>::iterator mi
= m
.begin();
738 for (unsigned int i
= 0; i
< nSize
; i
++)
740 std::pair
<K
, T
> item
;
741 Unserialize(is
, item
);
742 mi
= m
.insert(mi
, item
);
751 template<typename Stream
, typename K
, typename Pred
, typename A
>
752 void Serialize(Stream
& os
, const std::set
<K
, Pred
, A
>& m
)
754 WriteCompactSize(os
, m
.size());
755 for (typename
std::set
<K
, Pred
, A
>::const_iterator it
= m
.begin(); it
!= m
.end(); ++it
)
756 Serialize(os
, (*it
));
759 template<typename Stream
, typename K
, typename Pred
, typename A
>
760 void Unserialize(Stream
& is
, std::set
<K
, Pred
, A
>& m
)
763 unsigned int nSize
= ReadCompactSize(is
);
764 typename
std::set
<K
, Pred
, A
>::iterator it
= m
.begin();
765 for (unsigned int i
= 0; i
< nSize
; i
++)
768 Unserialize(is
, key
);
769 it
= m
.insert(it
, key
);
778 template<typename Stream
, typename T
> void
779 Serialize(Stream
& os
, const std::unique_ptr
<const T
>& p
)
784 template<typename Stream
, typename T
>
785 void Unserialize(Stream
& is
, std::unique_ptr
<const T
>& p
)
787 p
.reset(new T(deserialize
, is
));
795 template<typename Stream
, typename T
> void
796 Serialize(Stream
& os
, const std::shared_ptr
<const T
>& p
)
801 template<typename Stream
, typename T
>
802 void Unserialize(Stream
& is
, std::shared_ptr
<const T
>& p
)
804 p
= std::make_shared
<const T
>(deserialize
, is
);
810 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
812 struct CSerActionSerialize
814 constexpr bool ForRead() const { return false; }
816 struct CSerActionUnserialize
818 constexpr bool ForRead() const { return true; }
821 template<typename Stream
, typename T
>
822 inline void SerReadWrite(Stream
& s
, const T
& obj
, CSerActionSerialize ser_action
)
827 template<typename Stream
, typename T
>
828 inline void SerReadWrite(Stream
& s
, T
& obj
, CSerActionUnserialize ser_action
)
830 ::Unserialize(s
, obj
);
841 /* ::GetSerializeSize implementations
843 * Computing the serialized size of objects is done through a special stream
844 * object of type CSizeComputer, which only records the number of bytes written
847 * If your Serialize or SerializationOp method has non-trivial overhead for
848 * serialization, it may be worthwhile to implement a specialized version for
849 * CSizeComputer, which uses the s.seek() method to record bytes that would
850 * be written instead.
860 CSizeComputer(int nTypeIn
, int nVersionIn
) : nSize(0), nType(nTypeIn
), nVersion(nVersionIn
) {}
862 void write(const char *psz
, size_t _nSize
)
864 this->nSize
+= _nSize
;
867 /** Pretend _nSize bytes are written, without specifying them. */
868 void seek(size_t _nSize
)
870 this->nSize
+= _nSize
;
874 CSizeComputer
& operator<<(const T
& obj
)
876 ::Serialize(*this, obj
);
880 size_t size() const {
884 int GetVersion() const { return nVersion
; }
885 int GetType() const { return nType
; }
888 template<typename Stream
>
889 void SerializeMany(Stream
& s
)
893 template<typename Stream
, typename Arg
>
894 void SerializeMany(Stream
& s
, Arg
&& arg
)
896 ::Serialize(s
, std::forward
<Arg
>(arg
));
899 template<typename Stream
, typename Arg
, typename
... Args
>
900 void SerializeMany(Stream
& s
, Arg
&& arg
, Args
&&... args
)
902 ::Serialize(s
, std::forward
<Arg
>(arg
));
903 ::SerializeMany(s
, std::forward
<Args
>(args
)...);
906 template<typename Stream
>
907 inline void UnserializeMany(Stream
& s
)
911 template<typename Stream
, typename Arg
>
912 inline void UnserializeMany(Stream
& s
, Arg
& arg
)
914 ::Unserialize(s
, arg
);
917 template<typename Stream
, typename Arg
, typename
... Args
>
918 inline void UnserializeMany(Stream
& s
, Arg
& arg
, Args
&... args
)
920 ::Unserialize(s
, arg
);
921 ::UnserializeMany(s
, args
...);
924 template<typename Stream
, typename
... Args
>
925 inline void SerReadWriteMany(Stream
& s
, CSerActionSerialize ser_action
, Args
&&... args
)
927 ::SerializeMany(s
, std::forward
<Args
>(args
)...);
930 template<typename Stream
, typename
... Args
>
931 inline void SerReadWriteMany(Stream
& s
, CSerActionUnserialize ser_action
, Args
&... args
)
933 ::UnserializeMany(s
, args
...);
937 inline void WriteVarInt(CSizeComputer
&s
, I n
)
939 s
.seek(GetSizeOfVarInt
<I
>(n
));
942 inline void WriteCompactSize(CSizeComputer
&s
, uint64_t nSize
)
944 s
.seek(GetSizeOfCompactSize(nSize
));
947 template <typename T
>
948 size_t GetSerializeSize(const T
& t
, int nType
, int nVersion
= 0)
950 return (CSizeComputer(nType
, nVersion
) << t
).size();
953 template <typename S
, typename T
>
954 size_t GetSerializeSize(const S
& s
, const T
& t
)
956 return (CSizeComputer(s
.GetType(), s
.GetVersion()) << t
).size();
959 #endif // BITCOIN_SERIALIZE_H