-
+ 0DCF98014ABE06B33AB296A747E42E88770A87F8D4E408BE4FC1EBE6B02312695CC129D0B58C0886BF8521B6D2C79BF6F2C33E19C03AEE8F5357D8DB779829ADbitcoin/src/serialize.h(0 . 0)(1 . 1349)
19605 // Copyright (c) 2009-2010 Satoshi Nakamoto
19606 // Copyright (c) 2009-2012 The Bitcoin developers
19607 // Distributed under the MIT/X11 software license, see the accompanying
19608 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
19609 #ifndef BITCOIN_SERIALIZE_H
19610 #define BITCOIN_SERIALIZE_H
19611
19612 #include <string>
19613 #include <vector>
19614 #include <map>
19615 #include <set>
19616 #include <cassert>
19617 #include <climits>
19618 #include <cstring>
19619 #include <cstdio>
19620
19621 #include <boost/type_traits/is_fundamental.hpp>
19622 #include <boost/tuple/tuple.hpp>
19623 #include <boost/tuple/tuple_comparison.hpp>
19624 #include <boost/tuple/tuple_io.hpp>
19625
19626 #if defined(_MSC_VER) || defined(__BORLANDC__)
19627 typedef __int64 int64;
19628 typedef unsigned __int64 uint64;
19629 #else
19630 typedef long long int64;
19631 typedef unsigned long long uint64;
19632 #endif
19633 #if defined(_MSC_VER) && _MSC_VER < 1300
19634 #define for if (false) ; else for
19635 #endif
19636
19637 #ifdef WIN32
19638 #include <windows.h>
19639 // This is used to attempt to keep keying material out of swap
19640 // Note that VirtualLock does not provide this as a guarantee on Windows,
19641 // but, in practice, memory that has been VirtualLock'd almost never gets written to
19642 // the pagefile except in rare circumstances where memory is extremely low.
19643 #include <windows.h>
19644 #define mlock(p, n) VirtualLock((p), (n));
19645 #define munlock(p, n) VirtualUnlock((p), (n));
19646 #else
19647 #include <sys/mman.h>
19648 #include <limits.h>
19649 /* This comes from limits.h if it's not defined there set a sane default */
19650 #ifndef PAGESIZE
19651 #include <unistd.h>
19652 #define PAGESIZE sysconf(_SC_PAGESIZE)
19653 #endif
19654 #define mlock(a,b) \
19655 mlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
19656 (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
19657 #define munlock(a,b) \
19658 munlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
19659 (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
19660 #endif
19661
19662 class CScript;
19663 class CDataStream;
19664 class CAutoFile;
19665 static const unsigned int MAX_SIZE = 0x02000000;
19666
19667 static const int VERSION = 50300;
19668 static const char* pszSubVer = "";
19669 static const bool VERSION_IS_BETA = true;
19670
19671 // Used to bypass the rule against non-const reference to temporary
19672 // where it makes sense with wrappers such as CFlatData or CTxDB
19673 template<typename T>
19674 inline T& REF(const T& val)
19675 {
19676 return const_cast<T&>(val);
19677 }
19678
19679 /////////////////////////////////////////////////////////////////
19680 //
19681 // Templates for serializing to anything that looks like a stream,
19682 // i.e. anything that supports .read(char*, int) and .write(char*, int)
19683 //
19684
19685 enum
19686 {
19687 // primary actions
19688 SER_NETWORK = (1 << 0),
19689 SER_DISK = (1 << 1),
19690 SER_GETHASH = (1 << 2),
19691
19692 // modifiers
19693 SER_SKIPSIG = (1 << 16),
19694 SER_BLOCKHEADERONLY = (1 << 17),
19695 };
19696
19697 #define IMPLEMENT_SERIALIZE(statements) \
19698 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const \
19699 { \
19700 CSerActionGetSerializeSize ser_action; \
19701 const bool fGetSize = true; \
19702 const bool fWrite = false; \
19703 const bool fRead = false; \
19704 unsigned int nSerSize = 0; \
19705 ser_streamplaceholder s; \
19706 assert(fGetSize||fWrite||fRead); /* suppress warning */ \
19707 s.nType = nType; \
19708 s.nVersion = nVersion; \
19709 {statements} \
19710 return nSerSize; \
19711 } \
19712 template<typename Stream> \
19713 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const \
19714 { \
19715 CSerActionSerialize ser_action; \
19716 const bool fGetSize = false; \
19717 const bool fWrite = true; \
19718 const bool fRead = false; \
19719 unsigned int nSerSize = 0; \
19720 assert(fGetSize||fWrite||fRead); /* suppress warning */ \
19721 {statements} \
19722 } \
19723 template<typename Stream> \
19724 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) \
19725 { \
19726 CSerActionUnserialize ser_action; \
19727 const bool fGetSize = false; \
19728 const bool fWrite = false; \
19729 const bool fRead = true; \
19730 unsigned int nSerSize = 0; \
19731 assert(fGetSize||fWrite||fRead); /* suppress warning */ \
19732 {statements} \
19733 }
19734
19735 #define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
19736
19737
19738
19739
19740
19741
19742 //
19743 // Basic types
19744 //
19745 #define WRITEDATA(s, obj) s.write((char*)&(obj), sizeof(obj))
19746 #define READDATA(s, obj) s.read((char*)&(obj), sizeof(obj))
19747
19748 inline unsigned int GetSerializeSize(char a, int, int=0) { return sizeof(a); }
19749 inline unsigned int GetSerializeSize(signed char a, int, int=0) { return sizeof(a); }
19750 inline unsigned int GetSerializeSize(unsigned char a, int, int=0) { return sizeof(a); }
19751 inline unsigned int GetSerializeSize(signed short a, int, int=0) { return sizeof(a); }
19752 inline unsigned int GetSerializeSize(unsigned short a, int, int=0) { return sizeof(a); }
19753 inline unsigned int GetSerializeSize(signed int a, int, int=0) { return sizeof(a); }
19754 inline unsigned int GetSerializeSize(unsigned int a, int, int=0) { return sizeof(a); }
19755 inline unsigned int GetSerializeSize(signed long a, int, int=0) { return sizeof(a); }
19756 inline unsigned int GetSerializeSize(unsigned long a, int, int=0) { return sizeof(a); }
19757 inline unsigned int GetSerializeSize(int64 a, int, int=0) { return sizeof(a); }
19758 inline unsigned int GetSerializeSize(uint64 a, int, int=0) { return sizeof(a); }
19759 inline unsigned int GetSerializeSize(float a, int, int=0) { return sizeof(a); }
19760 inline unsigned int GetSerializeSize(double a, int, int=0) { return sizeof(a); }
19761
19762 template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { WRITEDATA(s, a); }
19763 template<typename Stream> inline void Serialize(Stream& s, signed char a, int, int=0) { WRITEDATA(s, a); }
19764 template<typename Stream> inline void Serialize(Stream& s, unsigned char a, int, int=0) { WRITEDATA(s, a); }
19765 template<typename Stream> inline void Serialize(Stream& s, signed short a, int, int=0) { WRITEDATA(s, a); }
19766 template<typename Stream> inline void Serialize(Stream& s, unsigned short a, int, int=0) { WRITEDATA(s, a); }
19767 template<typename Stream> inline void Serialize(Stream& s, signed int a, int, int=0) { WRITEDATA(s, a); }
19768 template<typename Stream> inline void Serialize(Stream& s, unsigned int a, int, int=0) { WRITEDATA(s, a); }
19769 template<typename Stream> inline void Serialize(Stream& s, signed long a, int, int=0) { WRITEDATA(s, a); }
19770 template<typename Stream> inline void Serialize(Stream& s, unsigned long a, int, int=0) { WRITEDATA(s, a); }
19771 template<typename Stream> inline void Serialize(Stream& s, int64 a, int, int=0) { WRITEDATA(s, a); }
19772 template<typename Stream> inline void Serialize(Stream& s, uint64 a, int, int=0) { WRITEDATA(s, a); }
19773 template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { WRITEDATA(s, a); }
19774 template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { WRITEDATA(s, a); }
19775
19776 template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { READDATA(s, a); }
19777 template<typename Stream> inline void Unserialize(Stream& s, signed char& a, int, int=0) { READDATA(s, a); }
19778 template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a, int, int=0) { READDATA(s, a); }
19779 template<typename Stream> inline void Unserialize(Stream& s, signed short& a, int, int=0) { READDATA(s, a); }
19780 template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a, int, int=0) { READDATA(s, a); }
19781 template<typename Stream> inline void Unserialize(Stream& s, signed int& a, int, int=0) { READDATA(s, a); }
19782 template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a, int, int=0) { READDATA(s, a); }
19783 template<typename Stream> inline void Unserialize(Stream& s, signed long& a, int, int=0) { READDATA(s, a); }
19784 template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a, int, int=0) { READDATA(s, a); }
19785 template<typename Stream> inline void Unserialize(Stream& s, int64& a, int, int=0) { READDATA(s, a); }
19786 template<typename Stream> inline void Unserialize(Stream& s, uint64& a, int, int=0) { READDATA(s, a); }
19787 template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { READDATA(s, a); }
19788 template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { READDATA(s, a); }
19789
19790 inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
19791 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; WRITEDATA(s, f); }
19792 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }
19793
19794
19795
19796
19797
19798
19799 //
19800 // Compact size
19801 // size < 253 -- 1 byte
19802 // size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
19803 // size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
19804 // size > UINT_MAX -- 9 bytes (255 + 8 bytes)
19805 //
19806 inline unsigned int GetSizeOfCompactSize(uint64 nSize)
19807 {
19808 if (nSize < 253) return sizeof(unsigned char);
19809 else if (nSize <= USHRT_MAX) return sizeof(unsigned char) + sizeof(unsigned short);
19810 else if (nSize <= UINT_MAX) return sizeof(unsigned char) + sizeof(unsigned int);
19811 else return sizeof(unsigned char) + sizeof(uint64);
19812 }
19813
19814 template<typename Stream>
19815 void WriteCompactSize(Stream& os, uint64 nSize)
19816 {
19817 if (nSize < 253)
19818 {
19819 unsigned char chSize = nSize;
19820 WRITEDATA(os, chSize);
19821 }
19822 else if (nSize <= USHRT_MAX)
19823 {
19824 unsigned char chSize = 253;
19825 unsigned short xSize = nSize;
19826 WRITEDATA(os, chSize);
19827 WRITEDATA(os, xSize);
19828 }
19829 else if (nSize <= UINT_MAX)
19830 {
19831 unsigned char chSize = 254;
19832 unsigned int xSize = nSize;
19833 WRITEDATA(os, chSize);
19834 WRITEDATA(os, xSize);
19835 }
19836 else
19837 {
19838 unsigned char chSize = 255;
19839 uint64 xSize = nSize;
19840 WRITEDATA(os, chSize);
19841 WRITEDATA(os, xSize);
19842 }
19843 return;
19844 }
19845
19846 template<typename Stream>
19847 uint64 ReadCompactSize(Stream& is)
19848 {
19849 unsigned char chSize;
19850 READDATA(is, chSize);
19851 uint64 nSizeRet = 0;
19852 if (chSize < 253)
19853 {
19854 nSizeRet = chSize;
19855 }
19856 else if (chSize == 253)
19857 {
19858 unsigned short xSize;
19859 READDATA(is, xSize);
19860 nSizeRet = xSize;
19861 }
19862 else if (chSize == 254)
19863 {
19864 unsigned int xSize;
19865 READDATA(is, xSize);
19866 nSizeRet = xSize;
19867 }
19868 else
19869 {
19870 uint64 xSize;
19871 READDATA(is, xSize);
19872 nSizeRet = xSize;
19873 }
19874 if (nSizeRet > (uint64)MAX_SIZE)
19875 throw std::ios_base::failure("ReadCompactSize() : size too large");
19876 return nSizeRet;
19877 }
19878
19879
19880
19881 //
19882 // Wrapper for serializing arrays and POD
19883 // There's a clever template way to make arrays serialize normally, but MSVC6 doesn't support it
19884 //
19885 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
19886 class CFlatData
19887 {
19888 protected:
19889 char* pbegin;
19890 char* pend;
19891 public:
19892 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
19893 char* begin() { return pbegin; }
19894 const char* begin() const { return pbegin; }
19895 char* end() { return pend; }
19896 const char* end() const { return pend; }
19897
19898 unsigned int GetSerializeSize(int, int=0) const
19899 {
19900 return pend - pbegin;
19901 }
19902
19903 template<typename Stream>
19904 void Serialize(Stream& s, int, int=0) const
19905 {
19906 s.write(pbegin, pend - pbegin);
19907 }
19908
19909 template<typename Stream>
19910 void Unserialize(Stream& s, int, int=0)
19911 {
19912 s.read(pbegin, pend - pbegin);
19913 }
19914 };
19915
19916
19917
19918 //
19919 // string stored as a fixed length field
19920 //
19921 template<std::size_t LEN>
19922 class CFixedFieldString
19923 {
19924 protected:
19925 const std::string* pcstr;
19926 std::string* pstr;
19927 public:
19928 explicit CFixedFieldString(const std::string& str) : pcstr(&str), pstr(NULL) { }
19929 explicit CFixedFieldString(std::string& str) : pcstr(&str), pstr(&str) { }
19930
19931 unsigned int GetSerializeSize(int, int=0) const
19932 {
19933 return LEN;
19934 }
19935
19936 template<typename Stream>
19937 void Serialize(Stream& s, int, int=0) const
19938 {
19939 char pszBuf[LEN];
19940 strncpy(pszBuf, pcstr->c_str(), LEN);
19941 s.write(pszBuf, LEN);
19942 }
19943
19944 template<typename Stream>
19945 void Unserialize(Stream& s, int, int=0)
19946 {
19947 if (pstr == NULL)
19948 throw std::ios_base::failure("CFixedFieldString::Unserialize : trying to unserialize to const string");
19949 char pszBuf[LEN+1];
19950 s.read(pszBuf, LEN);
19951 pszBuf[LEN] = '\0';
19952 *pstr = pszBuf;
19953 }
19954 };
19955
19956
19957
19958
19959
19960 //
19961 // Forward declarations
19962 //
19963
19964 // string
19965 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
19966 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
19967 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
19968
19969 // vector
19970 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
19971 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
19972 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion=VERSION);
19973 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&);
19974 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&);
19975 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion=VERSION);
19976 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&);
19977 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&);
19978 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion=VERSION);
19979
19980 // others derived from vector
19981 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion=VERSION);
19982 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion=VERSION);
19983 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion=VERSION);
19984
19985 // pair
19986 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion=VERSION);
19987 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion=VERSION);
19988 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion=VERSION);
19989
19990 // 3 tuple
19991 template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
19992 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);
19993 template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
19994
19995 // 4 tuple
19996 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);
19997 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);
19998 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);
19999
20000 // map
20001 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);
20002 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);
20003 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);
20004
20005 // set
20006 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
20007 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);
20008 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
20009
20010
20011
20012
20013
20014 //
20015 // If none of the specialized versions above matched, default to calling member function.
20016 // "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
20017 // The compiler will only cast int to long if none of the other templates matched.
20018 // Thanks to Boost serialization for this idea.
20019 //
20020 template<typename T>
20021 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion=VERSION)
20022 {
20023 return a.GetSerializeSize((int)nType, nVersion);
20024 }
20025
20026 template<typename Stream, typename T>
20027 inline void Serialize(Stream& os, const T& a, long nType, int nVersion=VERSION)
20028 {
20029 a.Serialize(os, (int)nType, nVersion);
20030 }
20031
20032 template<typename Stream, typename T>
20033 inline void Unserialize(Stream& is, T& a, long nType, int nVersion=VERSION)
20034 {
20035 a.Unserialize(is, (int)nType, nVersion);
20036 }
20037
20038
20039
20040
20041
20042 //
20043 // string
20044 //
20045 template<typename C>
20046 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
20047 {
20048 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
20049 }
20050
20051 template<typename Stream, typename C>
20052 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
20053 {
20054 WriteCompactSize(os, str.size());
20055 if (!str.empty())
20056 os.write((char*)&str[0], str.size() * sizeof(str[0]));
20057 }
20058
20059 template<typename Stream, typename C>
20060 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
20061 {
20062 unsigned int nSize = ReadCompactSize(is);
20063 str.resize(nSize);
20064 if (nSize != 0)
20065 is.read((char*)&str[0], nSize * sizeof(str[0]));
20066 }
20067
20068
20069
20070 //
20071 // vector
20072 //
20073 template<typename T, typename A>
20074 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
20075 {
20076 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
20077 }
20078
20079 template<typename T, typename A>
20080 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
20081 {
20082 unsigned int nSize = GetSizeOfCompactSize(v.size());
20083 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
20084 nSize += GetSerializeSize((*vi), nType, nVersion);
20085 return nSize;
20086 }
20087
20088 template<typename T, typename A>
20089 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
20090 {
20091 return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental<T>());
20092 }
20093
20094
20095 template<typename Stream, typename T, typename A>
20096 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
20097 {
20098 WriteCompactSize(os, v.size());
20099 if (!v.empty())
20100 os.write((char*)&v[0], v.size() * sizeof(T));
20101 }
20102
20103 template<typename Stream, typename T, typename A>
20104 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
20105 {
20106 WriteCompactSize(os, v.size());
20107 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
20108 ::Serialize(os, (*vi), nType, nVersion);
20109 }
20110
20111 template<typename Stream, typename T, typename A>
20112 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
20113 {
20114 Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
20115 }
20116
20117
20118 template<typename Stream, typename T, typename A>
20119 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
20120 {
20121 //unsigned int nSize = ReadCompactSize(is);
20122 //v.resize(nSize);
20123 //is.read((char*)&v[0], nSize * sizeof(T));
20124
20125 // Limit size per read so bogus size value won't cause out of memory
20126 v.clear();
20127 unsigned int nSize = ReadCompactSize(is);
20128 unsigned int i = 0;
20129 while (i < nSize)
20130 {
20131 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
20132 v.resize(i + blk);
20133 is.read((char*)&v[i], blk * sizeof(T));
20134 i += blk;
20135 }
20136 }
20137
20138 template<typename Stream, typename T, typename A>
20139 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
20140 {
20141 //unsigned int nSize = ReadCompactSize(is);
20142 //v.resize(nSize);
20143 //for (std::vector<T, A>::iterator vi = v.begin(); vi != v.end(); ++vi)
20144 // Unserialize(is, (*vi), nType, nVersion);
20145
20146 v.clear();
20147 unsigned int nSize = ReadCompactSize(is);
20148 unsigned int i = 0;
20149 unsigned int nMid = 0;
20150 while (nMid < nSize)
20151 {
20152 nMid += 5000000 / sizeof(T);
20153 if (nMid > nSize)
20154 nMid = nSize;
20155 v.resize(nMid);
20156 for (; i < nMid; i++)
20157 Unserialize(is, v[i], nType, nVersion);
20158 }
20159 }
20160
20161 template<typename Stream, typename T, typename A>
20162 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
20163 {
20164 Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental<T>());
20165 }
20166
20167
20168
20169 //
20170 // others derived from vector
20171 //
20172 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
20173 {
20174 return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
20175 }
20176
20177 template<typename Stream>
20178 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
20179 {
20180 Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
20181 }
20182
20183 template<typename Stream>
20184 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
20185 {
20186 Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
20187 }
20188
20189
20190
20191 //
20192 // pair
20193 //
20194 template<typename K, typename T>
20195 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
20196 {
20197 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
20198 }
20199
20200 template<typename Stream, typename K, typename T>
20201 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
20202 {
20203 Serialize(os, item.first, nType, nVersion);
20204 Serialize(os, item.second, nType, nVersion);
20205 }
20206
20207 template<typename Stream, typename K, typename T>
20208 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
20209 {
20210 Unserialize(is, item.first, nType, nVersion);
20211 Unserialize(is, item.second, nType, nVersion);
20212 }
20213
20214
20215
20216 //
20217 // 3 tuple
20218 //
20219 template<typename T0, typename T1, typename T2>
20220 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
20221 {
20222 unsigned int nSize = 0;
20223 nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
20224 nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
20225 nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
20226 return nSize;
20227 }
20228
20229 template<typename Stream, typename T0, typename T1, typename T2>
20230 void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
20231 {
20232 Serialize(os, boost::get<0>(item), nType, nVersion);
20233 Serialize(os, boost::get<1>(item), nType, nVersion);
20234 Serialize(os, boost::get<2>(item), nType, nVersion);
20235 }
20236
20237 template<typename Stream, typename T0, typename T1, typename T2>
20238 void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
20239 {
20240 Unserialize(is, boost::get<0>(item), nType, nVersion);
20241 Unserialize(is, boost::get<1>(item), nType, nVersion);
20242 Unserialize(is, boost::get<2>(item), nType, nVersion);
20243 }
20244
20245
20246
20247 //
20248 // 4 tuple
20249 //
20250 template<typename T0, typename T1, typename T2, typename T3>
20251 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
20252 {
20253 unsigned int nSize = 0;
20254 nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
20255 nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
20256 nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
20257 nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion);
20258 return nSize;
20259 }
20260
20261 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
20262 void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
20263 {
20264 Serialize(os, boost::get<0>(item), nType, nVersion);
20265 Serialize(os, boost::get<1>(item), nType, nVersion);
20266 Serialize(os, boost::get<2>(item), nType, nVersion);
20267 Serialize(os, boost::get<3>(item), nType, nVersion);
20268 }
20269
20270 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
20271 void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
20272 {
20273 Unserialize(is, boost::get<0>(item), nType, nVersion);
20274 Unserialize(is, boost::get<1>(item), nType, nVersion);
20275 Unserialize(is, boost::get<2>(item), nType, nVersion);
20276 Unserialize(is, boost::get<3>(item), nType, nVersion);
20277 }
20278
20279
20280
20281 //
20282 // map
20283 //
20284 template<typename K, typename T, typename Pred, typename A>
20285 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
20286 {
20287 unsigned int nSize = GetSizeOfCompactSize(m.size());
20288 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
20289 nSize += GetSerializeSize((*mi), nType, nVersion);
20290 return nSize;
20291 }
20292
20293 template<typename Stream, typename K, typename T, typename Pred, typename A>
20294 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
20295 {
20296 WriteCompactSize(os, m.size());
20297 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
20298 Serialize(os, (*mi), nType, nVersion);
20299 }
20300
20301 template<typename Stream, typename K, typename T, typename Pred, typename A>
20302 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
20303 {
20304 m.clear();
20305 unsigned int nSize = ReadCompactSize(is);
20306 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
20307 for (unsigned int i = 0; i < nSize; i++)
20308 {
20309 std::pair<K, T> item;
20310 Unserialize(is, item, nType, nVersion);
20311 mi = m.insert(mi, item);
20312 }
20313 }
20314
20315
20316
20317 //
20318 // set
20319 //
20320 template<typename K, typename Pred, typename A>
20321 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
20322 {
20323 unsigned int nSize = GetSizeOfCompactSize(m.size());
20324 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
20325 nSize += GetSerializeSize((*it), nType, nVersion);
20326 return nSize;
20327 }
20328
20329 template<typename Stream, typename K, typename Pred, typename A>
20330 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
20331 {
20332 WriteCompactSize(os, m.size());
20333 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
20334 Serialize(os, (*it), nType, nVersion);
20335 }
20336
20337 template<typename Stream, typename K, typename Pred, typename A>
20338 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
20339 {
20340 m.clear();
20341 unsigned int nSize = ReadCompactSize(is);
20342 typename std::set<K, Pred, A>::iterator it = m.begin();
20343 for (unsigned int i = 0; i < nSize; i++)
20344 {
20345 K key;
20346 Unserialize(is, key, nType, nVersion);
20347 it = m.insert(it, key);
20348 }
20349 }
20350
20351
20352
20353 //
20354 // Support for IMPLEMENT_SERIALIZE and READWRITE macro
20355 //
20356 class CSerActionGetSerializeSize { };
20357 class CSerActionSerialize { };
20358 class CSerActionUnserialize { };
20359
20360 template<typename Stream, typename T>
20361 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
20362 {
20363 return ::GetSerializeSize(obj, nType, nVersion);
20364 }
20365
20366 template<typename Stream, typename T>
20367 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
20368 {
20369 ::Serialize(s, obj, nType, nVersion);
20370 return 0;
20371 }
20372
20373 template<typename Stream, typename T>
20374 inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
20375 {
20376 ::Unserialize(s, obj, nType, nVersion);
20377 return 0;
20378 }
20379
20380 struct ser_streamplaceholder
20381 {
20382 int nType;
20383 int nVersion;
20384 };
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394 //
20395 // Allocator that locks its contents from being paged
20396 // out of memory and clears its contents before deletion.
20397 //
20398 template<typename T>
20399 struct secure_allocator : public std::allocator<T>
20400 {
20401 // MSVC8 default copy constructor is broken
20402 typedef std::allocator<T> base;
20403 typedef typename base::size_type size_type;
20404 typedef typename base::difference_type difference_type;
20405 typedef typename base::pointer pointer;
20406 typedef typename base::const_pointer const_pointer;
20407 typedef typename base::reference reference;
20408 typedef typename base::const_reference const_reference;
20409 typedef typename base::value_type value_type;
20410 secure_allocator() throw() {}
20411 secure_allocator(const secure_allocator& a) throw() : base(a) {}
20412 template <typename U>
20413 secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
20414 ~secure_allocator() throw() {}
20415 template<typename _Other> struct rebind
20416 { typedef secure_allocator<_Other> other; };
20417
20418 T* allocate(std::size_t n, const void *hint = 0)
20419 {
20420 T *p;
20421 p = std::allocator<T>::allocate(n, hint);
20422 if (p != NULL)
20423 mlock(p, sizeof(T) * n);
20424 return p;
20425 }
20426
20427 void deallocate(T* p, std::size_t n)
20428 {
20429 if (p != NULL)
20430 {
20431 memset(p, 0, sizeof(T) * n);
20432 munlock(p, sizeof(T) * n);
20433 }
20434 std::allocator<T>::deallocate(p, n);
20435 }
20436 };
20437
20438
20439 //
20440 // Allocator that clears its contents before deletion.
20441 //
20442 template<typename T>
20443 struct zero_after_free_allocator : public std::allocator<T>
20444 {
20445 // MSVC8 default copy constructor is broken
20446 typedef std::allocator<T> base;
20447 typedef typename base::size_type size_type;
20448 typedef typename base::difference_type difference_type;
20449 typedef typename base::pointer pointer;
20450 typedef typename base::const_pointer const_pointer;
20451 typedef typename base::reference reference;
20452 typedef typename base::const_reference const_reference;
20453 typedef typename base::value_type value_type;
20454 zero_after_free_allocator() throw() {}
20455 zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
20456 template <typename U>
20457 zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a) {}
20458 ~zero_after_free_allocator() throw() {}
20459 template<typename _Other> struct rebind
20460 { typedef zero_after_free_allocator<_Other> other; };
20461
20462 void deallocate(T* p, std::size_t n)
20463 {
20464 if (p != NULL)
20465 memset(p, 0, sizeof(T) * n);
20466 std::allocator<T>::deallocate(p, n);
20467 }
20468 };
20469
20470
20471
20472 //
20473 // Double ended buffer combining vector and stream-like interfaces.
20474 // >> and << read and write unformatted data using the above serialization templates.
20475 // Fills with data in linear time; some stringstream implementations take N^2 time.
20476 //
20477 class CDataStream
20478 {
20479 protected:
20480 typedef std::vector<char, zero_after_free_allocator<char> > vector_type;
20481 vector_type vch;
20482 unsigned int nReadPos;
20483 short state;
20484 short exceptmask;
20485 public:
20486 int nType;
20487 int nVersion;
20488
20489 typedef vector_type::allocator_type allocator_type;
20490 typedef vector_type::size_type size_type;
20491 typedef vector_type::difference_type difference_type;
20492 typedef vector_type::reference reference;
20493 typedef vector_type::const_reference const_reference;
20494 typedef vector_type::value_type value_type;
20495 typedef vector_type::iterator iterator;
20496 typedef vector_type::const_iterator const_iterator;
20497 typedef vector_type::reverse_iterator reverse_iterator;
20498
20499 explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
20500 {
20501 Init(nTypeIn, nVersionIn);
20502 }
20503
20504 CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
20505 {
20506 Init(nTypeIn, nVersionIn);
20507 }
20508
20509 #if !defined(_MSC_VER) || _MSC_VER >= 1300
20510 CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
20511 {
20512 Init(nTypeIn, nVersionIn);
20513 }
20514 #endif
20515
20516 CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
20517 {
20518 Init(nTypeIn, nVersionIn);
20519 }
20520
20521 CDataStream(const std::vector<char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
20522 {
20523 Init(nTypeIn, nVersionIn);
20524 }
20525
20526 CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
20527 {
20528 Init(nTypeIn, nVersionIn);
20529 }
20530
20531 void Init(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
20532 {
20533 nReadPos = 0;
20534 nType = nTypeIn;
20535 nVersion = nVersionIn;
20536 state = 0;
20537 exceptmask = std::ios::badbit | std::ios::failbit;
20538 }
20539
20540 CDataStream& operator+=(const CDataStream& b)
20541 {
20542 vch.insert(vch.end(), b.begin(), b.end());
20543 return *this;
20544 }
20545
20546 friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
20547 {
20548 CDataStream ret = a;
20549 ret += b;
20550 return (ret);
20551 }
20552
20553 std::string str() const
20554 {
20555 return (std::string(begin(), end()));
20556 }
20557
20558
20559 //
20560 // Vector subset
20561 //
20562 const_iterator begin() const { return vch.begin() + nReadPos; }
20563 iterator begin() { return vch.begin() + nReadPos; }
20564 const_iterator end() const { return vch.end(); }
20565 iterator end() { return vch.end(); }
20566 size_type size() const { return vch.size() - nReadPos; }
20567 bool empty() const { return vch.size() == nReadPos; }
20568 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
20569 void reserve(size_type n) { vch.reserve(n + nReadPos); }
20570 const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
20571 reference operator[](size_type pos) { return vch[pos + nReadPos]; }
20572 void clear() { vch.clear(); nReadPos = 0; }
20573 iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
20574 void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
20575
20576 void insert(iterator it, const_iterator first, const_iterator last)
20577 {
20578 if (it == vch.begin() + nReadPos && last - first <= nReadPos)
20579 {
20580 // special case for inserting at the front when there's room
20581 nReadPos -= (last - first);
20582 memcpy(&vch[nReadPos], &first[0], last - first);
20583 }
20584 else
20585 vch.insert(it, first, last);
20586 }
20587
20588 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
20589 {
20590 if (it == vch.begin() + nReadPos && last - first <= nReadPos)
20591 {
20592 // special case for inserting at the front when there's room
20593 nReadPos -= (last - first);
20594 memcpy(&vch[nReadPos], &first[0], last - first);
20595 }
20596 else
20597 vch.insert(it, first, last);
20598 }
20599
20600 #if !defined(_MSC_VER) || _MSC_VER >= 1300
20601 void insert(iterator it, const char* first, const char* last)
20602 {
20603 if (it == vch.begin() + nReadPos && last - first <= nReadPos)
20604 {
20605 // special case for inserting at the front when there's room
20606 nReadPos -= (last - first);
20607 memcpy(&vch[nReadPos], &first[0], last - first);
20608 }
20609 else
20610 vch.insert(it, first, last);
20611 }
20612 #endif
20613
20614 iterator erase(iterator it)
20615 {
20616 if (it == vch.begin() + nReadPos)
20617 {
20618 // special case for erasing from the front
20619 if (++nReadPos >= vch.size())
20620 {
20621 // whenever we reach the end, we take the opportunity to clear the buffer
20622 nReadPos = 0;
20623 return vch.erase(vch.begin(), vch.end());
20624 }
20625 return vch.begin() + nReadPos;
20626 }
20627 else
20628 return vch.erase(it);
20629 }
20630
20631 iterator erase(iterator first, iterator last)
20632 {
20633 if (first == vch.begin() + nReadPos)
20634 {
20635 // special case for erasing from the front
20636 if (last == vch.end())
20637 {
20638 nReadPos = 0;
20639 return vch.erase(vch.begin(), vch.end());
20640 }
20641 else
20642 {
20643 nReadPos = (last - vch.begin());
20644 return last;
20645 }
20646 }
20647 else
20648 return vch.erase(first, last);
20649 }
20650
20651 inline void Compact()
20652 {
20653 vch.erase(vch.begin(), vch.begin() + nReadPos);
20654 nReadPos = 0;
20655 }
20656
20657 bool Rewind(size_type n)
20658 {
20659 // Rewind by n characters if the buffer hasn't been compacted yet
20660 if (n > nReadPos)
20661 return false;
20662 nReadPos -= n;
20663 return true;
20664 }
20665
20666
20667 //
20668 // Stream subset
20669 //
20670 void setstate(short bits, const char* psz)
20671 {
20672 state |= bits;
20673 if (state & exceptmask)
20674 throw std::ios_base::failure(psz);
20675 }
20676
20677 bool eof() const { return size() == 0; }
20678 bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
20679 bool good() const { return !eof() && (state == 0); }
20680 void clear(short n) { state = n; } // name conflict with vector clear()
20681 short exceptions() { return exceptmask; }
20682 short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
20683 CDataStream* rdbuf() { return this; }
20684 int in_avail() { return size(); }
20685
20686 void SetType(int n) { nType = n; }
20687 int GetType() { return nType; }
20688 void SetVersion(int n) { nVersion = n; }
20689 int GetVersion() { return nVersion; }
20690 void ReadVersion() { *this >> nVersion; }
20691 void WriteVersion() { *this << nVersion; }
20692
20693 CDataStream& read(char* pch, int nSize)
20694 {
20695 // Read from the beginning of the buffer
20696 assert(nSize >= 0);
20697 unsigned int nReadPosNext = nReadPos + nSize;
20698 if (nReadPosNext >= vch.size())
20699 {
20700 if (nReadPosNext > vch.size())
20701 {
20702 setstate(std::ios::failbit, "CDataStream::read() : end of data");
20703 memset(pch, 0, nSize);
20704 nSize = vch.size() - nReadPos;
20705 }
20706 memcpy(pch, &vch[nReadPos], nSize);
20707 nReadPos = 0;
20708 vch.clear();
20709 return (*this);
20710 }
20711 memcpy(pch, &vch[nReadPos], nSize);
20712 nReadPos = nReadPosNext;
20713 return (*this);
20714 }
20715
20716 CDataStream& ignore(int nSize)
20717 {
20718 // Ignore from the beginning of the buffer
20719 assert(nSize >= 0);
20720 unsigned int nReadPosNext = nReadPos + nSize;
20721 if (nReadPosNext >= vch.size())
20722 {
20723 if (nReadPosNext > vch.size())
20724 {
20725 setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
20726 nSize = vch.size() - nReadPos;
20727 }
20728 nReadPos = 0;
20729 vch.clear();
20730 return (*this);
20731 }
20732 nReadPos = nReadPosNext;
20733 return (*this);
20734 }
20735
20736 CDataStream& write(const char* pch, int nSize)
20737 {
20738 // Write to the end of the buffer
20739 assert(nSize >= 0);
20740 vch.insert(vch.end(), pch, pch + nSize);
20741 return (*this);
20742 }
20743
20744 template<typename Stream>
20745 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
20746 {
20747 // Special case: stream << stream concatenates like stream += stream
20748 if (!vch.empty())
20749 s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
20750 }
20751
20752 template<typename T>
20753 unsigned int GetSerializeSize(const T& obj)
20754 {
20755 // Tells the size of the object if serialized to this stream
20756 return ::GetSerializeSize(obj, nType, nVersion);
20757 }
20758
20759 template<typename T>
20760 CDataStream& operator<<(const T& obj)
20761 {
20762 // Serialize to this stream
20763 ::Serialize(*this, obj, nType, nVersion);
20764 return (*this);
20765 }
20766
20767 template<typename T>
20768 CDataStream& operator>>(T& obj)
20769 {
20770 // Unserialize from this stream
20771 ::Unserialize(*this, obj, nType, nVersion);
20772 return (*this);
20773 }
20774 };
20775
20776 #ifdef TESTCDATASTREAM
20777 // VC6sp6
20778 // CDataStream:
20779 // n=1000 0 seconds
20780 // n=2000 0 seconds
20781 // n=4000 0 seconds
20782 // n=8000 0 seconds
20783 // n=16000 0 seconds
20784 // n=32000 0 seconds
20785 // n=64000 1 seconds
20786 // n=128000 1 seconds
20787 // n=256000 2 seconds
20788 // n=512000 4 seconds
20789 // n=1024000 8 seconds
20790 // n=2048000 16 seconds
20791 // n=4096000 32 seconds
20792 // stringstream:
20793 // n=1000 1 seconds
20794 // n=2000 1 seconds
20795 // n=4000 13 seconds
20796 // n=8000 87 seconds
20797 // n=16000 400 seconds
20798 // n=32000 1660 seconds
20799 // n=64000 6749 seconds
20800 // n=128000 27241 seconds
20801 // n=256000 109804 seconds
20802 #include <iostream>
20803 int main(int argc, char *argv[])
20804 {
20805 vector<unsigned char> vch(0xcc, 250);
20806 printf("CDataStream:\n");
20807 for (int n = 1000; n <= 4500000; n *= 2)
20808 {
20809 CDataStream ss;
20810 time_t nStart = time(NULL);
20811 for (int i = 0; i < n; i++)
20812 ss.write((char*)&vch[0], vch.size());
20813 printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
20814 }
20815 printf("stringstream:\n");
20816 for (int n = 1000; n <= 4500000; n *= 2)
20817 {
20818 stringstream ss;
20819 time_t nStart = time(NULL);
20820 for (int i = 0; i < n; i++)
20821 ss.write((char*)&vch[0], vch.size());
20822 printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
20823 }
20824 }
20825 #endif
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836 //
20837 // Automatic closing wrapper for FILE*
20838 // - Will automatically close the file when it goes out of scope if not null.
20839 // - If you're returning the file pointer, return file.release().
20840 // - If you need to close the file early, use file.fclose() instead of fclose(file).
20841 //
20842 class CAutoFile
20843 {
20844 protected:
20845 FILE* file;
20846 short state;
20847 short exceptmask;
20848 public:
20849 int nType;
20850 int nVersion;
20851
20852 typedef FILE element_type;
20853
20854 CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=VERSION)
20855 {
20856 file = filenew;
20857 nType = nTypeIn;
20858 nVersion = nVersionIn;
20859 state = 0;
20860 exceptmask = std::ios::badbit | std::ios::failbit;
20861 }
20862
20863 ~CAutoFile()
20864 {
20865 fclose();
20866 }
20867
20868 void fclose()
20869 {
20870 if (file != NULL && file != stdin && file != stdout && file != stderr)
20871 ::fclose(file);
20872 file = NULL;
20873 }
20874
20875 FILE* release() { FILE* ret = file; file = NULL; return ret; }
20876 operator FILE*() { return file; }
20877 FILE* operator->() { return file; }
20878 FILE& operator*() { return *file; }
20879 FILE** operator&() { return &file; }
20880 FILE* operator=(FILE* pnew) { return file = pnew; }
20881 bool operator!() { return (file == NULL); }
20882
20883
20884 //
20885 // Stream subset
20886 //
20887 void setstate(short bits, const char* psz)
20888 {
20889 state |= bits;
20890 if (state & exceptmask)
20891 throw std::ios_base::failure(psz);
20892 }
20893
20894 bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
20895 bool good() const { return state == 0; }
20896 void clear(short n = 0) { state = n; }
20897 short exceptions() { return exceptmask; }
20898 short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
20899
20900 void SetType(int n) { nType = n; }
20901 int GetType() { return nType; }
20902 void SetVersion(int n) { nVersion = n; }
20903 int GetVersion() { return nVersion; }
20904 void ReadVersion() { *this >> nVersion; }
20905 void WriteVersion() { *this << nVersion; }
20906
20907 CAutoFile& read(char* pch, int nSize)
20908 {
20909 if (!file)
20910 throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
20911 if (fread(pch, 1, nSize, file) != nSize)
20912 setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
20913 return (*this);
20914 }
20915
20916 CAutoFile& write(const char* pch, int nSize)
20917 {
20918 if (!file)
20919 throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
20920 if (fwrite(pch, 1, nSize, file) != nSize)
20921 setstate(std::ios::failbit, "CAutoFile::write : write failed");
20922 return (*this);
20923 }
20924
20925 template<typename T>
20926 unsigned int GetSerializeSize(const T& obj)
20927 {
20928 // Tells the size of the object if serialized to this stream
20929 return ::GetSerializeSize(obj, nType, nVersion);
20930 }
20931
20932 template<typename T>
20933 CAutoFile& operator<<(const T& obj)
20934 {
20935 // Serialize to this stream
20936 if (!file)
20937 throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
20938 ::Serialize(*this, obj, nType, nVersion);
20939 return (*this);
20940 }
20941
20942 template<typename T>
20943 CAutoFile& operator>>(T& obj)
20944 {
20945 // Unserialize from this stream
20946 if (!file)
20947 throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
20948 ::Unserialize(*this, obj, nType, nVersion);
20949 return (*this);
20950 }
20951 };
20952
20953 #endif