version.h: separate client, net sections. Move more constants to this file.
[novacoin.git] / src / serialize.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_SERIALIZE_H
6 #define BITCOIN_SERIALIZE_H
7
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <set>
12 #include <cassert>
13 #include <limits>
14 #include <cstring>
15 #include <cstdio>
16
17 #include <boost/type_traits/is_fundamental.hpp>
18 #include <boost/tuple/tuple.hpp>
19 #include <boost/tuple/tuple_comparison.hpp>
20 #include <boost/tuple/tuple_io.hpp>
21
22 #include "allocators.h"
23 #include "version.h"
24
25 typedef long long  int64;
26 typedef unsigned long long  uint64;
27
28 class CScript;
29 class CDataStream;
30 class CAutoFile;
31 static const unsigned int MAX_SIZE = 0x02000000;
32
33 // Used to bypass the rule against non-const reference to temporary
34 // where it makes sense with wrappers such as CFlatData or CTxDB
35 template<typename T>
36 inline T& REF(const T& val)
37 {
38     return const_cast<T&>(val);
39 }
40
41 /////////////////////////////////////////////////////////////////
42 //
43 // Templates for serializing to anything that looks like a stream,
44 // i.e. anything that supports .read(char*, int) and .write(char*, int)
45 //
46
47 enum
48 {
49     // primary actions
50     SER_NETWORK         = (1 << 0),
51     SER_DISK            = (1 << 1),
52     SER_GETHASH         = (1 << 2),
53
54     // modifiers
55     SER_SKIPSIG         = (1 << 16),
56     SER_BLOCKHEADERONLY = (1 << 17),
57 };
58
59 #define IMPLEMENT_SERIALIZE(statements)    \
60     unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const  \
61     {                                           \
62         CSerActionGetSerializeSize ser_action;  \
63         const bool fGetSize = true;             \
64         const bool fWrite = false;              \
65         const bool fRead = false;               \
66         unsigned int nSerSize = 0;              \
67         ser_streamplaceholder s;                \
68         assert(fGetSize||fWrite||fRead); /* suppress warning */ \
69         s.nType = nType;                        \
70         s.nVersion = nVersion;                  \
71         {statements}                            \
72         return nSerSize;                        \
73     }                                           \
74     template<typename Stream>                   \
75     void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const  \
76     {                                           \
77         CSerActionSerialize ser_action;         \
78         const bool fGetSize = false;            \
79         const bool fWrite = true;               \
80         const bool fRead = false;               \
81         unsigned int nSerSize = 0;              \
82         assert(fGetSize||fWrite||fRead); /* suppress warning */ \
83         {statements}                            \
84     }                                           \
85     template<typename Stream>                   \
86     void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)  \
87     {                                           \
88         CSerActionUnserialize ser_action;       \
89         const bool fGetSize = false;            \
90         const bool fWrite = false;              \
91         const bool fRead = true;                \
92         unsigned int nSerSize = 0;              \
93         assert(fGetSize||fWrite||fRead); /* suppress warning */ \
94         {statements}                            \
95     }
96
97 #define READWRITE(obj)      (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
98
99
100
101
102
103
104 //
105 // Basic types
106 //
107 #define WRITEDATA(s, obj)   s.write((char*)&(obj), sizeof(obj))
108 #define READDATA(s, obj)    s.read((char*)&(obj), sizeof(obj))
109
110 inline unsigned int GetSerializeSize(char a,           int, int=0) { return sizeof(a); }
111 inline unsigned int GetSerializeSize(signed char a,    int, int=0) { return sizeof(a); }
112 inline unsigned int GetSerializeSize(unsigned char a,  int, int=0) { return sizeof(a); }
113 inline unsigned int GetSerializeSize(signed short a,   int, int=0) { return sizeof(a); }
114 inline unsigned int GetSerializeSize(unsigned short a, int, int=0) { return sizeof(a); }
115 inline unsigned int GetSerializeSize(signed int a,     int, int=0) { return sizeof(a); }
116 inline unsigned int GetSerializeSize(unsigned int a,   int, int=0) { return sizeof(a); }
117 inline unsigned int GetSerializeSize(signed long a,    int, int=0) { return sizeof(a); }
118 inline unsigned int GetSerializeSize(unsigned long a,  int, int=0) { return sizeof(a); }
119 inline unsigned int GetSerializeSize(int64 a,          int, int=0) { return sizeof(a); }
120 inline unsigned int GetSerializeSize(uint64 a,         int, int=0) { return sizeof(a); }
121 inline unsigned int GetSerializeSize(float a,          int, int=0) { return sizeof(a); }
122 inline unsigned int GetSerializeSize(double a,         int, int=0) { return sizeof(a); }
123
124 template<typename Stream> inline void Serialize(Stream& s, char a,           int, int=0) { WRITEDATA(s, a); }
125 template<typename Stream> inline void Serialize(Stream& s, signed char a,    int, int=0) { WRITEDATA(s, a); }
126 template<typename Stream> inline void Serialize(Stream& s, unsigned char a,  int, int=0) { WRITEDATA(s, a); }
127 template<typename Stream> inline void Serialize(Stream& s, signed short a,   int, int=0) { WRITEDATA(s, a); }
128 template<typename Stream> inline void Serialize(Stream& s, unsigned short a, int, int=0) { WRITEDATA(s, a); }
129 template<typename Stream> inline void Serialize(Stream& s, signed int a,     int, int=0) { WRITEDATA(s, a); }
130 template<typename Stream> inline void Serialize(Stream& s, unsigned int a,   int, int=0) { WRITEDATA(s, a); }
131 template<typename Stream> inline void Serialize(Stream& s, signed long a,    int, int=0) { WRITEDATA(s, a); }
132 template<typename Stream> inline void Serialize(Stream& s, unsigned long a,  int, int=0) { WRITEDATA(s, a); }
133 template<typename Stream> inline void Serialize(Stream& s, int64 a,          int, int=0) { WRITEDATA(s, a); }
134 template<typename Stream> inline void Serialize(Stream& s, uint64 a,         int, int=0) { WRITEDATA(s, a); }
135 template<typename Stream> inline void Serialize(Stream& s, float a,          int, int=0) { WRITEDATA(s, a); }
136 template<typename Stream> inline void Serialize(Stream& s, double a,         int, int=0) { WRITEDATA(s, a); }
137
138 template<typename Stream> inline void Unserialize(Stream& s, char& a,           int, int=0) { READDATA(s, a); }
139 template<typename Stream> inline void Unserialize(Stream& s, signed char& a,    int, int=0) { READDATA(s, a); }
140 template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a,  int, int=0) { READDATA(s, a); }
141 template<typename Stream> inline void Unserialize(Stream& s, signed short& a,   int, int=0) { READDATA(s, a); }
142 template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a, int, int=0) { READDATA(s, a); }
143 template<typename Stream> inline void Unserialize(Stream& s, signed int& a,     int, int=0) { READDATA(s, a); }
144 template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a,   int, int=0) { READDATA(s, a); }
145 template<typename Stream> inline void Unserialize(Stream& s, signed long& a,    int, int=0) { READDATA(s, a); }
146 template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a,  int, int=0) { READDATA(s, a); }
147 template<typename Stream> inline void Unserialize(Stream& s, int64& a,          int, int=0) { READDATA(s, a); }
148 template<typename Stream> inline void Unserialize(Stream& s, uint64& a,         int, int=0) { READDATA(s, a); }
149 template<typename Stream> inline void Unserialize(Stream& s, float& a,          int, int=0) { READDATA(s, a); }
150 template<typename Stream> inline void Unserialize(Stream& s, double& a,         int, int=0) { READDATA(s, a); }
151
152 inline unsigned int GetSerializeSize(bool a, int, int=0)                          { return sizeof(char); }
153 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0)    { char f=a; WRITEDATA(s, f); }
154 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }
155
156
157
158
159
160
161 //
162 // Compact size
163 //  size <  253        -- 1 byte
164 //  size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
165 //  size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
166 //  size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
167 //
168 inline unsigned int GetSizeOfCompactSize(uint64 nSize)
169 {
170     if (nSize < 253)             return sizeof(unsigned char);
171     else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
172     else if (nSize <= std::numeric_limits<unsigned int>::max())  return sizeof(unsigned char) + sizeof(unsigned int);
173     else                         return sizeof(unsigned char) + sizeof(uint64);
174 }
175
176 template<typename Stream>
177 void WriteCompactSize(Stream& os, uint64 nSize)
178 {
179     if (nSize < 253)
180     {
181         unsigned char chSize = nSize;
182         WRITEDATA(os, chSize);
183     }
184     else if (nSize <= std::numeric_limits<unsigned short>::max())
185     {
186         unsigned char chSize = 253;
187         unsigned short xSize = nSize;
188         WRITEDATA(os, chSize);
189         WRITEDATA(os, xSize);
190     }
191     else if (nSize <= std::numeric_limits<unsigned int>::max())
192     {
193         unsigned char chSize = 254;
194         unsigned int xSize = nSize;
195         WRITEDATA(os, chSize);
196         WRITEDATA(os, xSize);
197     }
198     else
199     {
200         unsigned char chSize = 255;
201         uint64 xSize = nSize;
202         WRITEDATA(os, chSize);
203         WRITEDATA(os, xSize);
204     }
205     return;
206 }
207
208 template<typename Stream>
209 uint64 ReadCompactSize(Stream& is)
210 {
211     unsigned char chSize;
212     READDATA(is, chSize);
213     uint64 nSizeRet = 0;
214     if (chSize < 253)
215     {
216         nSizeRet = chSize;
217     }
218     else if (chSize == 253)
219     {
220         unsigned short xSize;
221         READDATA(is, xSize);
222         nSizeRet = xSize;
223     }
224     else if (chSize == 254)
225     {
226         unsigned int xSize;
227         READDATA(is, xSize);
228         nSizeRet = xSize;
229     }
230     else
231     {
232         uint64 xSize;
233         READDATA(is, xSize);
234         nSizeRet = xSize;
235     }
236     if (nSizeRet > (uint64)MAX_SIZE)
237         throw std::ios_base::failure("ReadCompactSize() : size too large");
238     return nSizeRet;
239 }
240
241
242
243 #define FLATDATA(obj)   REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
244
245 /** Wrapper for serializing arrays and POD.
246  * There's a clever template way to make arrays serialize normally, but MSVC6 doesn't support it.
247  */
248 class CFlatData
249 {
250 protected:
251     char* pbegin;
252     char* pend;
253 public:
254     CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
255     char* begin() { return pbegin; }
256     const char* begin() const { return pbegin; }
257     char* end() { return pend; }
258     const char* end() const { return pend; }
259
260     unsigned int GetSerializeSize(int, int=0) const
261     {
262         return pend - pbegin;
263     }
264
265     template<typename Stream>
266     void Serialize(Stream& s, int, int=0) const
267     {
268         s.write(pbegin, pend - pbegin);
269     }
270
271     template<typename Stream>
272     void Unserialize(Stream& s, int, int=0)
273     {
274         s.read(pbegin, pend - pbegin);
275     }
276 };
277
278
279
280 /** string stored as a fixed length field */
281 template<std::size_t LEN>
282 class CFixedFieldString
283 {
284 protected:
285     const std::string* pcstr;
286     std::string* pstr;
287 public:
288     explicit CFixedFieldString(const std::string& str) : pcstr(&str), pstr(NULL) { }
289     explicit CFixedFieldString(std::string& str) : pcstr(&str), pstr(&str) { }
290
291     unsigned int GetSerializeSize(int, int=0) const
292     {
293         return LEN;
294     }
295
296     template<typename Stream>
297     void Serialize(Stream& s, int, int=0) const
298     {
299         char pszBuf[LEN];
300         strncpy(pszBuf, pcstr->c_str(), LEN);
301         s.write(pszBuf, LEN);
302     }
303
304     template<typename Stream>
305     void Unserialize(Stream& s, int, int=0)
306     {
307         if (pstr == NULL)
308             throw std::ios_base::failure("CFixedFieldString::Unserialize : trying to unserialize to const string");
309         char pszBuf[LEN+1];
310         s.read(pszBuf, LEN);
311         pszBuf[LEN] = '\0';
312         *pstr = pszBuf;
313     }
314 };
315
316
317
318
319
320 //
321 // Forward declarations
322 //
323
324 // string
325 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
326 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
327 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
328
329 // vector
330 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
331 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
332 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion=PROTOCOL_VERSION);
333 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
334 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
335 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion=PROTOCOL_VERSION);
336 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
337 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
338 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion=PROTOCOL_VERSION);
339
340 // others derived from vector
341 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion=PROTOCOL_VERSION);
342 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion=PROTOCOL_VERSION);
343 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion=PROTOCOL_VERSION);
344
345 // pair
346 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion=PROTOCOL_VERSION);
347 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion=PROTOCOL_VERSION);
348 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion=PROTOCOL_VERSION);
349
350 // 3 tuple
351 template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=PROTOCOL_VERSION);
352 template<typename Stream, typename T0, typename T1, typename T2> void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=PROTOCOL_VERSION);
353 template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion=PROTOCOL_VERSION);
354
355 // 4 tuple
356 template<typename T0, typename T1, typename T2, typename T3> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=PROTOCOL_VERSION);
357 template<typename Stream, typename T0, typename T1, typename T2, typename T3> void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=PROTOCOL_VERSION);
358 template<typename Stream, typename T0, typename T1, typename T2, typename T3> void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=PROTOCOL_VERSION);
359
360 // map
361 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion=PROTOCOL_VERSION);
362 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=PROTOCOL_VERSION);
363 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=PROTOCOL_VERSION);
364
365 // set
366 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion=PROTOCOL_VERSION);
367 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion=PROTOCOL_VERSION);
368 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion=PROTOCOL_VERSION);
369
370
371
372
373
374 //
375 // If none of the specialized versions above matched, default to calling member function.
376 // "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
377 // The compiler will only cast int to long if none of the other templates matched.
378 // Thanks to Boost serialization for this idea.
379 //
380 template<typename T>
381 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion=PROTOCOL_VERSION)
382 {
383     return a.GetSerializeSize((int)nType, nVersion);
384 }
385
386 template<typename Stream, typename T>
387 inline void Serialize(Stream& os, const T& a, long nType, int nVersion=PROTOCOL_VERSION)
388 {
389     a.Serialize(os, (int)nType, nVersion);
390 }
391
392 template<typename Stream, typename T>
393 inline void Unserialize(Stream& is, T& a, long nType, int nVersion=PROTOCOL_VERSION)
394 {
395     a.Unserialize(is, (int)nType, nVersion);
396 }
397
398
399
400
401
402 //
403 // string
404 //
405 template<typename C>
406 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
407 {
408     return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
409 }
410
411 template<typename Stream, typename C>
412 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
413 {
414     WriteCompactSize(os, str.size());
415     if (!str.empty())
416         os.write((char*)&str[0], str.size() * sizeof(str[0]));
417 }
418
419 template<typename Stream, typename C>
420 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
421 {
422     unsigned int nSize = ReadCompactSize(is);
423     str.resize(nSize);
424     if (nSize != 0)
425         is.read((char*)&str[0], nSize * sizeof(str[0]));
426 }
427
428
429
430 //
431 // vector
432 //
433 template<typename T, typename A>
434 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
435 {
436     return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
437 }
438
439 template<typename T, typename A>
440 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
441 {
442     unsigned int nSize = GetSizeOfCompactSize(v.size());
443     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
444         nSize += GetSerializeSize((*vi), nType, nVersion);
445     return nSize;
446 }
447
448 template<typename T, typename A>
449 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
450 {
451     return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental<T>());
452 }
453
454
455 template<typename Stream, typename T, typename A>
456 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
457 {
458     WriteCompactSize(os, v.size());
459     if (!v.empty())
460         os.write((char*)&v[0], v.size() * sizeof(T));
461 }
462
463 template<typename Stream, typename T, typename A>
464 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
465 {
466     WriteCompactSize(os, v.size());
467     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
468         ::Serialize(os, (*vi), nType, nVersion);
469 }
470
471 template<typename Stream, typename T, typename A>
472 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
473 {
474     Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
475 }
476
477
478 template<typename Stream, typename T, typename A>
479 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
480 {
481     //unsigned int nSize = ReadCompactSize(is);
482     //v.resize(nSize);
483     //is.read((char*)&v[0], nSize * sizeof(T));
484
485     // Limit size per read so bogus size value won't cause out of memory
486     v.clear();
487     unsigned int nSize = ReadCompactSize(is);
488     unsigned int i = 0;
489     while (i < nSize)
490     {
491         unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
492         v.resize(i + blk);
493         is.read((char*)&v[i], blk * sizeof(T));
494         i += blk;
495     }
496 }
497
498 template<typename Stream, typename T, typename A>
499 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
500 {
501     //unsigned int nSize = ReadCompactSize(is);
502     //v.resize(nSize);
503     //for (std::vector<T, A>::iterator vi = v.begin(); vi != v.end(); ++vi)
504     //    Unserialize(is, (*vi), nType, nVersion);
505
506     v.clear();
507     unsigned int nSize = ReadCompactSize(is);
508     unsigned int i = 0;
509     unsigned int nMid = 0;
510     while (nMid < nSize)
511     {
512         nMid += 5000000 / sizeof(T);
513         if (nMid > nSize)
514             nMid = nSize;
515         v.resize(nMid);
516         for (; i < nMid; i++)
517             Unserialize(is, v[i], nType, nVersion);
518     }
519 }
520
521 template<typename Stream, typename T, typename A>
522 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
523 {
524     Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental<T>());
525 }
526
527
528
529 //
530 // others derived from vector
531 //
532 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
533 {
534     return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
535 }
536
537 template<typename Stream>
538 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
539 {
540     Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
541 }
542
543 template<typename Stream>
544 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
545 {
546     Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
547 }
548
549
550
551 //
552 // pair
553 //
554 template<typename K, typename T>
555 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
556 {
557     return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
558 }
559
560 template<typename Stream, typename K, typename T>
561 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
562 {
563     Serialize(os, item.first, nType, nVersion);
564     Serialize(os, item.second, nType, nVersion);
565 }
566
567 template<typename Stream, typename K, typename T>
568 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
569 {
570     Unserialize(is, item.first, nType, nVersion);
571     Unserialize(is, item.second, nType, nVersion);
572 }
573
574
575
576 //
577 // 3 tuple
578 //
579 template<typename T0, typename T1, typename T2>
580 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
581 {
582     unsigned int nSize = 0;
583     nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
584     nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
585     nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
586     return nSize;
587 }
588
589 template<typename Stream, typename T0, typename T1, typename T2>
590 void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
591 {
592     Serialize(os, boost::get<0>(item), nType, nVersion);
593     Serialize(os, boost::get<1>(item), nType, nVersion);
594     Serialize(os, boost::get<2>(item), nType, nVersion);
595 }
596
597 template<typename Stream, typename T0, typename T1, typename T2>
598 void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
599 {
600     Unserialize(is, boost::get<0>(item), nType, nVersion);
601     Unserialize(is, boost::get<1>(item), nType, nVersion);
602     Unserialize(is, boost::get<2>(item), nType, nVersion);
603 }
604
605
606
607 //
608 // 4 tuple
609 //
610 template<typename T0, typename T1, typename T2, typename T3>
611 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
612 {
613     unsigned int nSize = 0;
614     nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
615     nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
616     nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
617     nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion);
618     return nSize;
619 }
620
621 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
622 void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
623 {
624     Serialize(os, boost::get<0>(item), nType, nVersion);
625     Serialize(os, boost::get<1>(item), nType, nVersion);
626     Serialize(os, boost::get<2>(item), nType, nVersion);
627     Serialize(os, boost::get<3>(item), nType, nVersion);
628 }
629
630 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
631 void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
632 {
633     Unserialize(is, boost::get<0>(item), nType, nVersion);
634     Unserialize(is, boost::get<1>(item), nType, nVersion);
635     Unserialize(is, boost::get<2>(item), nType, nVersion);
636     Unserialize(is, boost::get<3>(item), nType, nVersion);
637 }
638
639
640
641 //
642 // map
643 //
644 template<typename K, typename T, typename Pred, typename A>
645 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
646 {
647     unsigned int nSize = GetSizeOfCompactSize(m.size());
648     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
649         nSize += GetSerializeSize((*mi), nType, nVersion);
650     return nSize;
651 }
652
653 template<typename Stream, typename K, typename T, typename Pred, typename A>
654 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
655 {
656     WriteCompactSize(os, m.size());
657     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
658         Serialize(os, (*mi), nType, nVersion);
659 }
660
661 template<typename Stream, typename K, typename T, typename Pred, typename A>
662 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
663 {
664     m.clear();
665     unsigned int nSize = ReadCompactSize(is);
666     typename std::map<K, T, Pred, A>::iterator mi = m.begin();
667     for (unsigned int i = 0; i < nSize; i++)
668     {
669         std::pair<K, T> item;
670         Unserialize(is, item, nType, nVersion);
671         mi = m.insert(mi, item);
672     }
673 }
674
675
676
677 //
678 // set
679 //
680 template<typename K, typename Pred, typename A>
681 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
682 {
683     unsigned int nSize = GetSizeOfCompactSize(m.size());
684     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
685         nSize += GetSerializeSize((*it), nType, nVersion);
686     return nSize;
687 }
688
689 template<typename Stream, typename K, typename Pred, typename A>
690 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
691 {
692     WriteCompactSize(os, m.size());
693     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
694         Serialize(os, (*it), nType, nVersion);
695 }
696
697 template<typename Stream, typename K, typename Pred, typename A>
698 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
699 {
700     m.clear();
701     unsigned int nSize = ReadCompactSize(is);
702     typename std::set<K, Pred, A>::iterator it = m.begin();
703     for (unsigned int i = 0; i < nSize; i++)
704     {
705         K key;
706         Unserialize(is, key, nType, nVersion);
707         it = m.insert(it, key);
708     }
709 }
710
711
712
713 //
714 // Support for IMPLEMENT_SERIALIZE and READWRITE macro
715 //
716 class CSerActionGetSerializeSize { };
717 class CSerActionSerialize { };
718 class CSerActionUnserialize { };
719
720 template<typename Stream, typename T>
721 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
722 {
723     return ::GetSerializeSize(obj, nType, nVersion);
724 }
725
726 template<typename Stream, typename T>
727 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
728 {
729     ::Serialize(s, obj, nType, nVersion);
730     return 0;
731 }
732
733 template<typename Stream, typename T>
734 inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
735 {
736     ::Unserialize(s, obj, nType, nVersion);
737     return 0;
738 }
739
740 struct ser_streamplaceholder
741 {
742     int nType;
743     int nVersion;
744 };
745
746
747
748
749
750
751
752
753
754
755
756
757 /** Double ended buffer combining vector and stream-like interfaces.
758  *
759  * >> and << read and write unformatted data using the above serialization templates.
760  * Fills with data in linear time; some stringstream implementations take N^2 time.
761  */
762 class CDataStream
763 {
764 protected:
765     typedef std::vector<char, zero_after_free_allocator<char> > vector_type;
766     vector_type vch;
767     unsigned int nReadPos;
768     short state;
769     short exceptmask;
770 public:
771     int nType;
772     int nVersion;
773
774     typedef vector_type::allocator_type   allocator_type;
775     typedef vector_type::size_type        size_type;
776     typedef vector_type::difference_type  difference_type;
777     typedef vector_type::reference        reference;
778     typedef vector_type::const_reference  const_reference;
779     typedef vector_type::value_type       value_type;
780     typedef vector_type::iterator         iterator;
781     typedef vector_type::const_iterator   const_iterator;
782     typedef vector_type::reverse_iterator reverse_iterator;
783
784     explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION)
785     {
786         Init(nTypeIn, nVersionIn);
787     }
788
789     CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(pbegin, pend)
790     {
791         Init(nTypeIn, nVersionIn);
792     }
793
794 #if !defined(_MSC_VER) || _MSC_VER >= 1300
795     CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(pbegin, pend)
796     {
797         Init(nTypeIn, nVersionIn);
798     }
799 #endif
800
801     CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(vchIn.begin(), vchIn.end())
802     {
803         Init(nTypeIn, nVersionIn);
804     }
805
806     CDataStream(const std::vector<char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(vchIn.begin(), vchIn.end())
807     {
808         Init(nTypeIn, nVersionIn);
809     }
810
811     CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
812     {
813         Init(nTypeIn, nVersionIn);
814     }
815
816     void Init(int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION)
817     {
818         nReadPos = 0;
819         nType = nTypeIn;
820         nVersion = nVersionIn;
821         state = 0;
822         exceptmask = std::ios::badbit | std::ios::failbit;
823     }
824
825     CDataStream& operator+=(const CDataStream& b)
826     {
827         vch.insert(vch.end(), b.begin(), b.end());
828         return *this;
829     }
830
831     friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
832     {
833         CDataStream ret = a;
834         ret += b;
835         return (ret);
836     }
837
838     std::string str() const
839     {
840         return (std::string(begin(), end()));
841     }
842
843
844     //
845     // Vector subset
846     //
847     const_iterator begin() const                     { return vch.begin() + nReadPos; }
848     iterator begin()                                 { return vch.begin() + nReadPos; }
849     const_iterator end() const                       { return vch.end(); }
850     iterator end()                                   { return vch.end(); }
851     size_type size() const                           { return vch.size() - nReadPos; }
852     bool empty() const                               { return vch.size() == nReadPos; }
853     void resize(size_type n, value_type c=0)         { vch.resize(n + nReadPos, c); }
854     void reserve(size_type n)                        { vch.reserve(n + nReadPos); }
855     const_reference operator[](size_type pos) const  { return vch[pos + nReadPos]; }
856     reference operator[](size_type pos)              { return vch[pos + nReadPos]; }
857     void clear()                                     { vch.clear(); nReadPos = 0; }
858     iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
859     void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
860
861     void insert(iterator it, const_iterator first, const_iterator last)
862     {
863         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
864         {
865             // special case for inserting at the front when there's room
866             nReadPos -= (last - first);
867             memcpy(&vch[nReadPos], &first[0], last - first);
868         }
869         else
870             vch.insert(it, first, last);
871     }
872
873     void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
874     {
875         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
876         {
877             // special case for inserting at the front when there's room
878             nReadPos -= (last - first);
879             memcpy(&vch[nReadPos], &first[0], last - first);
880         }
881         else
882             vch.insert(it, first, last);
883     }
884
885 #if !defined(_MSC_VER) || _MSC_VER >= 1300
886     void insert(iterator it, const char* first, const char* last)
887     {
888         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
889         {
890             // special case for inserting at the front when there's room
891             nReadPos -= (last - first);
892             memcpy(&vch[nReadPos], &first[0], last - first);
893         }
894         else
895             vch.insert(it, first, last);
896     }
897 #endif
898
899     iterator erase(iterator it)
900     {
901         if (it == vch.begin() + nReadPos)
902         {
903             // special case for erasing from the front
904             if (++nReadPos >= vch.size())
905             {
906                 // whenever we reach the end, we take the opportunity to clear the buffer
907                 nReadPos = 0;
908                 return vch.erase(vch.begin(), vch.end());
909             }
910             return vch.begin() + nReadPos;
911         }
912         else
913             return vch.erase(it);
914     }
915
916     iterator erase(iterator first, iterator last)
917     {
918         if (first == vch.begin() + nReadPos)
919         {
920             // special case for erasing from the front
921             if (last == vch.end())
922             {
923                 nReadPos = 0;
924                 return vch.erase(vch.begin(), vch.end());
925             }
926             else
927             {
928                 nReadPos = (last - vch.begin());
929                 return last;
930             }
931         }
932         else
933             return vch.erase(first, last);
934     }
935
936     inline void Compact()
937     {
938         vch.erase(vch.begin(), vch.begin() + nReadPos);
939         nReadPos = 0;
940     }
941
942     bool Rewind(size_type n)
943     {
944         // Rewind by n characters if the buffer hasn't been compacted yet
945         if (n > nReadPos)
946             return false;
947         nReadPos -= n;
948         return true;
949     }
950
951
952     //
953     // Stream subset
954     //
955     void setstate(short bits, const char* psz)
956     {
957         state |= bits;
958         if (state & exceptmask)
959             throw std::ios_base::failure(psz);
960     }
961
962     bool eof() const             { return size() == 0; }
963     bool fail() const            { return state & (std::ios::badbit | std::ios::failbit); }
964     bool good() const            { return !eof() && (state == 0); }
965     void clear(short n)          { state = n; }  // name conflict with vector clear()
966     short exceptions()           { return exceptmask; }
967     short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
968     CDataStream* rdbuf()         { return this; }
969     int in_avail()               { return size(); }
970
971     void SetType(int n)          { nType = n; }
972     int GetType()                { return nType; }
973     void SetVersion(int n)       { nVersion = n; }
974     int GetVersion()             { return nVersion; }
975     void ReadVersion()           { *this >> nVersion; }
976     void WriteVersion()          { *this << nVersion; }
977
978     CDataStream& read(char* pch, int nSize)
979     {
980         // Read from the beginning of the buffer
981         assert(nSize >= 0);
982         unsigned int nReadPosNext = nReadPos + nSize;
983         if (nReadPosNext >= vch.size())
984         {
985             if (nReadPosNext > vch.size())
986             {
987                 setstate(std::ios::failbit, "CDataStream::read() : end of data");
988                 memset(pch, 0, nSize);
989                 nSize = vch.size() - nReadPos;
990             }
991             memcpy(pch, &vch[nReadPos], nSize);
992             nReadPos = 0;
993             vch.clear();
994             return (*this);
995         }
996         memcpy(pch, &vch[nReadPos], nSize);
997         nReadPos = nReadPosNext;
998         return (*this);
999     }
1000
1001     CDataStream& ignore(int nSize)
1002     {
1003         // Ignore from the beginning of the buffer
1004         assert(nSize >= 0);
1005         unsigned int nReadPosNext = nReadPos + nSize;
1006         if (nReadPosNext >= vch.size())
1007         {
1008             if (nReadPosNext > vch.size())
1009             {
1010                 setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
1011                 nSize = vch.size() - nReadPos;
1012             }
1013             nReadPos = 0;
1014             vch.clear();
1015             return (*this);
1016         }
1017         nReadPos = nReadPosNext;
1018         return (*this);
1019     }
1020
1021     CDataStream& write(const char* pch, int nSize)
1022     {
1023         // Write to the end of the buffer
1024         assert(nSize >= 0);
1025         vch.insert(vch.end(), pch, pch + nSize);
1026         return (*this);
1027     }
1028
1029     template<typename Stream>
1030     void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
1031     {
1032         // Special case: stream << stream concatenates like stream += stream
1033         if (!vch.empty())
1034             s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
1035     }
1036
1037     template<typename T>
1038     unsigned int GetSerializeSize(const T& obj)
1039     {
1040         // Tells the size of the object if serialized to this stream
1041         return ::GetSerializeSize(obj, nType, nVersion);
1042     }
1043
1044     template<typename T>
1045     CDataStream& operator<<(const T& obj)
1046     {
1047         // Serialize to this stream
1048         ::Serialize(*this, obj, nType, nVersion);
1049         return (*this);
1050     }
1051
1052     template<typename T>
1053     CDataStream& operator>>(T& obj)
1054     {
1055         // Unserialize from this stream
1056         ::Unserialize(*this, obj, nType, nVersion);
1057         return (*this);
1058     }
1059 };
1060
1061 #ifdef TESTCDATASTREAM
1062 // VC6sp6
1063 // CDataStream:
1064 // n=1000       0 seconds
1065 // n=2000       0 seconds
1066 // n=4000       0 seconds
1067 // n=8000       0 seconds
1068 // n=16000      0 seconds
1069 // n=32000      0 seconds
1070 // n=64000      1 seconds
1071 // n=128000     1 seconds
1072 // n=256000     2 seconds
1073 // n=512000     4 seconds
1074 // n=1024000    8 seconds
1075 // n=2048000    16 seconds
1076 // n=4096000    32 seconds
1077 // stringstream:
1078 // n=1000       1 seconds
1079 // n=2000       1 seconds
1080 // n=4000       13 seconds
1081 // n=8000       87 seconds
1082 // n=16000      400 seconds
1083 // n=32000      1660 seconds
1084 // n=64000      6749 seconds
1085 // n=128000     27241 seconds
1086 // n=256000     109804 seconds
1087 #include <iostream>
1088 int main(int argc, char *argv[])
1089 {
1090     vector<unsigned char> vch(0xcc, 250);
1091     printf("CDataStream:\n");
1092     for (int n = 1000; n <= 4500000; n *= 2)
1093     {
1094         CDataStream ss;
1095         time_t nStart = time(NULL);
1096         for (int i = 0; i < n; i++)
1097             ss.write((char*)&vch[0], vch.size());
1098         printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
1099     }
1100     printf("stringstream:\n");
1101     for (int n = 1000; n <= 4500000; n *= 2)
1102     {
1103         stringstream ss;
1104         time_t nStart = time(NULL);
1105         for (int i = 0; i < n; i++)
1106             ss.write((char*)&vch[0], vch.size());
1107         printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
1108     }
1109 }
1110 #endif
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 /** RAII wrapper for FILE*.
1122  *
1123  * Will automatically close the file when it goes out of scope if not null.
1124  * If you're returning the file pointer, return file.release().
1125  * If you need to close the file early, use file.fclose() instead of fclose(file).
1126  */
1127 class CAutoFile
1128 {
1129 protected:
1130     FILE* file;
1131     short state;
1132     short exceptmask;
1133 public:
1134     int nType;
1135     int nVersion;
1136
1137     typedef FILE element_type;
1138
1139     CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=PROTOCOL_VERSION)
1140     {
1141         file = filenew;
1142         nType = nTypeIn;
1143         nVersion = nVersionIn;
1144         state = 0;
1145         exceptmask = std::ios::badbit | std::ios::failbit;
1146     }
1147
1148     ~CAutoFile()
1149     {
1150         fclose();
1151     }
1152
1153     void fclose()
1154     {
1155         if (file != NULL && file != stdin && file != stdout && file != stderr)
1156             ::fclose(file);
1157         file = NULL;
1158     }
1159
1160     FILE* release()             { FILE* ret = file; file = NULL; return ret; }
1161     operator FILE*()            { return file; }
1162     FILE* operator->()          { return file; }
1163     FILE& operator*()           { return *file; }
1164     FILE** operator&()          { return &file; }
1165     FILE* operator=(FILE* pnew) { return file = pnew; }
1166     bool operator!()            { return (file == NULL); }
1167
1168
1169     //
1170     // Stream subset
1171     //
1172     void setstate(short bits, const char* psz)
1173     {
1174         state |= bits;
1175         if (state & exceptmask)
1176             throw std::ios_base::failure(psz);
1177     }
1178
1179     bool fail() const            { return state & (std::ios::badbit | std::ios::failbit); }
1180     bool good() const            { return state == 0; }
1181     void clear(short n = 0)      { state = n; }
1182     short exceptions()           { return exceptmask; }
1183     short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
1184
1185     void SetType(int n)          { nType = n; }
1186     int GetType()                { return nType; }
1187     void SetVersion(int n)       { nVersion = n; }
1188     int GetVersion()             { return nVersion; }
1189     void ReadVersion()           { *this >> nVersion; }
1190     void WriteVersion()          { *this << nVersion; }
1191
1192     CAutoFile& read(char* pch, int nSize)
1193     {
1194         if (!file)
1195             throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
1196         if (fread(pch, 1, nSize, file) != nSize)
1197             setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
1198         return (*this);
1199     }
1200
1201     CAutoFile& write(const char* pch, int nSize)
1202     {
1203         if (!file)
1204             throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
1205         if (fwrite(pch, 1, nSize, file) != nSize)
1206             setstate(std::ios::failbit, "CAutoFile::write : write failed");
1207         return (*this);
1208     }
1209
1210     template<typename T>
1211     unsigned int GetSerializeSize(const T& obj)
1212     {
1213         // Tells the size of the object if serialized to this stream
1214         return ::GetSerializeSize(obj, nType, nVersion);
1215     }
1216
1217     template<typename T>
1218     CAutoFile& operator<<(const T& obj)
1219     {
1220         // Serialize to this stream
1221         if (!file)
1222             throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
1223         ::Serialize(*this, obj, nType, nVersion);
1224         return (*this);
1225     }
1226
1227     template<typename T>
1228     CAutoFile& operator>>(T& obj)
1229     {
1230         // Unserialize from this stream
1231         if (!file)
1232             throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
1233         ::Unserialize(*this, obj, nType, nVersion);
1234         return (*this);
1235     }
1236 };
1237
1238 #endif