Merge #9911: Wshadow: various gcc fixes
[bitcoinplatinum.git] / src / serialize.h
blobe4d72d2348f1701d0a146eb917af497e915bf669
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"
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);
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)
72 obj = htole16(obj);
73 s.write((char*)&obj, 2);
75 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
77 obj = htole32(obj);
78 s.write((char*)&obj, 4);
80 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
82 obj = htole64(obj);
83 s.write((char*)&obj, 8);
85 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
87 uint8_t obj;
88 s.read((char*)&obj, 1);
89 return obj;
91 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
93 uint16_t obj;
94 s.read((char*)&obj, 2);
95 return le16toh(obj);
97 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
99 uint32_t obj;
100 s.read((char*)&obj, 4);
101 return le32toh(obj);
103 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
105 uint64_t obj;
106 s.read((char*)&obj, 8);
107 return le64toh(obj);
109 inline uint64_t ser_double_to_uint64(double x)
111 union { double x; uint64_t y; } tmp;
112 tmp.x = x;
113 return tmp.y;
115 inline uint32_t ser_float_to_uint32(float x)
117 union { float x; uint32_t y; } tmp;
118 tmp.x = x;
119 return tmp.y;
121 inline double ser_uint64_to_double(uint64_t y)
123 union { double x; uint64_t y; } tmp;
124 tmp.y = y;
125 return tmp.x;
127 inline float ser_uint32_to_float(uint32_t y)
129 union { float x; uint32_t y; } tmp;
130 tmp.y = y;
131 return tmp.x;
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)
141 class CSizeComputer;
143 enum
145 // primary actions
146 SER_NETWORK = (1 << 0),
147 SER_DISK = (1 << 1),
148 SER_GETHASH = (1 << 2),
151 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
152 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
154 /**
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
158 * added as members.
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; }
203 * Compact Size
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)
222 if (nSize < 253)
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);
236 else
238 ser_writedata8(os, 255);
239 ser_writedata64(os, nSize);
241 return;
244 template<typename Stream>
245 uint64_t ReadCompactSize(Stream& is)
247 uint8_t chSize = ser_readdata8(is);
248 uint64_t nSizeRet = 0;
249 if (chSize < 253)
251 nSizeRet = chSize;
253 else if (chSize == 253)
255 nSizeRet = ser_readdata16(is);
256 if (nSizeRet < 253)
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()");
265 else
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");
273 return nSizeRet;
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))
285 * Properties:
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]
300 template<typename I>
301 inline unsigned int GetSizeOfVarInt(I n)
303 int nRet = 0;
304 while(true) {
305 nRet++;
306 if (n <= 0x7F)
307 break;
308 n = (n >> 7) - 1;
310 return nRet;
313 template<typename I>
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];
320 int len=0;
321 while(true) {
322 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
323 if (n <= 0x7F)
324 break;
325 n = (n >> 7) - 1;
326 len++;
328 do {
329 ser_writedata8(os, tmp[len]);
330 } while(len--);
333 template<typename Stream, typename I>
334 I ReadVarInt(Stream& is)
336 I n = 0;
337 while(true) {
338 unsigned char chData = ser_readdata8(is);
339 n = (n << 7) | (chData & 0x7F);
340 if (chData & 0x80)
341 n++;
342 else
343 return n;
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)))
352 /**
353 * Wrapper for serializing arrays and POD.
355 class CFlatData
357 protected:
358 char* pbegin;
359 char* pend;
360 public:
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);
392 template<typename I>
393 class CVarInt
395 protected:
396 I &n;
397 public:
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);
411 class CCompactSize
413 protected:
414 uint64_t &n;
415 public:
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>
430 class LimitedString
432 protected:
433 std::string& string;
434 public:
435 LimitedString(std::string& _string) : string(_string) {}
437 template<typename Stream>
438 void Unserialize(Stream& s)
440 size_t size = ReadCompactSize(s);
441 if (size > Limit) {
442 throw std::ios_base::failure("String length limit exceeded");
444 string.resize(size);
445 if (size != 0)
446 s.read((char*)&string[0], size);
449 template<typename Stream>
450 void Serialize(Stream& s) const
452 WriteCompactSize(s, string.size());
453 if (!string.empty())
454 s.write((char*)&string[0], string.size());
458 template<typename I>
459 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
462 * Forward declarations
466 * string
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);
472 * prevector
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);
483 * vector
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);
494 * pair
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);
500 * map
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);
506 * set
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);
512 * shared_ptr
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);
518 * unique_ptr
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)
531 a.Serialize(os);
534 template<typename Stream, typename T>
535 inline void Unserialize(Stream& is, T& a)
537 a.Unserialize(is);
545 * string
547 template<typename Stream, typename C>
548 void Serialize(Stream& os, const std::basic_string<C>& str)
550 WriteCompactSize(os, str.size());
551 if (!str.empty())
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);
559 str.resize(nSize);
560 if (nSize != 0)
561 is.read((char*)&str[0], nSize * sizeof(str[0]));
567 * prevector
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());
573 if (!v.empty())
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
596 v.clear();
597 unsigned int nSize = ReadCompactSize(is);
598 unsigned int i = 0;
599 while (i < nSize)
601 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
602 v.resize(i + blk);
603 is.read((char*)&v[i], blk * sizeof(T));
604 i += blk;
608 template<typename Stream, unsigned int N, typename T, typename V>
609 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
611 v.clear();
612 unsigned int nSize = ReadCompactSize(is);
613 unsigned int i = 0;
614 unsigned int nMid = 0;
615 while (nMid < nSize)
617 nMid += 5000000 / sizeof(T);
618 if (nMid > nSize)
619 nMid = nSize;
620 v.resize(nMid);
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());
635 * vector
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());
641 if (!v.empty())
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
664 v.clear();
665 unsigned int nSize = ReadCompactSize(is);
666 unsigned int i = 0;
667 while (i < nSize)
669 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
670 v.resize(i + blk);
671 is.read((char*)&v[i], blk * sizeof(T));
672 i += blk;
676 template<typename Stream, typename T, typename A, typename V>
677 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
679 v.clear();
680 unsigned int nSize = ReadCompactSize(is);
681 unsigned int i = 0;
682 unsigned int nMid = 0;
683 while (nMid < nSize)
685 nMid += 5000000 / sizeof(T);
686 if (nMid > nSize)
687 nMid = nSize;
688 v.resize(nMid);
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());
703 * pair
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);
722 * map
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)
735 m.clear();
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);
749 * set
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)
762 m.clear();
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++)
767 K key;
768 Unserialize(is, key);
769 it = m.insert(it, key);
776 * unique_ptr
778 template<typename Stream, typename T> void
779 Serialize(Stream& os, const std::unique_ptr<const T>& p)
781 Serialize(os, *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));
793 * shared_ptr
795 template<typename Stream, typename T> void
796 Serialize(Stream& os, const std::shared_ptr<const T>& p)
798 Serialize(os, *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)
824 ::Serialize(s, obj);
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
845 * to it.
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.
852 class CSizeComputer
854 protected:
855 size_t nSize;
857 const int nType;
858 const int nVersion;
859 public:
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;
873 template<typename T>
874 CSizeComputer& operator<<(const T& obj)
876 ::Serialize(*this, obj);
877 return (*this);
880 size_t size() const {
881 return nSize;
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...);
936 template<typename I>
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