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