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