1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
9 #include "compat/endian.h"
26 #include <boost/optional.hpp>
28 #include "prevector.h"
30 static const unsigned int MAX_SIZE = 0x02000000;
33 * Dummy data type to identify deserializing constructors.
35 * By convention, a constructor of a type T with signature
37 * template <typename Stream> T::T(deserialize_type, Stream& s)
39 * is a deserializing constructor, which builds the type by
40 * deserializing it from s. If T contains const fields, this
41 * is likely the only way to do so.
43 struct deserialize_type {};
44 constexpr deserialize_type deserialize {};
47 * Used to bypass the rule against non-const reference to temporary
48 * where it makes sense with wrappers such as CFlatData or CTxDB
51 inline T& REF(const T& val)
53 return const_cast<T&>(val);
57 * Used to acquire a non-const pointer "this" to generate bodies
58 * of const serialization operations from a template
61 inline T* NCONST_PTR(const T* val)
63 return const_cast<T*>(val);
67 * Get begin pointer of vector (non-const version).
68 * @note These functions avoid the undefined case of indexing into an empty
69 * vector, as well as that of indexing after the end of the vector.
72 inline typename V::value_type* begin_ptr(V& v)
74 return v.empty() ? NULL : &v[0];
76 /** Get begin pointer of vector (const version) */
78 inline const typename V::value_type* begin_ptr(const V& v)
80 return v.empty() ? NULL : &v[0];
82 /** Get end pointer of vector (non-const version) */
84 inline typename V::value_type* end_ptr(V& v)
86 return v.empty() ? NULL : (&v[0] + v.size());
88 /** Get end pointer of vector (const version) */
90 inline const typename V::value_type* end_ptr(const V& v)
92 return v.empty() ? NULL : (&v[0] + v.size());
96 * Lowest-level serialization and conversion.
97 * @note Sizes of these types are verified in the tests
99 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
101 s.write((char*)&obj, 1);
103 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
106 s.write((char*)&obj, 2);
108 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
111 s.write((char*)&obj, 4);
113 template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
116 s.write((char*)&obj, 4);
118 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
121 s.write((char*)&obj, 8);
123 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
126 s.read((char*)&obj, 1);
129 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
132 s.read((char*)&obj, 2);
135 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
138 s.read((char*)&obj, 4);
141 template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
144 s.read((char*)&obj, 4);
147 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
150 s.read((char*)&obj, 8);
153 inline uint64_t ser_double_to_uint64(double x)
155 union { double x; uint64_t y; } tmp;
159 inline uint32_t ser_float_to_uint32(float x)
161 union { float x; uint32_t y; } tmp;
165 inline double ser_uint64_to_double(uint64_t y)
167 union { double x; uint64_t y; } tmp;
171 inline float ser_uint32_to_float(uint32_t y)
173 union { float x; uint32_t y; } tmp;
179 /////////////////////////////////////////////////////////////////
181 // Templates for serializing to anything that looks like a stream,
182 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
190 SER_NETWORK = (1 << 0),
192 SER_GETHASH = (1 << 2),
195 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
196 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
199 * Implement three methods for serializable objects. These are actually wrappers over
200 * "SerializationOp" template, which implements the body of each class' serialization
201 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
204 #define ADD_SERIALIZE_METHODS \
205 template<typename Stream> \
206 void Serialize(Stream& s) const { \
207 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
209 template<typename Stream> \
210 void Unserialize(Stream& s) { \
211 SerializationOp(s, CSerActionUnserialize()); \
214 template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
215 template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
216 template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
217 template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
218 template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
219 template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
220 template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
221 template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
222 template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
223 template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
224 template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
226 template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
227 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
228 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
229 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
230 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
231 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
232 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
233 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
234 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
235 template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
236 template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
238 template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
239 template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
248 * size < 253 -- 1 byte
249 * size <= 0xFFFF -- 3 bytes (253 + 2 bytes)
250 * size <= 0xFFFFFFFF -- 5 bytes (254 + 4 bytes)
251 * size > 0xFFFFFFFF -- 9 bytes (255 + 8 bytes)
253 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
255 if (nSize < 253) return 1;
256 else if (nSize <= 0xFFFFu) return 3;
257 else if (nSize <= 0xFFFFFFFFu) return 5;
261 inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
263 template<typename Stream>
264 void WriteCompactSize(Stream& os, uint64_t nSize)
268 ser_writedata8(os, nSize);
270 else if (nSize <= 0xFFFFu)
272 ser_writedata8(os, 253);
273 ser_writedata16(os, nSize);
275 else if (nSize <= 0xFFFFFFFFu)
277 ser_writedata8(os, 254);
278 ser_writedata32(os, nSize);
282 ser_writedata8(os, 255);
283 ser_writedata64(os, nSize);
287 template<typename Stream>
288 uint64_t ReadCompactSize(Stream& is)
290 uint8_t chSize = ser_readdata8(is);
291 uint64_t nSizeRet = 0;
296 else if (chSize == 253)
298 nSizeRet = ser_readdata16(is);
300 throw std::ios_base::failure("non-canonical ReadCompactSize()");
302 else if (chSize == 254)
304 nSizeRet = ser_readdata32(is);
305 if (nSizeRet < 0x10000u)
306 throw std::ios_base::failure("non-canonical ReadCompactSize()");
310 nSizeRet = ser_readdata64(is);
311 if (nSizeRet < 0x100000000ULL)
312 throw std::ios_base::failure("non-canonical ReadCompactSize()");
314 if (nSizeRet > (uint64_t)MAX_SIZE)
315 throw std::ios_base::failure("ReadCompactSize(): size too large");
320 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
321 * The high bit in each byte signifies whether another digit follows. To make
322 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
323 * Thus, the byte sequence a[] with length len, where all but the last byte
324 * has bit 128 set, encodes the number:
326 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
329 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
330 * * Every integer has exactly one encoding
331 * * Encoding does not depend on size of original integer type
332 * * No redundancy: every (infinite) byte sequence corresponds to a list
333 * of encoded integers.
335 * 0: [0x00] 256: [0x81 0x00]
336 * 1: [0x01] 16383: [0xFE 0x7F]
337 * 127: [0x7F] 16384: [0xFF 0x00]
338 * 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F]
339 * 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F]
340 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
344 inline unsigned int GetSizeOfVarInt(I n)
357 inline void WriteVarInt(CSizeComputer& os, I n);
359 template<typename Stream, typename I>
360 void WriteVarInt(Stream& os, I n)
362 unsigned char tmp[(sizeof(n)*8+6)/7];
365 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
372 ser_writedata8(os, tmp[len]);
376 template<typename Stream, typename I>
377 I ReadVarInt(Stream& is)
381 unsigned char chData = ser_readdata8(is);
382 n = (n << 7) | (chData & 0x7F);
390 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
391 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
392 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
393 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
396 * Wrapper for serializing arrays and POD.
404 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
405 template <class T, class TAl>
406 explicit CFlatData(std::vector<T,TAl> &v)
408 pbegin = (char*)begin_ptr(v);
409 pend = (char*)end_ptr(v);
411 template <unsigned int N, typename T, typename S, typename D>
412 explicit CFlatData(prevector<N, T, S, D> &v)
414 pbegin = (char*)begin_ptr(v);
415 pend = (char*)end_ptr(v);
417 char* begin() { return pbegin; }
418 const char* begin() const { return pbegin; }
419 char* end() { return pend; }
420 const char* end() const { return pend; }
422 template<typename Stream>
423 void Serialize(Stream& s) const
425 s.write(pbegin, pend - pbegin);
428 template<typename Stream>
429 void Unserialize(Stream& s)
431 s.read(pbegin, pend - pbegin);
441 CVarInt(I& nIn) : n(nIn) { }
443 template<typename Stream>
444 void Serialize(Stream &s) const {
445 WriteVarInt<Stream,I>(s, n);
448 template<typename Stream>
449 void Unserialize(Stream& s) {
450 n = ReadVarInt<Stream,I>(s);
459 CCompactSize(uint64_t& nIn) : n(nIn) { }
461 template<typename Stream>
462 void Serialize(Stream &s) const {
463 WriteCompactSize<Stream>(s, n);
466 template<typename Stream>
467 void Unserialize(Stream& s) {
468 n = ReadCompactSize<Stream>(s);
472 template<size_t Limit>
478 LimitedString(std::string& _string) : string(_string) {}
480 template<typename Stream>
481 void Unserialize(Stream& s)
483 size_t size = ReadCompactSize(s);
485 throw std::ios_base::failure("String length limit exceeded");
489 s.read((char*)&string[0], size);
492 template<typename Stream>
493 void Serialize(Stream& s) const
495 WriteCompactSize(s, string.size());
497 s.write((char*)&string[0], string.size());
502 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
505 * Forward declarations
511 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
512 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
516 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
518 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
519 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
520 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
521 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
522 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
523 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
527 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
529 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
530 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
531 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
532 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
533 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
534 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
539 template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item);
540 template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item);
545 template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const std::array<T, N>& item);
546 template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, std::array<T, N>& item);
551 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
552 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
557 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
558 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
563 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
564 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
569 template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m);
570 template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m);
575 template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
576 template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
581 template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
582 template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
587 * If none of the specialized versions above matched, default to calling member function.
589 template<typename Stream, typename T>
590 inline void Serialize(Stream& os, const T& a)
595 template<typename Stream, typename T>
596 inline void Unserialize(Stream& is, T& a)
608 template<typename Stream, typename C>
609 void Serialize(Stream& os, const std::basic_string<C>& str)
611 WriteCompactSize(os, str.size());
613 os.write((char*)&str[0], str.size() * sizeof(str[0]));
616 template<typename Stream, typename C>
617 void Unserialize(Stream& is, std::basic_string<C>& str)
619 unsigned int nSize = ReadCompactSize(is);
622 is.read((char*)&str[0], nSize * sizeof(str[0]));
630 template<typename Stream, unsigned int N, typename T>
631 void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
633 WriteCompactSize(os, v.size());
635 os.write((char*)&v[0], v.size() * sizeof(T));
638 template<typename Stream, unsigned int N, typename T, typename V>
639 void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
641 WriteCompactSize(os, v.size());
642 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
643 ::Serialize(os, (*vi));
646 template<typename Stream, unsigned int N, typename T>
647 inline void Serialize(Stream& os, const prevector<N, T>& v)
649 Serialize_impl(os, v, T());
653 template<typename Stream, unsigned int N, typename T>
654 void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
656 // Limit size per read so bogus size value won't cause out of memory
658 unsigned int nSize = ReadCompactSize(is);
662 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
664 is.read((char*)&v[i], blk * sizeof(T));
669 template<typename Stream, unsigned int N, typename T, typename V>
670 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
673 unsigned int nSize = ReadCompactSize(is);
675 unsigned int nMid = 0;
678 nMid += 5000000 / sizeof(T);
682 for (; i < nMid; i++)
683 Unserialize(is, v[i]);
687 template<typename Stream, unsigned int N, typename T>
688 inline void Unserialize(Stream& is, prevector<N, T>& v)
690 Unserialize_impl(is, v, T());
698 template<typename Stream, typename T, typename A>
699 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
701 WriteCompactSize(os, v.size());
703 os.write((char*)&v[0], v.size() * sizeof(T));
706 template<typename Stream, typename T, typename A, typename V>
707 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
709 WriteCompactSize(os, v.size());
710 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
711 ::Serialize(os, (*vi));
714 template<typename Stream, typename T, typename A>
715 inline void Serialize(Stream& os, const std::vector<T, A>& v)
717 Serialize_impl(os, v, T());
721 template<typename Stream, typename T, typename A>
722 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
724 // Limit size per read so bogus size value won't cause out of memory
726 unsigned int nSize = ReadCompactSize(is);
730 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
732 is.read((char*)&v[i], blk * sizeof(T));
737 template<typename Stream, typename T, typename A, typename V>
738 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
741 unsigned int nSize = ReadCompactSize(is);
743 unsigned int nMid = 0;
746 nMid += 5000000 / sizeof(T);
750 for (; i < nMid; i++)
751 Unserialize(is, v[i]);
755 template<typename Stream, typename T, typename A>
756 inline void Unserialize(Stream& is, std::vector<T, A>& v)
758 Unserialize_impl(is, v, T());
766 template<typename Stream, typename T>
767 void Serialize(Stream& os, const boost::optional<T>& item)
769 // If the value is there, put 0x01 and then serialize the value.
770 // If it's not, put 0x00.
772 unsigned char discriminant = 0x01;
773 Serialize(os, discriminant);
774 Serialize(os, *item);
776 unsigned char discriminant = 0x00;
777 Serialize(os, discriminant);
781 template<typename Stream, typename T>
782 void Unserialize(Stream& is, boost::optional<T>& item)
784 unsigned char discriminant = 0x00;
785 Unserialize(is, discriminant);
787 if (discriminant == 0x00) {
789 } else if (discriminant == 0x01) {
791 Unserialize(is, object);
794 throw std::ios_base::failure("non-canonical optional discriminant");
803 template<typename Stream, typename T, std::size_t N>
804 void Serialize(Stream& os, const std::array<T, N>& item)
806 for (size_t i = 0; i < N; i++) {
807 Serialize(os, item[i]);
811 template<typename Stream, typename T, std::size_t N>
812 void Unserialize(Stream& is, std::array<T, N>& item)
814 for (size_t i = 0; i < N; i++) {
815 Unserialize(is, item[i]);
823 template<typename Stream, typename K, typename T>
824 void Serialize(Stream& os, const std::pair<K, T>& item)
826 Serialize(os, item.first);
827 Serialize(os, item.second);
830 template<typename Stream, typename K, typename T>
831 void Unserialize(Stream& is, std::pair<K, T>& item)
833 Unserialize(is, item.first);
834 Unserialize(is, item.second);
842 template<typename Stream, typename K, typename T, typename Pred, typename A>
843 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
845 WriteCompactSize(os, m.size());
846 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
847 Serialize(os, (*mi));
850 template<typename Stream, typename K, typename T, typename Pred, typename A>
851 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
854 unsigned int nSize = ReadCompactSize(is);
855 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
856 for (unsigned int i = 0; i < nSize; i++)
858 std::pair<K, T> item;
859 Unserialize(is, item);
860 mi = m.insert(mi, item);
869 template<typename Stream, typename K, typename Pred, typename A>
870 void Serialize(Stream& os, const std::set<K, Pred, A>& m)
872 WriteCompactSize(os, m.size());
873 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
874 Serialize(os, (*it));
877 template<typename Stream, typename K, typename Pred, typename A>
878 void Unserialize(Stream& is, std::set<K, Pred, A>& m)
881 unsigned int nSize = ReadCompactSize(is);
882 typename std::set<K, Pred, A>::iterator it = m.begin();
883 for (unsigned int i = 0; i < nSize; i++)
886 Unserialize(is, key);
887 it = m.insert(it, key);
896 template<typename Stream, typename T, typename A>
897 void Serialize(Stream& os, const std::list<T, A>& l)
899 WriteCompactSize(os, l.size());
900 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
901 Serialize(os, (*it));
904 template<typename Stream, typename T, typename A>
905 void Unserialize(Stream& is, std::list<T, A>& l)
908 unsigned int nSize = ReadCompactSize(is);
909 typename std::list<T, A>::iterator it = l.begin();
910 for (unsigned int i = 0; i < nSize; i++)
913 Unserialize(is, item);
923 template<typename Stream, typename T> void
924 Serialize(Stream& os, const std::unique_ptr<const T>& p)
929 template<typename Stream, typename T>
930 void Unserialize(Stream& is, std::unique_ptr<const T>& p)
932 p.reset(new T(deserialize, is));
940 template<typename Stream, typename T> void
941 Serialize(Stream& os, const std::shared_ptr<const T>& p)
946 template<typename Stream, typename T>
947 void Unserialize(Stream& is, std::shared_ptr<const T>& p)
949 p = std::make_shared<const T>(deserialize, is);
955 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
957 struct CSerActionSerialize
959 constexpr bool ForRead() const { return false; }
961 struct CSerActionUnserialize
963 constexpr bool ForRead() const { return true; }
966 template<typename Stream, typename T>
967 inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
972 template<typename Stream, typename T>
973 inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
975 ::Unserialize(s, obj);
986 /* ::GetSerializeSize implementations
988 * Computing the serialized size of objects is done through a special stream
989 * object of type CSizeComputer, which only records the number of bytes written
992 * If your Serialize or SerializationOp method has non-trivial overhead for
993 * serialization, it may be worthwhile to implement a specialized version for
994 * CSizeComputer, which uses the s.seek() method to record bytes that would
995 * be written instead.
1005 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
1007 void write(const char *psz, size_t _nSize)
1009 this->nSize += _nSize;
1012 /** Pretend _nSize bytes are written, without specifying them. */
1013 void seek(size_t _nSize)
1015 this->nSize += _nSize;
1018 template<typename T>
1019 CSizeComputer& operator<<(const T& obj)
1021 ::Serialize(*this, obj);
1025 size_t size() const {
1029 int GetVersion() const { return nVersion; }
1030 int GetType() const { return nType; }
1033 template<typename Stream>
1034 void SerializeMany(Stream& s)
1038 template<typename Stream, typename Arg>
1039 void SerializeMany(Stream& s, Arg&& arg)
1041 ::Serialize(s, std::forward<Arg>(arg));
1044 template<typename Stream, typename Arg, typename... Args>
1045 void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
1047 ::Serialize(s, std::forward<Arg>(arg));
1048 ::SerializeMany(s, std::forward<Args>(args)...);
1051 template<typename Stream>
1052 inline void UnserializeMany(Stream& s)
1056 template<typename Stream, typename Arg>
1057 inline void UnserializeMany(Stream& s, Arg& arg)
1059 ::Unserialize(s, arg);
1062 template<typename Stream, typename Arg, typename... Args>
1063 inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
1065 ::Unserialize(s, arg);
1066 ::UnserializeMany(s, args...);
1069 template<typename Stream, typename... Args>
1070 inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
1072 ::SerializeMany(s, std::forward<Args>(args)...);
1075 template<typename Stream, typename... Args>
1076 inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
1078 ::UnserializeMany(s, args...);
1081 template<typename I>
1082 inline void WriteVarInt(CSizeComputer &s, I n)
1084 s.seek(GetSizeOfVarInt<I>(n));
1087 inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
1089 s.seek(GetSizeOfCompactSize(nSize));
1092 template <typename T>
1093 size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
1095 return (CSizeComputer(nType, nVersion) << t).size();
1098 template <typename S, typename T>
1099 size_t GetSerializeSize(const S& s, const T& t)
1101 return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
1104 #endif // BITCOIN_SERIALIZE_H