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