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 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 <boost/array.hpp>
25 #include <boost/optional.hpp>
29 static const unsigned int MAX_SIZE = 0x02000000;
32 * Used to bypass the rule against non-const reference to temporary
33 * where it makes sense with wrappers such as CFlatData or CTxDB
36 inline T& REF(const T& val)
38 return const_cast<T&>(val);
42 * Used to acquire a non-const pointer "this" to generate bodies
43 * of const serialization operations from a template
46 inline T* NCONST_PTR(const T* val)
48 return const_cast<T*>(val);
52 * Get begin pointer of vector (non-const version).
53 * @note These functions avoid the undefined case of indexing into an empty
54 * vector, as well as that of indexing after the end of the vector.
56 template <class T, class TAl>
57 inline T* begin_ptr(std::vector<T,TAl>& v)
59 return v.empty() ? NULL : &v[0];
61 /** Get begin pointer of vector (const version) */
62 template <class T, class TAl>
63 inline const T* begin_ptr(const std::vector<T,TAl>& v)
65 return v.empty() ? NULL : &v[0];
67 /** Get end pointer of vector (non-const version) */
68 template <class T, class TAl>
69 inline T* end_ptr(std::vector<T,TAl>& v)
71 return v.empty() ? NULL : (&v[0] + v.size());
73 /** Get end pointer of vector (const version) */
74 template <class T, class TAl>
75 inline const T* end_ptr(const std::vector<T,TAl>& v)
77 return v.empty() ? NULL : (&v[0] + v.size());
81 * Lowest-level serialization and conversion.
82 * @note Sizes of these types are verified in the tests
84 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
86 s.write((char*)&obj, 1);
88 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
91 s.write((char*)&obj, 2);
93 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
96 s.write((char*)&obj, 4);
98 template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
101 s.write((char*)&obj, 4);
103 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
106 s.write((char*)&obj, 8);
108 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
111 s.read((char*)&obj, 1);
114 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
117 s.read((char*)&obj, 2);
120 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
123 s.read((char*)&obj, 4);
126 template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
129 s.read((char*)&obj, 4);
132 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
135 s.read((char*)&obj, 8);
138 inline uint64_t ser_double_to_uint64(double x)
140 union { double x; uint64_t y; } tmp;
144 inline uint32_t ser_float_to_uint32(float x)
146 union { float x; uint32_t y; } tmp;
150 inline double ser_uint64_to_double(uint64_t y)
152 union { double x; uint64_t y; } tmp;
156 inline float ser_uint32_to_float(uint32_t y)
158 union { float x; uint32_t y; } tmp;
164 /////////////////////////////////////////////////////////////////
166 // Templates for serializing to anything that looks like a stream,
167 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
173 SER_NETWORK = (1 << 0),
175 SER_GETHASH = (1 << 2),
178 #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
181 * Implement three methods for serializable objects. These are actually wrappers over
182 * "SerializationOp" template, which implements the body of each class' serialization
183 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
186 #define ADD_SERIALIZE_METHODS \
187 size_t GetSerializeSize(int nType, int nVersion) const { \
188 CSizeComputer s(nType, nVersion); \
189 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
192 template<typename Stream> \
193 void Serialize(Stream& s, int nType, int nVersion) const { \
194 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
196 template<typename Stream> \
197 void Unserialize(Stream& s, int nType, int nVersion) { \
198 SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
204 inline unsigned int GetSerializeSize(char a, int, int=0) { return 1; }
205 inline unsigned int GetSerializeSize(int8_t a, int, int=0) { return 1; }
206 inline unsigned int GetSerializeSize(uint8_t a, int, int=0) { return 1; }
207 inline unsigned int GetSerializeSize(int16_t a, int, int=0) { return 2; }
208 inline unsigned int GetSerializeSize(uint16_t a, int, int=0) { return 2; }
209 inline unsigned int GetSerializeSize(int32_t a, int, int=0) { return 4; }
210 inline unsigned int GetSerializeSize(uint32_t a, int, int=0) { return 4; }
211 inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return 8; }
212 inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return 8; }
213 inline unsigned int GetSerializeSize(float a, int, int=0) { return 4; }
214 inline unsigned int GetSerializeSize(double a, int, int=0) { return 8; }
216 template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
217 template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
218 template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
219 template<typename Stream> inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); }
220 template<typename Stream> inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); }
221 template<typename Stream> inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); }
222 template<typename Stream> inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); }
223 template<typename Stream> inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); }
224 template<typename Stream> inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); }
225 template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
226 template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
228 template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
229 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); }
230 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); }
231 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); }
232 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); }
233 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); }
234 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); }
235 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); }
236 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); }
237 template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
238 template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
240 inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
241 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
242 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
251 * size < 253 -- 1 byte
252 * size <= 0xFFFF -- 3 bytes (253 + 2 bytes)
253 * size <= 0xFFFFFFFF -- 5 bytes (254 + 4 bytes)
254 * size > 0xFFFFFFFF -- 9 bytes (255 + 8 bytes)
256 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
258 if (nSize < 253) return 1;
259 else if (nSize <= 0xFFFFu) return 3;
260 else if (nSize <= 0xFFFFFFFFu) return 5;
264 template<typename Stream>
265 void WriteCompactSize(Stream& os, uint64_t nSize)
269 ser_writedata8(os, nSize);
271 else if (nSize <= 0xFFFFu)
273 ser_writedata8(os, 253);
274 ser_writedata16(os, nSize);
276 else if (nSize <= 0xFFFFFFFFu)
278 ser_writedata8(os, 254);
279 ser_writedata32(os, nSize);
283 ser_writedata8(os, 255);
284 ser_writedata64(os, nSize);
288 template<typename Stream>
289 uint64_t ReadCompactSize(Stream& is)
291 uint8_t chSize = ser_readdata8(is);
292 uint64_t nSizeRet = 0;
297 else if (chSize == 253)
299 nSizeRet = ser_readdata16(is);
301 throw std::ios_base::failure("non-canonical ReadCompactSize()");
303 else if (chSize == 254)
305 nSizeRet = ser_readdata32(is);
306 if (nSizeRet < 0x10000u)
307 throw std::ios_base::failure("non-canonical ReadCompactSize()");
311 nSizeRet = ser_readdata64(is);
312 if (nSizeRet < 0x100000000ULL)
313 throw std::ios_base::failure("non-canonical ReadCompactSize()");
315 if (nSizeRet > (uint64_t)MAX_SIZE)
316 throw std::ios_base::failure("ReadCompactSize(): size too large");
321 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
322 * The high bit in each byte signifies whether another digit follows. To make
323 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
324 * Thus, the byte sequence a[] with length len, where all but the last byte
325 * has bit 128 set, encodes the number:
327 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
330 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
331 * * Every integer has exactly one encoding
332 * * Encoding does not depend on size of original integer type
333 * * No redundancy: every (infinite) byte sequence corresponds to a list
334 * of encoded integers.
336 * 0: [0x00] 256: [0x81 0x00]
337 * 1: [0x01] 16383: [0xFE 0x7F]
338 * 127: [0x7F] 16384: [0xFF 0x00]
339 * 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F]
340 * 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F]
341 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
345 inline unsigned int GetSizeOfVarInt(I n)
357 template<typename Stream, typename I>
358 void WriteVarInt(Stream& os, I n)
360 unsigned char tmp[(sizeof(n)*8+6)/7];
363 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
370 ser_writedata8(os, tmp[len]);
374 template<typename Stream, typename I>
375 I ReadVarInt(Stream& is)
379 unsigned char chData = ser_readdata8(is);
380 n = (n << 7) | (chData & 0x7F);
388 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
389 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
390 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
393 * Wrapper for serializing arrays and POD.
401 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
402 template <class T, class TAl>
403 explicit CFlatData(std::vector<T,TAl> &v)
405 pbegin = (char*)begin_ptr(v);
406 pend = (char*)end_ptr(v);
408 char* begin() { return pbegin; }
409 const char* begin() const { return pbegin; }
410 char* end() { return pend; }
411 const char* end() const { return pend; }
413 unsigned int GetSerializeSize(int, int=0) const
415 return pend - pbegin;
418 template<typename Stream>
419 void Serialize(Stream& s, int, int=0) const
421 s.write(pbegin, pend - pbegin);
424 template<typename Stream>
425 void Unserialize(Stream& s, int, int=0)
427 s.read(pbegin, pend - pbegin);
437 CVarInt(I& nIn) : n(nIn) { }
439 unsigned int GetSerializeSize(int, int) const {
440 return GetSizeOfVarInt<I>(n);
443 template<typename Stream>
444 void Serialize(Stream &s, int, int) const {
445 WriteVarInt<Stream,I>(s, n);
448 template<typename Stream>
449 void Unserialize(Stream& s, int, int) {
450 n = ReadVarInt<Stream,I>(s);
454 template<size_t Limit>
460 LimitedString(std::string& string) : string(string) {}
462 template<typename Stream>
463 void Unserialize(Stream& s, int, int=0)
465 size_t size = ReadCompactSize(s);
467 throw std::ios_base::failure("String length limit exceeded");
471 s.read((char*)&string[0], size);
474 template<typename Stream>
475 void Serialize(Stream& s, int, int=0) const
477 WriteCompactSize(s, string.size());
479 s.write((char*)&string[0], string.size());
482 unsigned int GetSerializeSize(int, int=0) const
484 return GetSizeOfCompactSize(string.size()) + string.size();
489 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
492 * Forward declarations
498 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
499 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
500 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
504 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
506 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
507 template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
508 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
509 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
510 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&);
511 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
512 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
513 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&);
514 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
517 * others derived from vector
519 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion);
520 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
521 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
526 template<typename T> unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion);
527 template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion);
528 template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion);
533 template<typename T, std::size_t N> unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion);
534 template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion);
535 template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion);
540 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
541 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
542 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
547 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
548 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion);
549 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion);
554 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
555 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
556 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
561 template<typename T, typename A> unsigned int GetSerializeSize(const std::list<T, A>& m, int nType, int nVersion);
562 template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m, int nType, int nVersion);
563 template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m, int nType, int nVersion);
570 * If none of the specialized versions above matched, default to calling member function.
571 * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
572 * The compiler will only cast int to long if none of the other templates matched.
573 * Thanks to Boost serialization for this idea.
576 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
578 return a.GetSerializeSize((int)nType, nVersion);
581 template<typename Stream, typename T>
582 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
584 a.Serialize(os, (int)nType, nVersion);
587 template<typename Stream, typename T>
588 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
590 a.Unserialize(is, (int)nType, nVersion);
601 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
603 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
606 template<typename Stream, typename C>
607 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
609 WriteCompactSize(os, str.size());
611 os.write((char*)&str[0], str.size() * sizeof(str[0]));
614 template<typename Stream, typename C>
615 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
617 unsigned int nSize = ReadCompactSize(is);
620 is.read((char*)&str[0], nSize * sizeof(str[0]));
628 template<typename T, typename A>
629 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
631 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
634 template<typename T, typename A, typename V>
635 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
637 unsigned int nSize = GetSizeOfCompactSize(v.size());
638 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
639 nSize += GetSerializeSize((*vi), nType, nVersion);
643 template<typename T, typename A>
644 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
646 return GetSerializeSize_impl(v, nType, nVersion, T());
650 template<typename Stream, typename T, typename A>
651 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
653 WriteCompactSize(os, v.size());
655 os.write((char*)&v[0], v.size() * sizeof(T));
658 template<typename Stream, typename T, typename A, typename V>
659 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
661 WriteCompactSize(os, v.size());
662 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
663 ::Serialize(os, (*vi), nType, nVersion);
666 template<typename Stream, typename T, typename A>
667 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
669 Serialize_impl(os, v, nType, nVersion, T());
673 template<typename Stream, typename T, typename A>
674 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
676 // Limit size per read so bogus size value won't cause out of memory
678 unsigned int nSize = ReadCompactSize(is);
682 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
684 is.read((char*)&v[i], blk * sizeof(T));
689 template<typename Stream, typename T, typename A, typename V>
690 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
693 unsigned int nSize = ReadCompactSize(is);
695 unsigned int nMid = 0;
698 nMid += 5000000 / sizeof(T);
702 for (; i < nMid; i++)
703 Unserialize(is, v[i], nType, nVersion);
707 template<typename Stream, typename T, typename A>
708 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
710 Unserialize_impl(is, v, nType, nVersion, T());
716 * others derived from vector
718 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
720 return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
723 template<typename Stream>
724 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
726 Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
729 template<typename Stream>
730 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
732 Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
741 unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion)
744 return 1 + GetSerializeSize(*item, nType, nVersion);
750 template<typename Stream, typename T>
751 void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion)
753 // If the value is there, put 0x01 and then serialize the value.
754 // If it's not, put 0x00.
756 unsigned char discriminant = 0x01;
757 Serialize(os, discriminant, nType, nVersion);
758 Serialize(os, *item, nType, nVersion);
760 unsigned char discriminant = 0x00;
761 Serialize(os, discriminant, nType, nVersion);
765 template<typename Stream, typename T>
766 void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion)
768 unsigned char discriminant = 0x00;
769 Unserialize(is, discriminant, nType, nVersion);
771 if (discriminant == 0x00) {
773 } else if (discriminant == 0x01) {
775 Unserialize(is, object, nType, nVersion);
778 throw std::ios_base::failure("non-canonical optional discriminant");
787 template<typename T, std::size_t N>
788 unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion)
790 unsigned int size = 0;
791 for (size_t i = 0; i < N; i++) {
792 size += GetSerializeSize(item[0], nType, nVersion);
797 template<typename Stream, typename T, std::size_t N>
798 void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion)
800 for (size_t i = 0; i < N; i++) {
801 Serialize(os, item[i], nType, nVersion);
805 template<typename Stream, typename T, std::size_t N>
806 void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion)
808 for (size_t i = 0; i < N; i++) {
809 Unserialize(is, item[i], nType, nVersion);
817 template<typename K, typename T>
818 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
820 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
823 template<typename Stream, typename K, typename T>
824 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
826 Serialize(os, item.first, nType, nVersion);
827 Serialize(os, item.second, nType, nVersion);
830 template<typename Stream, typename K, typename T>
831 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
833 Unserialize(is, item.first, nType, nVersion);
834 Unserialize(is, item.second, nType, nVersion);
842 template<typename K, typename T, typename Pred, typename A>
843 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
845 unsigned int nSize = GetSizeOfCompactSize(m.size());
846 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
847 nSize += GetSerializeSize((*mi), nType, nVersion);
851 template<typename Stream, typename K, typename T, typename Pred, typename A>
852 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
854 WriteCompactSize(os, m.size());
855 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
856 Serialize(os, (*mi), nType, nVersion);
859 template<typename Stream, typename K, typename T, typename Pred, typename A>
860 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
863 unsigned int nSize = ReadCompactSize(is);
864 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
865 for (unsigned int i = 0; i < nSize; i++)
867 std::pair<K, T> item;
868 Unserialize(is, item, nType, nVersion);
869 mi = m.insert(mi, item);
878 template<typename K, typename Pred, typename A>
879 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
881 unsigned int nSize = GetSizeOfCompactSize(m.size());
882 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
883 nSize += GetSerializeSize((*it), nType, nVersion);
887 template<typename Stream, typename K, typename Pred, typename A>
888 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
890 WriteCompactSize(os, m.size());
891 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
892 Serialize(os, (*it), nType, nVersion);
895 template<typename Stream, typename K, typename Pred, typename A>
896 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
899 unsigned int nSize = ReadCompactSize(is);
900 typename std::set<K, Pred, A>::iterator it = m.begin();
901 for (unsigned int i = 0; i < nSize; i++)
904 Unserialize(is, key, nType, nVersion);
905 it = m.insert(it, key);
914 template<typename T, typename A>
915 unsigned int GetSerializeSize(const std::list<T, A>& l, int nType, int nVersion)
917 unsigned int nSize = GetSizeOfCompactSize(l.size());
918 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
919 nSize += GetSerializeSize((*it), nType, nVersion);
923 template<typename Stream, typename T, typename A>
924 void Serialize(Stream& os, const std::list<T, A>& l, int nType, int nVersion)
926 WriteCompactSize(os, l.size());
927 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
928 Serialize(os, (*it), nType, nVersion);
931 template<typename Stream, typename T, typename A>
932 void Unserialize(Stream& is, std::list<T, A>& l, int nType, int nVersion)
935 unsigned int nSize = ReadCompactSize(is);
936 typename std::list<T, A>::iterator it = l.begin();
937 for (unsigned int i = 0; i < nSize; i++)
940 Unserialize(is, item, nType, nVersion);
948 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
950 struct CSerActionSerialize
952 bool ForRead() const { return false; }
954 struct CSerActionUnserialize
956 bool ForRead() const { return true; }
959 template<typename Stream, typename T>
960 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
962 ::Serialize(s, obj, nType, nVersion);
965 template<typename Stream, typename T>
966 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
968 ::Unserialize(s, obj, nType, nVersion);
988 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
990 CSizeComputer& write(const char *psz, size_t nSize)
992 this->nSize += nSize;
997 CSizeComputer& operator<<(const T& obj)
999 ::Serialize(*this, obj, nType, nVersion);
1003 size_t size() const {
1008 #endif // BITCOIN_SERIALIZE_H