]>
Commit | Line | Data |
---|---|---|
0a61b0df | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
1c0aa911 | 3 | // Distributed under the MIT software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 5 | |
223b6f1b WL |
6 | #ifndef BITCOIN_SERIALIZE_H |
7 | #define BITCOIN_SERIALIZE_H | |
0a61b0df | 8 | |
5207f33f PK |
9 | #include "compat/endian.h" |
10 | ||
51ed9ec9 BD |
11 | #include <algorithm> |
12 | #include <assert.h> | |
51ed9ec9 | 13 | #include <ios> |
611116d4 | 14 | #include <limits> |
be74c80d | 15 | #include <list> |
0a61b0df | 16 | #include <map> |
d743f035 | 17 | #include <set> |
51ed9ec9 BD |
18 | #include <stdint.h> |
19 | #include <string> | |
20 | #include <string.h> | |
21 | #include <utility> | |
22 | #include <vector> | |
223b6f1b | 23 | |
5884044b | 24 | #include <boost/array.hpp> |
291b191b | 25 | #include <boost/optional.hpp> |
5884044b | 26 | |
51ed9ec9 BD |
27 | class CScript; |
28 | ||
0a61b0df | 29 | static const unsigned int MAX_SIZE = 0x02000000; |
30 | ||
1c0aa911 MF |
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 | */ | |
82dc6426 GS |
35 | template<typename T> |
36 | inline T& REF(const T& val) | |
37 | { | |
38 | return const_cast<T&>(val); | |
39 | } | |
0a61b0df | 40 | |
1c0aa911 MF |
41 | /** |
42 | * Used to acquire a non-const pointer "this" to generate bodies | |
43 | * of const serialization operations from a template | |
44 | */ | |
3d796f89 | 45 | template<typename T> |
84881f8c | 46 | inline T* NCONST_PTR(const T* val) |
3d796f89 | 47 | { |
84881f8c | 48 | return const_cast<T*>(val); |
3d796f89 KD |
49 | } |
50 | ||
1c0aa911 MF |
51 | /** |
52 | * Get begin pointer of vector (non-const version). | |
fa126eff WL |
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 <class T, class TAl> | |
57 | inline T* begin_ptr(std::vector<T,TAl>& v) | |
58 | { | |
59 | return v.empty() ? NULL : &v[0]; | |
60 | } | |
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) | |
64 | { | |
65 | return v.empty() ? NULL : &v[0]; | |
66 | } | |
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) | |
70 | { | |
71 | return v.empty() ? NULL : (&v[0] + v.size()); | |
72 | } | |
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) | |
76 | { | |
77 | return v.empty() ? NULL : (&v[0] + v.size()); | |
78 | } | |
79 | ||
01f9c344 WL |
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 | ||
0a61b0df | 153 | ///////////////////////////////////////////////////////////////// |
154 | // | |
155 | // Templates for serializing to anything that looks like a stream, | |
8695a393 | 156 | // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t) |
0a61b0df | 157 | // |
158 | ||
159 | enum | |
160 | { | |
161 | // primary actions | |
162 | SER_NETWORK = (1 << 0), | |
163 | SER_DISK = (1 << 1), | |
164 | SER_GETHASH = (1 << 2), | |
0a61b0df | 165 | }; |
166 | ||
31e9a838 | 167 | #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action)) |
0a61b0df | 168 | |
1c0aa911 MF |
169 | /** |
170 | * Implement three methods for serializable objects. These are actually wrappers over | |
3d796f89 | 171 | * "SerializationOp" template, which implements the body of each class' serialization |
3f6540ad | 172 | * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be |
1c0aa911 MF |
173 | * added as members. |
174 | */ | |
3f6540ad | 175 | #define ADD_SERIALIZE_METHODS \ |
31e9a838 PW |
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); \ | |
0a61b0df | 188 | } |
189 | ||
1c0aa911 MF |
190 | /* |
191 | * Basic Types | |
192 | */ | |
01f9c344 WL |
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)); } | |
0a61b0df | 228 | |
229 | inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); } | |
01f9c344 WL |
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; } | |
0a61b0df | 232 | |
233 | ||
234 | ||
235 | ||
236 | ||
237 | ||
1c0aa911 MF |
238 | /** |
239 | * Compact Size | |
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) | |
244 | */ | |
51ed9ec9 | 245 | inline unsigned int GetSizeOfCompactSize(uint64_t nSize) |
0a61b0df | 246 | { |
a790fa46 | 247 | if (nSize < 253) return sizeof(unsigned char); |
26ce92b3 GA |
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); | |
51ed9ec9 | 250 | else return sizeof(unsigned char) + sizeof(uint64_t); |
0a61b0df | 251 | } |
252 | ||
253 | template<typename Stream> | |
51ed9ec9 | 254 | void WriteCompactSize(Stream& os, uint64_t nSize) |
0a61b0df | 255 | { |
a790fa46 | 256 | if (nSize < 253) |
0a61b0df | 257 | { |
01f9c344 | 258 | ser_writedata8(os, nSize); |
0a61b0df | 259 | } |
26ce92b3 | 260 | else if (nSize <= std::numeric_limits<unsigned short>::max()) |
0a61b0df | 261 | { |
01f9c344 WL |
262 | ser_writedata8(os, 253); |
263 | ser_writedata16(os, nSize); | |
0a61b0df | 264 | } |
26ce92b3 | 265 | else if (nSize <= std::numeric_limits<unsigned int>::max()) |
0a61b0df | 266 | { |
01f9c344 WL |
267 | ser_writedata8(os, 254); |
268 | ser_writedata32(os, nSize); | |
0a61b0df | 269 | } |
270 | else | |
271 | { | |
01f9c344 WL |
272 | ser_writedata8(os, 255); |
273 | ser_writedata64(os, nSize); | |
0a61b0df | 274 | } |
275 | return; | |
276 | } | |
277 | ||
278 | template<typename Stream> | |
51ed9ec9 | 279 | uint64_t ReadCompactSize(Stream& is) |
0a61b0df | 280 | { |
01f9c344 | 281 | uint8_t chSize = ser_readdata8(is); |
51ed9ec9 | 282 | uint64_t nSizeRet = 0; |
a790fa46 | 283 | if (chSize < 253) |
0a61b0df | 284 | { |
285 | nSizeRet = chSize; | |
286 | } | |
a790fa46 | 287 | else if (chSize == 253) |
0a61b0df | 288 | { |
01f9c344 | 289 | nSizeRet = ser_readdata16(is); |
8dc206a1 GA |
290 | if (nSizeRet < 253) |
291 | throw std::ios_base::failure("non-canonical ReadCompactSize()"); | |
0a61b0df | 292 | } |
a790fa46 | 293 | else if (chSize == 254) |
0a61b0df | 294 | { |
01f9c344 | 295 | nSizeRet = ser_readdata32(is); |
8dc206a1 GA |
296 | if (nSizeRet < 0x10000u) |
297 | throw std::ios_base::failure("non-canonical ReadCompactSize()"); | |
0a61b0df | 298 | } |
299 | else | |
300 | { | |
01f9c344 | 301 | nSizeRet = ser_readdata64(is); |
775b7b8d | 302 | if (nSizeRet < 0x100000000ULL) |
8dc206a1 | 303 | throw std::ios_base::failure("non-canonical ReadCompactSize()"); |
0a61b0df | 304 | } |
51ed9ec9 | 305 | if (nSizeRet > (uint64_t)MAX_SIZE) |
5262fde0 | 306 | throw std::ios_base::failure("ReadCompactSize(): size too large"); |
0a61b0df | 307 | return nSizeRet; |
308 | } | |
309 | ||
1c0aa911 MF |
310 | /** |
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: | |
316 | * | |
317 | * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1)) | |
318 | * | |
319 | * Properties: | |
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. | |
325 | * | |
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] | |
332 | */ | |
4d6144f9 PW |
333 | |
334 | template<typename I> | |
335 | inline unsigned int GetSizeOfVarInt(I n) | |
336 | { | |
337 | int nRet = 0; | |
338 | while(true) { | |
339 | nRet++; | |
340 | if (n <= 0x7F) | |
341 | break; | |
342 | n = (n >> 7) - 1; | |
343 | } | |
344 | return nRet; | |
345 | } | |
346 | ||
347 | template<typename Stream, typename I> | |
348 | void WriteVarInt(Stream& os, I n) | |
349 | { | |
350 | unsigned char tmp[(sizeof(n)*8+6)/7]; | |
351 | int len=0; | |
352 | while(true) { | |
353 | tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00); | |
354 | if (n <= 0x7F) | |
355 | break; | |
356 | n = (n >> 7) - 1; | |
357 | len++; | |
358 | } | |
359 | do { | |
01f9c344 | 360 | ser_writedata8(os, tmp[len]); |
4d6144f9 PW |
361 | } while(len--); |
362 | } | |
0a61b0df | 363 | |
4d6144f9 PW |
364 | template<typename Stream, typename I> |
365 | I ReadVarInt(Stream& is) | |
366 | { | |
367 | I n = 0; | |
368 | while(true) { | |
01f9c344 | 369 | unsigned char chData = ser_readdata8(is); |
4d6144f9 PW |
370 | n = (n << 7) | (chData & 0x7F); |
371 | if (chData & 0x80) | |
372 | n++; | |
373 | else | |
374 | return n; | |
375 | } | |
376 | } | |
0a61b0df | 377 | |
216e9a44 PW |
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))) | |
6b8de05d | 381 | |
1c0aa911 MF |
382 | /** |
383 | * Wrapper for serializing arrays and POD. | |
6b8de05d | 384 | */ |
0a61b0df | 385 | class CFlatData |
386 | { | |
387 | protected: | |
388 | char* pbegin; | |
389 | char* pend; | |
390 | public: | |
391 | CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { } | |
fa126eff WL |
392 | template <class T, class TAl> |
393 | explicit CFlatData(std::vector<T,TAl> &v) | |
394 | { | |
395 | pbegin = (char*)begin_ptr(v); | |
396 | pend = (char*)end_ptr(v); | |
397 | } | |
0a61b0df | 398 | char* begin() { return pbegin; } |
399 | const char* begin() const { return pbegin; } | |
400 | char* end() { return pend; } | |
401 | const char* end() const { return pend; } | |
402 | ||
403 | unsigned int GetSerializeSize(int, int=0) const | |
404 | { | |
405 | return pend - pbegin; | |
406 | } | |
407 | ||
408 | template<typename Stream> | |
409 | void Serialize(Stream& s, int, int=0) const | |
410 | { | |
411 | s.write(pbegin, pend - pbegin); | |
412 | } | |
413 | ||
414 | template<typename Stream> | |
415 | void Unserialize(Stream& s, int, int=0) | |
416 | { | |
417 | s.read(pbegin, pend - pbegin); | |
418 | } | |
419 | }; | |
420 | ||
4d6144f9 PW |
421 | template<typename I> |
422 | class CVarInt | |
423 | { | |
424 | protected: | |
425 | I &n; | |
426 | public: | |
427 | CVarInt(I& nIn) : n(nIn) { } | |
428 | ||
429 | unsigned int GetSerializeSize(int, int) const { | |
430 | return GetSizeOfVarInt<I>(n); | |
431 | } | |
432 | ||
433 | template<typename Stream> | |
434 | void Serialize(Stream &s, int, int) const { | |
435 | WriteVarInt<Stream,I>(s, n); | |
436 | } | |
437 | ||
438 | template<typename Stream> | |
439 | void Unserialize(Stream& s, int, int) { | |
440 | n = ReadVarInt<Stream,I>(s); | |
441 | } | |
442 | }; | |
443 | ||
216e9a44 PW |
444 | template<size_t Limit> |
445 | class LimitedString | |
446 | { | |
447 | protected: | |
448 | std::string& string; | |
449 | public: | |
450 | LimitedString(std::string& string) : string(string) {} | |
451 | ||
452 | template<typename Stream> | |
453 | void Unserialize(Stream& s, int, int=0) | |
454 | { | |
455 | size_t size = ReadCompactSize(s); | |
456 | if (size > Limit) { | |
457 | throw std::ios_base::failure("String length limit exceeded"); | |
458 | } | |
459 | string.resize(size); | |
460 | if (size != 0) | |
461 | s.read((char*)&string[0], size); | |
462 | } | |
463 | ||
464 | template<typename Stream> | |
465 | void Serialize(Stream& s, int, int=0) const | |
466 | { | |
467 | WriteCompactSize(s, string.size()); | |
468 | if (!string.empty()) | |
469 | s.write((char*)&string[0], string.size()); | |
470 | } | |
471 | ||
472 | unsigned int GetSerializeSize(int, int=0) const | |
473 | { | |
474 | return GetSizeOfCompactSize(string.size()) + string.size(); | |
475 | } | |
476 | }; | |
477 | ||
4d6144f9 PW |
478 | template<typename I> |
479 | CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); } | |
480 | ||
1c0aa911 MF |
481 | /** |
482 | * Forward declarations | |
483 | */ | |
0a61b0df | 484 | |
1c0aa911 MF |
485 | /** |
486 | * string | |
487 | */ | |
223b6f1b WL |
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); | |
0a61b0df | 491 | |
1c0aa911 MF |
492 | /** |
493 | * vector | |
494 | * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. | |
495 | */ | |
1d9b86d5 CF |
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&); | |
6b6aaa16 | 498 | template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion); |
1d9b86d5 CF |
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&); | |
6b6aaa16 | 501 | template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion); |
1d9b86d5 CF |
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&); | |
6b6aaa16 | 504 | template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion); |
0a61b0df | 505 | |
1c0aa911 MF |
506 | /** |
507 | * others derived from vector | |
508 | */ | |
6b6aaa16 PW |
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); | |
0a61b0df | 512 | |
291b191b SB |
513 | /** |
514 | * optional | |
515 | */ | |
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); | |
519 | ||
5884044b SB |
520 | /** |
521 | * array | |
522 | */ | |
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); | |
526 | ||
1c0aa911 MF |
527 | /** |
528 | * pair | |
529 | */ | |
6b6aaa16 PW |
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); | |
0a61b0df | 533 | |
1c0aa911 MF |
534 | /** |
535 | * map | |
536 | */ | |
6b6aaa16 PW |
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); | |
0a61b0df | 540 | |
1c0aa911 MF |
541 | /** |
542 | * set | |
543 | */ | |
6b6aaa16 PW |
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); | |
0a61b0df | 547 | |
be74c80d JG |
548 | /** |
549 | * list | |
550 | */ | |
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); | |
554 | ||
0a61b0df | 555 | |
556 | ||
557 | ||
558 | ||
1c0aa911 MF |
559 | /** |
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. | |
564 | */ | |
0a61b0df | 565 | template<typename T> |
6b6aaa16 | 566 | inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion) |
0a61b0df | 567 | { |
568 | return a.GetSerializeSize((int)nType, nVersion); | |
569 | } | |
570 | ||
571 | template<typename Stream, typename T> | |
6b6aaa16 | 572 | inline void Serialize(Stream& os, const T& a, long nType, int nVersion) |
0a61b0df | 573 | { |
574 | a.Serialize(os, (int)nType, nVersion); | |
575 | } | |
576 | ||
577 | template<typename Stream, typename T> | |
6b6aaa16 | 578 | inline void Unserialize(Stream& is, T& a, long nType, int nVersion) |
0a61b0df | 579 | { |
580 | a.Unserialize(is, (int)nType, nVersion); | |
581 | } | |
582 | ||
583 | ||
584 | ||
585 | ||
586 | ||
1c0aa911 MF |
587 | /** |
588 | * string | |
589 | */ | |
0a61b0df | 590 | template<typename C> |
223b6f1b | 591 | unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int) |
0a61b0df | 592 | { |
593 | return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]); | |
594 | } | |
595 | ||
596 | template<typename Stream, typename C> | |
223b6f1b | 597 | void Serialize(Stream& os, const std::basic_string<C>& str, int, int) |
0a61b0df | 598 | { |
599 | WriteCompactSize(os, str.size()); | |
600 | if (!str.empty()) | |
601 | os.write((char*)&str[0], str.size() * sizeof(str[0])); | |
602 | } | |
603 | ||
604 | template<typename Stream, typename C> | |
223b6f1b | 605 | void Unserialize(Stream& is, std::basic_string<C>& str, int, int) |
0a61b0df | 606 | { |
607 | unsigned int nSize = ReadCompactSize(is); | |
608 | str.resize(nSize); | |
609 | if (nSize != 0) | |
610 | is.read((char*)&str[0], nSize * sizeof(str[0])); | |
611 | } | |
612 | ||
613 | ||
614 | ||
1c0aa911 MF |
615 | /** |
616 | * vector | |
617 | */ | |
0a61b0df | 618 | template<typename T, typename A> |
1d9b86d5 | 619 | unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&) |
0a61b0df | 620 | { |
621 | return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T)); | |
622 | } | |
623 | ||
1d9b86d5 CF |
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&) | |
0a61b0df | 626 | { |
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); | |
630 | return nSize; | |
631 | } | |
632 | ||
633 | template<typename T, typename A> | |
634 | inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion) | |
635 | { | |
1d9b86d5 | 636 | return GetSerializeSize_impl(v, nType, nVersion, T()); |
0a61b0df | 637 | } |
638 | ||
639 | ||
640 | template<typename Stream, typename T, typename A> | |
1d9b86d5 | 641 | void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&) |
0a61b0df | 642 | { |
643 | WriteCompactSize(os, v.size()); | |
644 | if (!v.empty()) | |
645 | os.write((char*)&v[0], v.size() * sizeof(T)); | |
646 | } | |
647 | ||
1d9b86d5 CF |
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&) | |
0a61b0df | 650 | { |
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); | |
654 | } | |
655 | ||
656 | template<typename Stream, typename T, typename A> | |
657 | inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion) | |
658 | { | |
1d9b86d5 | 659 | Serialize_impl(os, v, nType, nVersion, T()); |
0a61b0df | 660 | } |
661 | ||
662 | ||
663 | template<typename Stream, typename T, typename A> | |
1d9b86d5 | 664 | void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&) |
0a61b0df | 665 | { |
0a61b0df | 666 | // Limit size per read so bogus size value won't cause out of memory |
667 | v.clear(); | |
668 | unsigned int nSize = ReadCompactSize(is); | |
669 | unsigned int i = 0; | |
670 | while (i < nSize) | |
671 | { | |
223b6f1b | 672 | unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); |
0a61b0df | 673 | v.resize(i + blk); |
674 | is.read((char*)&v[i], blk * sizeof(T)); | |
675 | i += blk; | |
676 | } | |
677 | } | |
678 | ||
1d9b86d5 CF |
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&) | |
0a61b0df | 681 | { |
0a61b0df | 682 | v.clear(); |
683 | unsigned int nSize = ReadCompactSize(is); | |
684 | unsigned int i = 0; | |
685 | unsigned int nMid = 0; | |
686 | while (nMid < nSize) | |
687 | { | |
688 | nMid += 5000000 / sizeof(T); | |
689 | if (nMid > nSize) | |
690 | nMid = nSize; | |
691 | v.resize(nMid); | |
692 | for (; i < nMid; i++) | |
693 | Unserialize(is, v[i], nType, nVersion); | |
694 | } | |
695 | } | |
696 | ||
697 | template<typename Stream, typename T, typename A> | |
698 | inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion) | |
699 | { | |
1d9b86d5 | 700 | Unserialize_impl(is, v, nType, nVersion, T()); |
0a61b0df | 701 | } |
702 | ||
703 | ||
704 | ||
1c0aa911 MF |
705 | /** |
706 | * others derived from vector | |
707 | */ | |
0a61b0df | 708 | inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion) |
709 | { | |
223b6f1b | 710 | return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion); |
0a61b0df | 711 | } |
712 | ||
713 | template<typename Stream> | |
714 | void Serialize(Stream& os, const CScript& v, int nType, int nVersion) | |
715 | { | |
223b6f1b | 716 | Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion); |
0a61b0df | 717 | } |
718 | ||
719 | template<typename Stream> | |
720 | void Unserialize(Stream& is, CScript& v, int nType, int nVersion) | |
721 | { | |
223b6f1b | 722 | Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion); |
0a61b0df | 723 | } |
724 | ||
725 | ||
291b191b SB |
726 | |
727 | /** | |
728 | * optional | |
729 | */ | |
730 | template<typename T> | |
731 | unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion) | |
732 | { | |
733 | if (item) { | |
734 | return 1 + GetSerializeSize(*item, nType, nVersion); | |
735 | } else { | |
736 | return 1; | |
737 | } | |
738 | } | |
739 | ||
740 | template<typename Stream, typename T> | |
741 | void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion) | |
742 | { | |
743 | // If the value is there, put 0x01 and then serialize the value. | |
744 | // If it's not, put 0x00. | |
745 | if (item) { | |
746 | unsigned char discriminant = 0x01; | |
747 | Serialize(os, discriminant, nType, nVersion); | |
748 | Serialize(os, *item, nType, nVersion); | |
749 | } else { | |
750 | unsigned char discriminant = 0x00; | |
751 | Serialize(os, discriminant, nType, nVersion); | |
752 | } | |
753 | } | |
754 | ||
755 | template<typename Stream, typename T> | |
756 | void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion) | |
757 | { | |
758 | unsigned char discriminant = 0x00; | |
759 | Unserialize(is, discriminant, nType, nVersion); | |
760 | ||
761 | if (discriminant == 0x00) { | |
762 | item = boost::none; | |
e1ff849d | 763 | } else if (discriminant == 0x01) { |
291b191b SB |
764 | T object; |
765 | Unserialize(is, object, nType, nVersion); | |
766 | item = object; | |
e1ff849d SB |
767 | } else { |
768 | throw std::ios_base::failure("non-canonical optional discriminant"); | |
291b191b SB |
769 | } |
770 | } | |
771 | ||
772 | ||
773 | ||
5884044b SB |
774 | /** |
775 | * array | |
776 | */ | |
777 | template<typename T, std::size_t N> | |
778 | unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion) | |
779 | { | |
780 | unsigned int size = 0; | |
781 | for (size_t i = 0; i < N; i++) { | |
782 | size += GetSerializeSize(item[0], nType, nVersion); | |
783 | } | |
784 | return size; | |
785 | } | |
786 | ||
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) | |
789 | { | |
790 | for (size_t i = 0; i < N; i++) { | |
791 | Serialize(os, item[i], nType, nVersion); | |
792 | } | |
793 | } | |
794 | ||
795 | template<typename Stream, typename T, std::size_t N> | |
796 | void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion) | |
797 | { | |
798 | for (size_t i = 0; i < N; i++) { | |
799 | Unserialize(is, item[i], nType, nVersion); | |
800 | } | |
801 | } | |
802 | ||
0a61b0df | 803 | |
1c0aa911 MF |
804 | /** |
805 | * pair | |
806 | */ | |
0a61b0df | 807 | template<typename K, typename T> |
808 | unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion) | |
809 | { | |
810 | return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion); | |
811 | } | |
812 | ||
813 | template<typename Stream, typename K, typename T> | |
814 | void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion) | |
815 | { | |
816 | Serialize(os, item.first, nType, nVersion); | |
817 | Serialize(os, item.second, nType, nVersion); | |
818 | } | |
819 | ||
820 | template<typename Stream, typename K, typename T> | |
821 | void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion) | |
822 | { | |
823 | Unserialize(is, item.first, nType, nVersion); | |
824 | Unserialize(is, item.second, nType, nVersion); | |
825 | } | |
826 | ||
827 | ||
828 | ||
1c0aa911 MF |
829 | /** |
830 | * map | |
831 | */ | |
0a61b0df | 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) | |
834 | { | |
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); | |
838 | return nSize; | |
839 | } | |
840 | ||
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) | |
843 | { | |
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); | |
847 | } | |
848 | ||
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) | |
851 | { | |
852 | m.clear(); | |
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++) | |
856 | { | |
223b6f1b | 857 | std::pair<K, T> item; |
0a61b0df | 858 | Unserialize(is, item, nType, nVersion); |
859 | mi = m.insert(mi, item); | |
860 | } | |
861 | } | |
862 | ||
863 | ||
864 | ||
1c0aa911 MF |
865 | /** |
866 | * set | |
867 | */ | |
0a61b0df | 868 | template<typename K, typename Pred, typename A> |
869 | unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion) | |
870 | { | |
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); | |
874 | return nSize; | |
875 | } | |
876 | ||
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) | |
879 | { | |
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); | |
883 | } | |
884 | ||
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) | |
887 | { | |
888 | m.clear(); | |
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++) | |
892 | { | |
893 | K key; | |
894 | Unserialize(is, key, nType, nVersion); | |
895 | it = m.insert(it, key); | |
896 | } | |
897 | } | |
898 | ||
899 | ||
900 | ||
be74c80d JG |
901 | /** |
902 | * list | |
903 | */ | |
904 | template<typename T, typename A> | |
905 | unsigned int GetSerializeSize(const std::list<T, A>& l, int nType, int nVersion) | |
906 | { | |
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); | |
910 | return nSize; | |
911 | } | |
912 | ||
913 | template<typename Stream, typename T, typename A> | |
914 | void Serialize(Stream& os, const std::list<T, A>& l, int nType, int nVersion) | |
915 | { | |
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); | |
919 | } | |
920 | ||
921 | template<typename Stream, typename T, typename A> | |
922 | void Unserialize(Stream& is, std::list<T, A>& l, int nType, int nVersion) | |
923 | { | |
924 | l.clear(); | |
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++) | |
928 | { | |
929 | T item; | |
930 | Unserialize(is, item, nType, nVersion); | |
931 | l.push_back(item); | |
932 | } | |
933 | } | |
934 | ||
935 | ||
936 | ||
1c0aa911 MF |
937 | /** |
938 | * Support for ADD_SERIALIZE_METHODS and READWRITE macro | |
939 | */ | |
31e9a838 | 940 | struct CSerActionSerialize |
0a61b0df | 941 | { |
31e9a838 PW |
942 | bool ForRead() const { return false; } |
943 | }; | |
944 | struct CSerActionUnserialize | |
945 | { | |
946 | bool ForRead() const { return true; } | |
947 | }; | |
0a61b0df | 948 | |
949 | template<typename Stream, typename T> | |
31e9a838 | 950 | inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action) |
0a61b0df | 951 | { |
952 | ::Serialize(s, obj, nType, nVersion); | |
0a61b0df | 953 | } |
954 | ||
955 | template<typename Stream, typename T> | |
31e9a838 | 956 | inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action) |
0a61b0df | 957 | { |
958 | ::Unserialize(s, obj, nType, nVersion); | |
0a61b0df | 959 | } |
960 | ||
0a61b0df | 961 | |
962 | ||
963 | ||
964 | ||
965 | ||
966 | ||
f7a9a113 MC |
967 | |
968 | ||
b069750d PW |
969 | class CSizeComputer |
970 | { | |
971 | protected: | |
972 | size_t nSize; | |
973 | ||
974 | public: | |
975 | int nType; | |
976 | int nVersion; | |
977 | ||
978 | CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {} | |
979 | ||
8695a393 | 980 | CSizeComputer& write(const char *psz, size_t nSize) |
b069750d PW |
981 | { |
982 | this->nSize += nSize; | |
983 | return *this; | |
984 | } | |
985 | ||
986 | template<typename T> | |
987 | CSizeComputer& operator<<(const T& obj) | |
988 | { | |
989 | ::Serialize(*this, obj, nType, nVersion); | |
990 | return (*this); | |
991 | } | |
992 | ||
993 | size_t size() const { | |
994 | return nSize; | |
995 | } | |
996 | }; | |
997 | ||
093303a8 | 998 | #endif // BITCOIN_SERIALIZE_H |