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_writedata64(Stream &s, uint64_t obj)
101 s.write((char*)&obj, 8);
103 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
106 s.read((char*)&obj, 1);
109 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
112 s.read((char*)&obj, 2);
115 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
118 s.read((char*)&obj, 4);
121 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
124 s.read((char*)&obj, 8);
127 inline uint64_t ser_double_to_uint64(double x)
129 union { double x; uint64_t y; } tmp;
133 inline uint32_t ser_float_to_uint32(float x)
135 union { float x; uint32_t y; } tmp;
139 inline double ser_uint64_to_double(uint64_t y)
141 union { double x; uint64_t y; } tmp;
145 inline float ser_uint32_to_float(uint32_t y)
147 union { float x; uint32_t y; } tmp;
153 /////////////////////////////////////////////////////////////////
155 // Templates for serializing to anything that looks like a stream,
156 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
162 SER_NETWORK = (1 << 0),
164 SER_GETHASH = (1 << 2),
167 #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
170 * Implement three methods for serializable objects. These are actually wrappers over
171 * "SerializationOp" template, which implements the body of each class' serialization
172 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
175 #define ADD_SERIALIZE_METHODS \
176 size_t GetSerializeSize(int nType, int nVersion) const { \
177 CSizeComputer s(nType, nVersion); \
178 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
181 template<typename Stream> \
182 void Serialize(Stream& s, int nType, int nVersion) const { \
183 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
185 template<typename Stream> \
186 void Unserialize(Stream& s, int nType, int nVersion) { \
187 SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
193 inline unsigned int GetSerializeSize(char a, int, int=0) { return 1; }
194 inline unsigned int GetSerializeSize(int8_t a, int, int=0) { return 1; }
195 inline unsigned int GetSerializeSize(uint8_t a, int, int=0) { return 1; }
196 inline unsigned int GetSerializeSize(int16_t a, int, int=0) { return 2; }
197 inline unsigned int GetSerializeSize(uint16_t a, int, int=0) { return 2; }
198 inline unsigned int GetSerializeSize(int32_t a, int, int=0) { return 4; }
199 inline unsigned int GetSerializeSize(uint32_t a, int, int=0) { return 4; }
200 inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return 8; }
201 inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return 8; }
202 inline unsigned int GetSerializeSize(float a, int, int=0) { return 4; }
203 inline unsigned int GetSerializeSize(double a, int, int=0) { return 8; }
205 template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
206 template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
207 template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
208 template<typename Stream> inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); }
209 template<typename Stream> inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); }
210 template<typename Stream> inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); }
211 template<typename Stream> inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); }
212 template<typename Stream> inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); }
213 template<typename Stream> inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); }
214 template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
215 template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
217 template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
218 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); }
219 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); }
220 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); }
221 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); }
222 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); }
223 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); }
224 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); }
225 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); }
226 template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
227 template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
229 inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
230 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
231 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
240 * size < 253 -- 1 byte
241 * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
242 * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
243 * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
245 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
247 if (nSize < 253) return sizeof(unsigned char);
248 else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
249 else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
250 else return sizeof(unsigned char) + sizeof(uint64_t);
253 template<typename Stream>
254 void WriteCompactSize(Stream& os, uint64_t nSize)
258 ser_writedata8(os, nSize);
260 else if (nSize <= std::numeric_limits<unsigned short>::max())
262 ser_writedata8(os, 253);
263 ser_writedata16(os, nSize);
265 else if (nSize <= std::numeric_limits<unsigned int>::max())
267 ser_writedata8(os, 254);
268 ser_writedata32(os, nSize);
272 ser_writedata8(os, 255);
273 ser_writedata64(os, nSize);
278 template<typename Stream>
279 uint64_t ReadCompactSize(Stream& is)
281 uint8_t chSize = ser_readdata8(is);
282 uint64_t nSizeRet = 0;
287 else if (chSize == 253)
289 nSizeRet = ser_readdata16(is);
291 throw std::ios_base::failure("non-canonical ReadCompactSize()");
293 else if (chSize == 254)
295 nSizeRet = ser_readdata32(is);
296 if (nSizeRet < 0x10000u)
297 throw std::ios_base::failure("non-canonical ReadCompactSize()");
301 nSizeRet = ser_readdata64(is);
302 if (nSizeRet < 0x100000000ULL)
303 throw std::ios_base::failure("non-canonical ReadCompactSize()");
305 if (nSizeRet > (uint64_t)MAX_SIZE)
306 throw std::ios_base::failure("ReadCompactSize(): size too large");
311 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
312 * The high bit in each byte signifies whether another digit follows. To make
313 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
314 * Thus, the byte sequence a[] with length len, where all but the last byte
315 * has bit 128 set, encodes the number:
317 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
320 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
321 * * Every integer has exactly one encoding
322 * * Encoding does not depend on size of original integer type
323 * * No redundancy: every (infinite) byte sequence corresponds to a list
324 * of encoded integers.
326 * 0: [0x00] 256: [0x81 0x00]
327 * 1: [0x01] 16383: [0xFE 0x7F]
328 * 127: [0x7F] 16384: [0xFF 0x00]
329 * 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F]
330 * 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F]
331 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
335 inline unsigned int GetSizeOfVarInt(I n)
347 template<typename Stream, typename I>
348 void WriteVarInt(Stream& os, I n)
350 unsigned char tmp[(sizeof(n)*8+6)/7];
353 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
360 ser_writedata8(os, tmp[len]);
364 template<typename Stream, typename I>
365 I ReadVarInt(Stream& is)
369 unsigned char chData = ser_readdata8(is);
370 n = (n << 7) | (chData & 0x7F);
378 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
379 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
380 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
383 * Wrapper for serializing arrays and POD.
391 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
392 template <class T, class TAl>
393 explicit CFlatData(std::vector<T,TAl> &v)
395 pbegin = (char*)begin_ptr(v);
396 pend = (char*)end_ptr(v);
398 char* begin() { return pbegin; }
399 const char* begin() const { return pbegin; }
400 char* end() { return pend; }
401 const char* end() const { return pend; }
403 unsigned int GetSerializeSize(int, int=0) const
405 return pend - pbegin;
408 template<typename Stream>
409 void Serialize(Stream& s, int, int=0) const
411 s.write(pbegin, pend - pbegin);
414 template<typename Stream>
415 void Unserialize(Stream& s, int, int=0)
417 s.read(pbegin, pend - pbegin);
427 CVarInt(I& nIn) : n(nIn) { }
429 unsigned int GetSerializeSize(int, int) const {
430 return GetSizeOfVarInt<I>(n);
433 template<typename Stream>
434 void Serialize(Stream &s, int, int) const {
435 WriteVarInt<Stream,I>(s, n);
438 template<typename Stream>
439 void Unserialize(Stream& s, int, int) {
440 n = ReadVarInt<Stream,I>(s);
444 template<size_t Limit>
450 LimitedString(std::string& string) : string(string) {}
452 template<typename Stream>
453 void Unserialize(Stream& s, int, int=0)
455 size_t size = ReadCompactSize(s);
457 throw std::ios_base::failure("String length limit exceeded");
461 s.read((char*)&string[0], size);
464 template<typename Stream>
465 void Serialize(Stream& s, int, int=0) const
467 WriteCompactSize(s, string.size());
469 s.write((char*)&string[0], string.size());
472 unsigned int GetSerializeSize(int, int=0) const
474 return GetSizeOfCompactSize(string.size()) + string.size();
479 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
482 * Forward declarations
488 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
489 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
490 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
494 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
496 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
497 template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
498 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
499 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&);
500 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&);
501 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
502 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
503 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&);
504 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
507 * others derived from vector
509 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion);
510 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
511 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
516 template<typename T> unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion);
517 template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion);
518 template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion);
523 template<typename T, std::size_t N> unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion);
524 template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion);
525 template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion);
530 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
531 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
532 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
537 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
538 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);
539 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);
544 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
545 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
546 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
551 template<typename T, typename A> unsigned int GetSerializeSize(const std::list<T, A>& m, int nType, int nVersion);
552 template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m, int nType, int nVersion);
553 template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m, int nType, int nVersion);
560 * If none of the specialized versions above matched, default to calling member function.
561 * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
562 * The compiler will only cast int to long if none of the other templates matched.
563 * Thanks to Boost serialization for this idea.
566 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
568 return a.GetSerializeSize((int)nType, nVersion);
571 template<typename Stream, typename T>
572 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
574 a.Serialize(os, (int)nType, nVersion);
577 template<typename Stream, typename T>
578 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
580 a.Unserialize(is, (int)nType, nVersion);
591 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
593 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
596 template<typename Stream, typename C>
597 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
599 WriteCompactSize(os, str.size());
601 os.write((char*)&str[0], str.size() * sizeof(str[0]));
604 template<typename Stream, typename C>
605 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
607 unsigned int nSize = ReadCompactSize(is);
610 is.read((char*)&str[0], nSize * sizeof(str[0]));
618 template<typename T, typename A>
619 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
621 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
624 template<typename T, typename A, typename V>
625 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
627 unsigned int nSize = GetSizeOfCompactSize(v.size());
628 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
629 nSize += GetSerializeSize((*vi), nType, nVersion);
633 template<typename T, typename A>
634 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
636 return GetSerializeSize_impl(v, nType, nVersion, T());
640 template<typename Stream, typename T, typename A>
641 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
643 WriteCompactSize(os, v.size());
645 os.write((char*)&v[0], v.size() * sizeof(T));
648 template<typename Stream, typename T, typename A, typename V>
649 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
651 WriteCompactSize(os, v.size());
652 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
653 ::Serialize(os, (*vi), nType, nVersion);
656 template<typename Stream, typename T, typename A>
657 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
659 Serialize_impl(os, v, nType, nVersion, T());
663 template<typename Stream, typename T, typename A>
664 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
666 // Limit size per read so bogus size value won't cause out of memory
668 unsigned int nSize = ReadCompactSize(is);
672 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
674 is.read((char*)&v[i], blk * sizeof(T));
679 template<typename Stream, typename T, typename A, typename V>
680 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
683 unsigned int nSize = ReadCompactSize(is);
685 unsigned int nMid = 0;
688 nMid += 5000000 / sizeof(T);
692 for (; i < nMid; i++)
693 Unserialize(is, v[i], nType, nVersion);
697 template<typename Stream, typename T, typename A>
698 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
700 Unserialize_impl(is, v, nType, nVersion, T());
706 * others derived from vector
708 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
710 return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
713 template<typename Stream>
714 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
716 Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
719 template<typename Stream>
720 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
722 Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
731 unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion)
734 return 1 + GetSerializeSize(*item, nType, nVersion);
740 template<typename Stream, typename T>
741 void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion)
743 // If the value is there, put 0x01 and then serialize the value.
744 // If it's not, put 0x00.
746 unsigned char discriminant = 0x01;
747 Serialize(os, discriminant, nType, nVersion);
748 Serialize(os, *item, nType, nVersion);
750 unsigned char discriminant = 0x00;
751 Serialize(os, discriminant, nType, nVersion);
755 template<typename Stream, typename T>
756 void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion)
758 unsigned char discriminant = 0x00;
759 Unserialize(is, discriminant, nType, nVersion);
761 if (discriminant == 0x00) {
763 } else if (discriminant == 0x01) {
765 Unserialize(is, object, nType, nVersion);
768 throw std::ios_base::failure("non-canonical optional discriminant");
777 template<typename T, std::size_t N>
778 unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion)
780 unsigned int size = 0;
781 for (size_t i = 0; i < N; i++) {
782 size += GetSerializeSize(item[0], nType, nVersion);
787 template<typename Stream, typename T, std::size_t N>
788 void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion)
790 for (size_t i = 0; i < N; i++) {
791 Serialize(os, item[i], nType, nVersion);
795 template<typename Stream, typename T, std::size_t N>
796 void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion)
798 for (size_t i = 0; i < N; i++) {
799 Unserialize(is, item[i], nType, nVersion);
807 template<typename K, typename T>
808 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
810 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
813 template<typename Stream, typename K, typename T>
814 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
816 Serialize(os, item.first, nType, nVersion);
817 Serialize(os, item.second, nType, nVersion);
820 template<typename Stream, typename K, typename T>
821 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
823 Unserialize(is, item.first, nType, nVersion);
824 Unserialize(is, item.second, nType, nVersion);
832 template<typename K, typename T, typename Pred, typename A>
833 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
835 unsigned int nSize = GetSizeOfCompactSize(m.size());
836 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
837 nSize += GetSerializeSize((*mi), nType, nVersion);
841 template<typename Stream, typename K, typename T, typename Pred, typename A>
842 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
844 WriteCompactSize(os, m.size());
845 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
846 Serialize(os, (*mi), nType, nVersion);
849 template<typename Stream, typename K, typename T, typename Pred, typename A>
850 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
853 unsigned int nSize = ReadCompactSize(is);
854 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
855 for (unsigned int i = 0; i < nSize; i++)
857 std::pair<K, T> item;
858 Unserialize(is, item, nType, nVersion);
859 mi = m.insert(mi, item);
868 template<typename K, typename Pred, typename A>
869 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
871 unsigned int nSize = GetSizeOfCompactSize(m.size());
872 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
873 nSize += GetSerializeSize((*it), nType, nVersion);
877 template<typename Stream, typename K, typename Pred, typename A>
878 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
880 WriteCompactSize(os, m.size());
881 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
882 Serialize(os, (*it), nType, nVersion);
885 template<typename Stream, typename K, typename Pred, typename A>
886 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
889 unsigned int nSize = ReadCompactSize(is);
890 typename std::set<K, Pred, A>::iterator it = m.begin();
891 for (unsigned int i = 0; i < nSize; i++)
894 Unserialize(is, key, nType, nVersion);
895 it = m.insert(it, key);
904 template<typename T, typename A>
905 unsigned int GetSerializeSize(const std::list<T, A>& l, int nType, int nVersion)
907 unsigned int nSize = GetSizeOfCompactSize(l.size());
908 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
909 nSize += GetSerializeSize((*it), nType, nVersion);
913 template<typename Stream, typename T, typename A>
914 void Serialize(Stream& os, const std::list<T, A>& l, int nType, int nVersion)
916 WriteCompactSize(os, l.size());
917 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
918 Serialize(os, (*it), nType, nVersion);
921 template<typename Stream, typename T, typename A>
922 void Unserialize(Stream& is, std::list<T, A>& l, int nType, int nVersion)
925 unsigned int nSize = ReadCompactSize(is);
926 typename std::list<T, A>::iterator it = l.begin();
927 for (unsigned int i = 0; i < nSize; i++)
930 Unserialize(is, item, nType, nVersion);
938 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
940 struct CSerActionSerialize
942 bool ForRead() const { return false; }
944 struct CSerActionUnserialize
946 bool ForRead() const { return true; }
949 template<typename Stream, typename T>
950 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
952 ::Serialize(s, obj, nType, nVersion);
955 template<typename Stream, typename T>
956 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
958 ::Unserialize(s, obj, nType, nVersion);
978 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
980 CSizeComputer& write(const char *psz, size_t nSize)
982 this->nSize += nSize;
987 CSizeComputer& operator<<(const T& obj)
989 ::Serialize(*this, obj, nType, nVersion);
993 size_t size() const {
998 #endif // BITCOIN_SERIALIZE_H