]> Git Repo - VerusCoin.git/blob - src/serialize.h
Prevector type
[VerusCoin.git] / src / serialize.h
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.
5
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
8
9 #include "compat/endian.h"
10
11 #include <algorithm>
12 #include <assert.h>
13 #include <ios>
14 #include <limits>
15 #include <list>
16 #include <map>
17 #include <set>
18 #include <stdint.h>
19 #include <string>
20 #include <string.h>
21 #include <utility>
22 #include <vector>
23
24 #include <boost/array.hpp>
25 #include <boost/optional.hpp>
26
27 #include "prevector.h"
28
29 static const unsigned int MAX_SIZE = 0x02000000;
30
31 /**
32  * Used to bypass the rule against non-const reference to temporary
33  * where it makes sense with wrappers such as CFlatData or CTxDB
34  */
35 template<typename T>
36 inline T& REF(const T& val)
37 {
38     return const_cast<T&>(val);
39 }
40
41 /**
42  * Used to acquire a non-const pointer "this" to generate bodies
43  * of const serialization operations from a template
44  */
45 template<typename T>
46 inline T* NCONST_PTR(const T* val)
47 {
48     return const_cast<T*>(val);
49 }
50
51 /** 
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.
55  */
56 template <typename V>
57 inline typename V::value_type* begin_ptr(V& v)
58 {
59     return v.empty() ? NULL : &v[0];
60 }
61 /** Get begin pointer of vector (const version) */
62 template <typename V>
63 inline const typename V::value_type* begin_ptr(const V& v)
64 {
65     return v.empty() ? NULL : &v[0];
66 }
67 /** Get end pointer of vector (non-const version) */
68 template <typename V>
69 inline typename V::value_type* end_ptr(V& v)
70 {
71     return v.empty() ? NULL : (&v[0] + v.size());
72 }
73 /** Get end pointer of vector (const version) */
74 template <typename V>
75 inline const typename V::value_type* end_ptr(const V& v)
76 {
77     return v.empty() ? NULL : (&v[0] + v.size());
78 }
79
80 /*
81  * Lowest-level serialization and conversion.
82  * @note Sizes of these types are verified in the tests
83  */
84 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
85 {
86     s.write((char*)&obj, 1);
87 }
88 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
89 {
90     obj = htole16(obj);
91     s.write((char*)&obj, 2);
92 }
93 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
94 {
95     obj = htole32(obj);
96     s.write((char*)&obj, 4);
97 }
98 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
99 {
100     obj = htole64(obj);
101     s.write((char*)&obj, 8);
102 }
103 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
104 {
105     uint8_t obj;
106     s.read((char*)&obj, 1);
107     return obj;
108 }
109 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
110 {
111     uint16_t obj;
112     s.read((char*)&obj, 2);
113     return le16toh(obj);
114 }
115 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
116 {
117     uint32_t obj;
118     s.read((char*)&obj, 4);
119     return le32toh(obj);
120 }
121 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
122 {
123     uint64_t obj;
124     s.read((char*)&obj, 8);
125     return le64toh(obj);
126 }
127 inline uint64_t ser_double_to_uint64(double x)
128 {
129     union { double x; uint64_t y; } tmp;
130     tmp.x = x;
131     return tmp.y;
132 }
133 inline uint32_t ser_float_to_uint32(float x)
134 {
135     union { float x; uint32_t y; } tmp;
136     tmp.x = x;
137     return tmp.y;
138 }
139 inline double ser_uint64_to_double(uint64_t y)
140 {
141     union { double x; uint64_t y; } tmp;
142     tmp.y = y;
143     return tmp.x;
144 }
145 inline float ser_uint32_to_float(uint32_t y)
146 {
147     union { float x; uint32_t y; } tmp;
148     tmp.y = y;
149     return tmp.x;
150 }
151
152
153 /////////////////////////////////////////////////////////////////
154 //
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)
157 //
158
159 enum
160 {
161     // primary actions
162     SER_NETWORK         = (1 << 0),
163     SER_DISK            = (1 << 1),
164     SER_GETHASH         = (1 << 2),
165 };
166
167 #define READWRITE(obj)      (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
168
169 /** 
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
173  * added as members. 
174  */
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);\
179         return s.size();                                                             \
180     }                                                                                \
181     template<typename Stream>                                                        \
182     void Serialize(Stream& s, int nType, int nVersion) const {                       \
183         NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
184     }                                                                                \
185     template<typename Stream>                                                        \
186     void Unserialize(Stream& s, int nType, int nVersion) {                           \
187         SerializationOp(s, CSerActionUnserialize(), nType, nVersion);                \
188     }
189
190 /*
191  * Basic Types
192  */
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; }
204
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)); }
216
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)); }
228
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; }
232
233
234
235
236
237
238 /**
239  * Compact Size
240  * size <  253        -- 1 byte
241  * size <= 0xFFFF     -- 3 bytes  (253 + 2 bytes)
242  * size <= 0xFFFFFFFF -- 5 bytes  (254 + 4 bytes)
243  * size >  0xFFFFFFFF -- 9 bytes  (255 + 8 bytes)
244  */
245 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
246 {
247     if (nSize < 253)                return 1;
248     else if (nSize <= 0xFFFFu)      return 3;
249     else if (nSize <= 0xFFFFFFFFu)  return 5;
250     else                            return 9;
251 }
252
253 template<typename Stream>
254 void WriteCompactSize(Stream& os, uint64_t nSize)
255 {
256     if (nSize < 253)
257     {
258         ser_writedata8(os, nSize);
259     }
260     else if (nSize <= 0xFFFFu)
261     {
262         ser_writedata8(os, 253);
263         ser_writedata16(os, nSize);
264     }
265     else if (nSize <= 0xFFFFFFFFu)
266     {
267         ser_writedata8(os, 254);
268         ser_writedata32(os, nSize);
269     }
270     else
271     {
272         ser_writedata8(os, 255);
273         ser_writedata64(os, nSize);
274     }
275 }
276
277 template<typename Stream>
278 uint64_t ReadCompactSize(Stream& is)
279 {
280     uint8_t chSize = ser_readdata8(is);
281     uint64_t nSizeRet = 0;
282     if (chSize < 253)
283     {
284         nSizeRet = chSize;
285     }
286     else if (chSize == 253)
287     {
288         nSizeRet = ser_readdata16(is);
289         if (nSizeRet < 253)
290             throw std::ios_base::failure("non-canonical ReadCompactSize()");
291     }
292     else if (chSize == 254)
293     {
294         nSizeRet = ser_readdata32(is);
295         if (nSizeRet < 0x10000u)
296             throw std::ios_base::failure("non-canonical ReadCompactSize()");
297     }
298     else
299     {
300         nSizeRet = ser_readdata64(is);
301         if (nSizeRet < 0x100000000ULL)
302             throw std::ios_base::failure("non-canonical ReadCompactSize()");
303     }
304     if (nSizeRet > (uint64_t)MAX_SIZE)
305         throw std::ios_base::failure("ReadCompactSize(): size too large");
306     return nSizeRet;
307 }
308
309 /**
310  * Variable-length integers: bytes are a MSB base-128 encoding of the number.
311  * The high bit in each byte signifies whether another digit follows. To make
312  * sure the encoding is one-to-one, one is subtracted from all but the last digit.
313  * Thus, the byte sequence a[] with length len, where all but the last byte
314  * has bit 128 set, encodes the number:
315  * 
316  *  (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
317  * 
318  * Properties:
319  * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
320  * * Every integer has exactly one encoding
321  * * Encoding does not depend on size of original integer type
322  * * No redundancy: every (infinite) byte sequence corresponds to a list
323  *   of encoded integers.
324  * 
325  * 0:         [0x00]  256:        [0x81 0x00]
326  * 1:         [0x01]  16383:      [0xFE 0x7F]
327  * 127:       [0x7F]  16384:      [0xFF 0x00]
328  * 128:  [0x80 0x00]  16511: [0x80 0xFF 0x7F]
329  * 255:  [0x80 0x7F]  65535: [0x82 0xFD 0x7F]
330  * 2^32:           [0x8E 0xFE 0xFE 0xFF 0x00]
331  */
332
333 template<typename I>
334 inline unsigned int GetSizeOfVarInt(I n)
335 {
336     int nRet = 0;
337     while(true) {
338         nRet++;
339         if (n <= 0x7F)
340             break;
341         n = (n >> 7) - 1;
342     }
343     return nRet;
344 }
345
346 template<typename Stream, typename I>
347 void WriteVarInt(Stream& os, I n)
348 {
349     unsigned char tmp[(sizeof(n)*8+6)/7];
350     int len=0;
351     while(true) {
352         tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
353         if (n <= 0x7F)
354             break;
355         n = (n >> 7) - 1;
356         len++;
357     }
358     do {
359         ser_writedata8(os, tmp[len]);
360     } while(len--);
361 }
362
363 template<typename Stream, typename I>
364 I ReadVarInt(Stream& is)
365 {
366     I n = 0;
367     while(true) {
368         unsigned char chData = ser_readdata8(is);
369         n = (n << 7) | (chData & 0x7F);
370         if (chData & 0x80)
371             n++;
372         else
373             return n;
374     }
375 }
376
377 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
378 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
379 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
380
381 /** 
382  * Wrapper for serializing arrays and POD.
383  */
384 class CFlatData
385 {
386 protected:
387     char* pbegin;
388     char* pend;
389 public:
390     CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
391     template <class T, class TAl>
392     explicit CFlatData(std::vector<T,TAl> &v)
393     {
394         pbegin = (char*)begin_ptr(v);
395         pend = (char*)end_ptr(v);
396     }
397     template <unsigned int N, typename T, typename S, typename D>
398     explicit CFlatData(prevector<N, T, S, D> &v)
399     {
400         pbegin = (char*)begin_ptr(v);
401         pend = (char*)end_ptr(v);
402     }
403     char* begin() { return pbegin; }
404     const char* begin() const { return pbegin; }
405     char* end() { return pend; }
406     const char* end() const { return pend; }
407
408     unsigned int GetSerializeSize(int, int=0) const
409     {
410         return pend - pbegin;
411     }
412
413     template<typename Stream>
414     void Serialize(Stream& s, int, int=0) const
415     {
416         s.write(pbegin, pend - pbegin);
417     }
418
419     template<typename Stream>
420     void Unserialize(Stream& s, int, int=0)
421     {
422         s.read(pbegin, pend - pbegin);
423     }
424 };
425
426 template<typename I>
427 class CVarInt
428 {
429 protected:
430     I &n;
431 public:
432     CVarInt(I& nIn) : n(nIn) { }
433
434     unsigned int GetSerializeSize(int, int) const {
435         return GetSizeOfVarInt<I>(n);
436     }
437
438     template<typename Stream>
439     void Serialize(Stream &s, int, int) const {
440         WriteVarInt<Stream,I>(s, n);
441     }
442
443     template<typename Stream>
444     void Unserialize(Stream& s, int, int) {
445         n = ReadVarInt<Stream,I>(s);
446     }
447 };
448
449 template<size_t Limit>
450 class LimitedString
451 {
452 protected:
453     std::string& string;
454 public:
455     LimitedString(std::string& string) : string(string) {}
456
457     template<typename Stream>
458     void Unserialize(Stream& s, int, int=0)
459     {
460         size_t size = ReadCompactSize(s);
461         if (size > Limit) {
462             throw std::ios_base::failure("String length limit exceeded");
463         }
464         string.resize(size);
465         if (size != 0)
466             s.read((char*)&string[0], size);
467     }
468
469     template<typename Stream>
470     void Serialize(Stream& s, int, int=0) const
471     {
472         WriteCompactSize(s, string.size());
473         if (!string.empty())
474             s.write((char*)&string[0], string.size());
475     }
476
477     unsigned int GetSerializeSize(int, int=0) const
478     {
479         return GetSizeOfCompactSize(string.size()) + string.size();
480     }
481 };
482
483 template<typename I>
484 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
485
486 /**
487  * Forward declarations
488  */
489
490 /**
491  *  string
492  */
493 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
494 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
495 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
496
497 /**
498  * prevector
499  * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
500  */
501 template<unsigned int N, typename T> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
502 template<unsigned int N, typename T, typename V> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&);
503 template<unsigned int N, typename T> inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion);
504 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
505 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&);
506 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion);
507 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
508 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&);
509 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion);
510
511 /**
512  * vector
513  * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
514  */
515 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
516 template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
517 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
518 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&);
519 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&);
520 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
521 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
522 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&);
523 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
524
525 /**
526  * optional
527  */
528 template<typename T> unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion);
529 template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion);
530 template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion);
531
532 /**
533  * array
534  */
535 template<typename T, std::size_t N> unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion);
536 template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion);
537 template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion);
538
539 /**
540  * pair
541  */
542 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
543 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
544 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
545
546 /**
547  * map
548  */
549 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
550 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);
551 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);
552
553 /**
554  * set
555  */
556 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
557 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
558 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
559
560 /**
561  * list
562  */
563 template<typename T, typename A> unsigned int GetSerializeSize(const std::list<T, A>& m, int nType, int nVersion);
564 template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m, int nType, int nVersion);
565 template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m, int nType, int nVersion);
566
567
568
569
570
571 /**
572  * If none of the specialized versions above matched, default to calling member function.
573  * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
574  * The compiler will only cast int to long if none of the other templates matched.
575  * Thanks to Boost serialization for this idea.
576  */
577 template<typename T>
578 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
579 {
580     return a.GetSerializeSize((int)nType, nVersion);
581 }
582
583 template<typename Stream, typename T>
584 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
585 {
586     a.Serialize(os, (int)nType, nVersion);
587 }
588
589 template<typename Stream, typename T>
590 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
591 {
592     a.Unserialize(is, (int)nType, nVersion);
593 }
594
595
596
597
598
599 /**
600  * string
601  */
602 template<typename C>
603 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
604 {
605     return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
606 }
607
608 template<typename Stream, typename C>
609 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
610 {
611     WriteCompactSize(os, str.size());
612     if (!str.empty())
613         os.write((char*)&str[0], str.size() * sizeof(str[0]));
614 }
615
616 template<typename Stream, typename C>
617 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
618 {
619     unsigned int nSize = ReadCompactSize(is);
620     str.resize(nSize);
621     if (nSize != 0)
622         is.read((char*)&str[0], nSize * sizeof(str[0]));
623 }
624
625
626
627 /**
628  * prevector
629  */
630 template<unsigned int N, typename T>
631 unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
632 {
633     return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
634 }
635
636 template<unsigned int N, typename T, typename V>
637 unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&)
638 {
639     unsigned int nSize = GetSizeOfCompactSize(v.size());
640     for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
641         nSize += GetSerializeSize((*vi), nType, nVersion);
642     return nSize;
643 }
644
645 template<unsigned int N, typename T>
646 inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion)
647 {
648     return GetSerializeSize_impl(v, nType, nVersion, T());
649 }
650
651
652 template<typename Stream, unsigned int N, typename T>
653 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
654 {
655     WriteCompactSize(os, v.size());
656     if (!v.empty())
657         os.write((char*)&v[0], v.size() * sizeof(T));
658 }
659
660 template<typename Stream, unsigned int N, typename T, typename V>
661 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&)
662 {
663     WriteCompactSize(os, v.size());
664     for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
665         ::Serialize(os, (*vi), nType, nVersion);
666 }
667
668 template<typename Stream, unsigned int N, typename T>
669 inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion)
670 {
671     Serialize_impl(os, v, nType, nVersion, T());
672 }
673
674
675 template<typename Stream, unsigned int N, typename T>
676 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
677 {
678     // Limit size per read so bogus size value won't cause out of memory
679     v.clear();
680     unsigned int nSize = ReadCompactSize(is);
681     unsigned int i = 0;
682     while (i < nSize)
683     {
684         unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
685         v.resize(i + blk);
686         is.read((char*)&v[i], blk * sizeof(T));
687         i += blk;
688     }
689 }
690
691 template<typename Stream, unsigned int N, typename T, typename V>
692 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&)
693 {
694     v.clear();
695     unsigned int nSize = ReadCompactSize(is);
696     unsigned int i = 0;
697     unsigned int nMid = 0;
698     while (nMid < nSize)
699     {
700         nMid += 5000000 / sizeof(T);
701         if (nMid > nSize)
702             nMid = nSize;
703         v.resize(nMid);
704         for (; i < nMid; i++)
705             Unserialize(is, v[i], nType, nVersion);
706     }
707 }
708
709 template<typename Stream, unsigned int N, typename T>
710 inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
711 {
712     Unserialize_impl(is, v, nType, nVersion, T());
713 }
714
715
716
717 /**
718  * vector
719  */
720 template<typename T, typename A>
721 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
722 {
723     return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
724 }
725
726 template<typename T, typename A, typename V>
727 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
728 {
729     unsigned int nSize = GetSizeOfCompactSize(v.size());
730     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
731         nSize += GetSerializeSize((*vi), nType, nVersion);
732     return nSize;
733 }
734
735 template<typename T, typename A>
736 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
737 {
738     return GetSerializeSize_impl(v, nType, nVersion, T());
739 }
740
741
742 template<typename Stream, typename T, typename A>
743 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
744 {
745     WriteCompactSize(os, v.size());
746     if (!v.empty())
747         os.write((char*)&v[0], v.size() * sizeof(T));
748 }
749
750 template<typename Stream, typename T, typename A, typename V>
751 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
752 {
753     WriteCompactSize(os, v.size());
754     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
755         ::Serialize(os, (*vi), nType, nVersion);
756 }
757
758 template<typename Stream, typename T, typename A>
759 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
760 {
761     Serialize_impl(os, v, nType, nVersion, T());
762 }
763
764
765 template<typename Stream, typename T, typename A>
766 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
767 {
768     // Limit size per read so bogus size value won't cause out of memory
769     v.clear();
770     unsigned int nSize = ReadCompactSize(is);
771     unsigned int i = 0;
772     while (i < nSize)
773     {
774         unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
775         v.resize(i + blk);
776         is.read((char*)&v[i], blk * sizeof(T));
777         i += blk;
778     }
779 }
780
781 template<typename Stream, typename T, typename A, typename V>
782 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
783 {
784     v.clear();
785     unsigned int nSize = ReadCompactSize(is);
786     unsigned int i = 0;
787     unsigned int nMid = 0;
788     while (nMid < nSize)
789     {
790         nMid += 5000000 / sizeof(T);
791         if (nMid > nSize)
792             nMid = nSize;
793         v.resize(nMid);
794         for (; i < nMid; i++)
795             Unserialize(is, v[i], nType, nVersion);
796     }
797 }
798
799 template<typename Stream, typename T, typename A>
800 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
801 {
802     Unserialize_impl(is, v, nType, nVersion, T());
803 }
804
805
806
807 /**
808  * optional
809  */
810 template<typename T>
811 unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion)
812 {
813     if (item) {
814         return 1 + GetSerializeSize(*item, nType, nVersion);
815     } else {
816         return 1;
817     }
818 }
819
820 template<typename Stream, typename T>
821 void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion)
822 {
823     // If the value is there, put 0x01 and then serialize the value.
824     // If it's not, put 0x00.
825     if (item) {
826         unsigned char discriminant = 0x01;
827         Serialize(os, discriminant, nType, nVersion);
828         Serialize(os, *item, nType, nVersion);
829     } else {
830         unsigned char discriminant = 0x00;
831         Serialize(os, discriminant, nType, nVersion);
832     }
833 }
834
835 template<typename Stream, typename T>
836 void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion)
837 {
838     unsigned char discriminant = 0x00;
839     Unserialize(is, discriminant, nType, nVersion);
840
841     if (discriminant == 0x00) {
842         item = boost::none;
843     } else if (discriminant == 0x01) {
844         T object;
845         Unserialize(is, object, nType, nVersion);
846         item = object;
847     } else {
848         throw std::ios_base::failure("non-canonical optional discriminant");
849     }
850 }
851
852
853
854 /**
855  * array
856  */
857 template<typename T, std::size_t N>
858 unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion)
859 {
860     unsigned int size = 0;
861     for (size_t i = 0; i < N; i++) {
862         size += GetSerializeSize(item[0], nType, nVersion);
863     }
864     return size;
865 }
866
867 template<typename Stream, typename T, std::size_t N>
868 void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion)
869 {
870     for (size_t i = 0; i < N; i++) {
871         Serialize(os, item[i], nType, nVersion);
872     }
873 }
874
875 template<typename Stream, typename T, std::size_t N>
876 void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion)
877 {
878     for (size_t i = 0; i < N; i++) {
879         Unserialize(is, item[i], nType, nVersion);
880     }
881 }
882
883
884 /**
885  * pair
886  */
887 template<typename K, typename T>
888 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
889 {
890     return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
891 }
892
893 template<typename Stream, typename K, typename T>
894 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
895 {
896     Serialize(os, item.first, nType, nVersion);
897     Serialize(os, item.second, nType, nVersion);
898 }
899
900 template<typename Stream, typename K, typename T>
901 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
902 {
903     Unserialize(is, item.first, nType, nVersion);
904     Unserialize(is, item.second, nType, nVersion);
905 }
906
907
908
909 /**
910  * map
911  */
912 template<typename K, typename T, typename Pred, typename A>
913 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
914 {
915     unsigned int nSize = GetSizeOfCompactSize(m.size());
916     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
917         nSize += GetSerializeSize((*mi), nType, nVersion);
918     return nSize;
919 }
920
921 template<typename Stream, typename K, typename T, typename Pred, typename A>
922 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
923 {
924     WriteCompactSize(os, m.size());
925     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
926         Serialize(os, (*mi), nType, nVersion);
927 }
928
929 template<typename Stream, typename K, typename T, typename Pred, typename A>
930 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
931 {
932     m.clear();
933     unsigned int nSize = ReadCompactSize(is);
934     typename std::map<K, T, Pred, A>::iterator mi = m.begin();
935     for (unsigned int i = 0; i < nSize; i++)
936     {
937         std::pair<K, T> item;
938         Unserialize(is, item, nType, nVersion);
939         mi = m.insert(mi, item);
940     }
941 }
942
943
944
945 /**
946  * set
947  */
948 template<typename K, typename Pred, typename A>
949 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
950 {
951     unsigned int nSize = GetSizeOfCompactSize(m.size());
952     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
953         nSize += GetSerializeSize((*it), nType, nVersion);
954     return nSize;
955 }
956
957 template<typename Stream, typename K, typename Pred, typename A>
958 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
959 {
960     WriteCompactSize(os, m.size());
961     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
962         Serialize(os, (*it), nType, nVersion);
963 }
964
965 template<typename Stream, typename K, typename Pred, typename A>
966 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
967 {
968     m.clear();
969     unsigned int nSize = ReadCompactSize(is);
970     typename std::set<K, Pred, A>::iterator it = m.begin();
971     for (unsigned int i = 0; i < nSize; i++)
972     {
973         K key;
974         Unserialize(is, key, nType, nVersion);
975         it = m.insert(it, key);
976     }
977 }
978
979
980
981 /**
982  * list
983  */
984 template<typename T, typename A>
985 unsigned int GetSerializeSize(const std::list<T, A>& l, int nType, int nVersion)
986 {
987     unsigned int nSize = GetSizeOfCompactSize(l.size());
988     for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
989         nSize += GetSerializeSize((*it), nType, nVersion);
990     return nSize;
991 }
992
993 template<typename Stream, typename T, typename A>
994 void Serialize(Stream& os, const std::list<T, A>& l, int nType, int nVersion)
995 {
996     WriteCompactSize(os, l.size());
997     for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
998         Serialize(os, (*it), nType, nVersion);
999 }
1000
1001 template<typename Stream, typename T, typename A>
1002 void Unserialize(Stream& is, std::list<T, A>& l, int nType, int nVersion)
1003 {
1004     l.clear();
1005     unsigned int nSize = ReadCompactSize(is);
1006     typename std::list<T, A>::iterator it = l.begin();
1007     for (unsigned int i = 0; i < nSize; i++)
1008     {
1009         T item;
1010         Unserialize(is, item, nType, nVersion);
1011         l.push_back(item);
1012     }
1013 }
1014
1015
1016
1017 /**
1018  * Support for ADD_SERIALIZE_METHODS and READWRITE macro
1019  */
1020 struct CSerActionSerialize
1021 {
1022     bool ForRead() const { return false; }
1023 };
1024 struct CSerActionUnserialize
1025 {
1026     bool ForRead() const { return true; }
1027 };
1028
1029 template<typename Stream, typename T>
1030 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
1031 {
1032     ::Serialize(s, obj, nType, nVersion);
1033 }
1034
1035 template<typename Stream, typename T>
1036 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
1037 {
1038     ::Unserialize(s, obj, nType, nVersion);
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049 class CSizeComputer
1050 {
1051 protected:
1052     size_t nSize;
1053
1054 public:
1055     int nType;
1056     int nVersion;
1057
1058     CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
1059
1060     CSizeComputer& write(const char *psz, size_t nSize)
1061     {
1062         this->nSize += nSize;
1063         return *this;
1064     }
1065
1066     template<typename T>
1067     CSizeComputer& operator<<(const T& obj)
1068     {
1069         ::Serialize(*this, obj, nType, nVersion);
1070         return (*this);
1071     }
1072
1073     size_t size() const {
1074         return nSize;
1075     }
1076 };
1077
1078 #endif // BITCOIN_SERIALIZE_H
This page took 0.085839 seconds and 4 git commands to generate.