[Wallet] Bugfix: FRT: don't terminate when keypool is empty
[bitcoinplatinum.git] / src / serialize.h
blobe28ca548c081880611416717366202293a297f06
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
9 #include "compat/endian.h"
11 #include <algorithm>
12 #include <assert.h>
13 #include <ios>
14 #include <limits>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <stdint.h>
19 #include <string>
20 #include <string.h>
21 #include <utility>
22 #include <vector>
24 #include "prevector.h"
26 static const unsigned int MAX_SIZE = 0x02000000;
28 /**
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 {};
42 /**
43 * Used to bypass the rule against non-const reference to temporary
44 * where it makes sense with wrappers such as CFlatData or CTxDB
46 template<typename T>
47 inline T& REF(const T& val)
49 return const_cast<T&>(val);
52 /**
53 * Used to acquire a non-const pointer "this" to generate bodies
54 * of const serialization operations from a template
56 template<typename T>
57 inline T* NCONST_PTR(const T* val)
59 return const_cast<T*>(val);
62 /**
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.
69 template <typename V>
70 inline typename V::value_type* begin_ptr(V& v)
72 return v.data();
74 template <typename V>
75 inline const typename V::value_type* begin_ptr(const V& v)
77 return v.data();
79 template <typename V>
80 inline typename V::value_type* end_ptr(V& v)
82 return v.data() + v.size();
84 template <typename V>
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)
100 obj = htole16(obj);
101 s.write((char*)&obj, 2);
103 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
105 obj = htole32(obj);
106 s.write((char*)&obj, 4);
108 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
110 obj = htole64(obj);
111 s.write((char*)&obj, 8);
113 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
115 uint8_t obj;
116 s.read((char*)&obj, 1);
117 return obj;
119 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
121 uint16_t obj;
122 s.read((char*)&obj, 2);
123 return le16toh(obj);
125 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
127 uint32_t obj;
128 s.read((char*)&obj, 4);
129 return le32toh(obj);
131 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
133 uint64_t obj;
134 s.read((char*)&obj, 8);
135 return le64toh(obj);
137 inline uint64_t ser_double_to_uint64(double x)
139 union { double x; uint64_t y; } tmp;
140 tmp.x = x;
141 return tmp.y;
143 inline uint32_t ser_float_to_uint32(float x)
145 union { float x; uint32_t y; } tmp;
146 tmp.x = x;
147 return tmp.y;
149 inline double ser_uint64_to_double(uint64_t y)
151 union { double x; uint64_t y; } tmp;
152 tmp.y = y;
153 return tmp.x;
155 inline float ser_uint32_to_float(uint32_t y)
157 union { float x; uint32_t y; } tmp;
158 tmp.y = y;
159 return tmp.x;
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)
169 class CSizeComputer;
171 enum
173 // primary actions
174 SER_NETWORK = (1 << 0),
175 SER_DISK = (1 << 1),
176 SER_GETHASH = (1 << 2),
179 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
180 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
182 /**
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
186 * added as members.
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; }
231 * Compact Size
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)
250 if (nSize < 253)
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);
264 else
266 ser_writedata8(os, 255);
267 ser_writedata64(os, nSize);
269 return;
272 template<typename Stream>
273 uint64_t ReadCompactSize(Stream& is)
275 uint8_t chSize = ser_readdata8(is);
276 uint64_t nSizeRet = 0;
277 if (chSize < 253)
279 nSizeRet = chSize;
281 else if (chSize == 253)
283 nSizeRet = ser_readdata16(is);
284 if (nSizeRet < 253)
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()");
293 else
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");
301 return nSizeRet;
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))
313 * Properties:
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]
328 template<typename I>
329 inline unsigned int GetSizeOfVarInt(I n)
331 int nRet = 0;
332 while(true) {
333 nRet++;
334 if (n <= 0x7F)
335 break;
336 n = (n >> 7) - 1;
338 return nRet;
341 template<typename I>
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];
348 int len=0;
349 while(true) {
350 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
351 if (n <= 0x7F)
352 break;
353 n = (n >> 7) - 1;
354 len++;
356 do {
357 ser_writedata8(os, tmp[len]);
358 } while(len--);
361 template<typename Stream, typename I>
362 I ReadVarInt(Stream& is)
364 I n = 0;
365 while(true) {
366 unsigned char chData = ser_readdata8(is);
367 n = (n << 7) | (chData & 0x7F);
368 if (chData & 0x80)
369 n++;
370 else
371 return n;
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)))
380 /**
381 * Wrapper for serializing arrays and POD.
383 class CFlatData
385 protected:
386 char* pbegin;
387 char* pend;
388 public:
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);
420 template<typename I>
421 class CVarInt
423 protected:
424 I &n;
425 public:
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);
439 class CCompactSize
441 protected:
442 uint64_t &n;
443 public:
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>
458 class LimitedString
460 protected:
461 std::string& string;
462 public:
463 LimitedString(std::string& _string) : string(_string) {}
465 template<typename Stream>
466 void Unserialize(Stream& s)
468 size_t size = ReadCompactSize(s);
469 if (size > Limit) {
470 throw std::ios_base::failure("String length limit exceeded");
472 string.resize(size);
473 if (size != 0)
474 s.read((char*)&string[0], size);
477 template<typename Stream>
478 void Serialize(Stream& s) const
480 WriteCompactSize(s, string.size());
481 if (!string.empty())
482 s.write((char*)&string[0], string.size());
486 template<typename I>
487 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
490 * Forward declarations
494 * string
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);
500 * prevector
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);
511 * vector
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);
522 * pair
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);
528 * map
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);
534 * set
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);
540 * shared_ptr
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);
546 * unique_ptr
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)
559 a.Serialize(os);
562 template<typename Stream, typename T>
563 inline void Unserialize(Stream& is, T& a)
565 a.Unserialize(is);
573 * string
575 template<typename Stream, typename C>
576 void Serialize(Stream& os, const std::basic_string<C>& str)
578 WriteCompactSize(os, str.size());
579 if (!str.empty())
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);
587 str.resize(nSize);
588 if (nSize != 0)
589 is.read((char*)&str[0], nSize * sizeof(str[0]));
595 * prevector
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());
601 if (!v.empty())
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
624 v.clear();
625 unsigned int nSize = ReadCompactSize(is);
626 unsigned int i = 0;
627 while (i < nSize)
629 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
630 v.resize(i + blk);
631 is.read((char*)&v[i], blk * sizeof(T));
632 i += blk;
636 template<typename Stream, unsigned int N, typename T, typename V>
637 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
639 v.clear();
640 unsigned int nSize = ReadCompactSize(is);
641 unsigned int i = 0;
642 unsigned int nMid = 0;
643 while (nMid < nSize)
645 nMid += 5000000 / sizeof(T);
646 if (nMid > nSize)
647 nMid = nSize;
648 v.resize(nMid);
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());
663 * vector
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());
669 if (!v.empty())
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
692 v.clear();
693 unsigned int nSize = ReadCompactSize(is);
694 unsigned int i = 0;
695 while (i < nSize)
697 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
698 v.resize(i + blk);
699 is.read((char*)&v[i], blk * sizeof(T));
700 i += blk;
704 template<typename Stream, typename T, typename A, typename V>
705 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
707 v.clear();
708 unsigned int nSize = ReadCompactSize(is);
709 unsigned int i = 0;
710 unsigned int nMid = 0;
711 while (nMid < nSize)
713 nMid += 5000000 / sizeof(T);
714 if (nMid > nSize)
715 nMid = nSize;
716 v.resize(nMid);
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());
731 * pair
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);
750 * map
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)
763 m.clear();
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);
777 * set
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)
790 m.clear();
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++)
795 K key;
796 Unserialize(is, key);
797 it = m.insert(it, key);
804 * unique_ptr
806 template<typename Stream, typename T> void
807 Serialize(Stream& os, const std::unique_ptr<const T>& p)
809 Serialize(os, *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));
821 * shared_ptr
823 template<typename Stream, typename T> void
824 Serialize(Stream& os, const std::shared_ptr<const T>& p)
826 Serialize(os, *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)
852 ::Serialize(s, obj);
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
873 * to it.
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.
880 class CSizeComputer
882 protected:
883 size_t nSize;
885 const int nType;
886 const int nVersion;
887 public:
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;
901 template<typename T>
902 CSizeComputer& operator<<(const T& obj)
904 ::Serialize(*this, obj);
905 return (*this);
908 size_t size() const {
909 return nSize;
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...);
964 template<typename I>
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