X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fserialize.h;h=bc8ce0126e315ae2b9b26146c7edc9701e2af3b8;hb=9d314f181af7c43ad8cd7223c08486e316bef5e2;hp=a1f8a0ee67d0d4e522dc1f24f42516ebf768b424;hpb=5b1a3f0a5efd2d87368de586ccf685449b2af847;p=novacoin.git diff --git a/src/serialize.h b/src/serialize.h index a1f8a0e..bc8ce01 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -9,17 +9,16 @@ #include #include #include +#include +#include #include #include #include #include -#ifndef Q_MOC_RUN -#include -#include -#include -#include -#endif +//#ifndef Q_MOC_RUN +//#include +//#endif #if defined __USE_MINGW_ANSI_STDIO #undef __USE_MINGW_ANSI_STDIO // This constant forces MinGW to conduct stupid behavior @@ -56,56 +55,12 @@ enum SER_DISK = (1 << 1), SER_GETHASH = (1 << 2), - // modifiers - SER_SKIPSIG = (1 << 16), - SER_BLOCKHEADERONLY = (1 << 17), + // modifiers + SER_SKIPSIG = (1 << 16), + SER_BLOCKHEADERONLY = (1 << 17) }; -#ifdef _MSC_VER -#define IMPLEMENT_SERIALIZE(statements) \ - unsigned int GetSerializeSize(int nType, int nVersion) const \ - { \ - CSerActionGetSerializeSize ser_action; \ - const bool fGetSize = true; \ - const bool fWrite = false; \ - const bool fRead = false; \ - unsigned int nSerSize = 0; \ - ser_streamplaceholder s; \ - assert(fGetSize||fWrite||fRead); /* suppress warning */ \ - s.nType = nType; \ - s.nVersion = nVersion; \ - std::map mapUnkIds; \ - {statements} \ - return nSerSize; \ - } \ - template \ - void Serialize(Stream& s, int nType, int nVersion) const \ - { \ - CSerActionSerialize ser_action; \ - const bool fGetSize = false; \ - const bool fWrite = true; \ - const bool fRead = false; \ - unsigned int nSerSize = 0; \ - assert(fGetSize||fWrite||fRead); /* suppress warning */ \ - std::map mapUnkIds; \ - {statements} \ - } \ - template \ - void Unserialize(Stream& s, int nType, int nVersion) \ - { \ - CSerActionUnserialize ser_action; \ - const bool fGetSize = false; \ - const bool fWrite = false; \ - const bool fRead = true; \ - unsigned int nSerSize = 0; \ - std::map mapUnkIds; \ - assert(fGetSize||fWrite||fRead); /* suppress warning */ \ - {statements} \ - } - -#else - #define IMPLEMENT_SERIALIZE(statements) \ unsigned int GetSerializeSize(int nType, int nVersion) const \ { \ @@ -144,8 +99,6 @@ enum {statements} \ } -#endif - #define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action)) @@ -316,7 +269,7 @@ template inline unsigned int GetSizeOfVarInt(I n) { int nRet = 0; - while(true) { + for ( ; ; ) { nRet++; if (n <= 0x7F) break; @@ -330,7 +283,7 @@ void WriteVarInt(Stream& os, I n) { unsigned char tmp[(sizeof(n)*8+6)/7]; int len=0; - while(true) { + for ( ; ; ) { tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00); if (n <= 0x7F) break; @@ -346,7 +299,7 @@ template I ReadVarInt(Stream& is) { I n = 0; - while(true) { + for ( ; ; ) { unsigned char chData; READDATA(is, chData); n = (n << 7) | (chData & 0x7F); @@ -428,14 +381,14 @@ template void Serialize(Stream& os, const std::basi template void Unserialize(Stream& is, std::basic_string& str, int, int=0); // vector -template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::true_type&); -template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::false_type&); +template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const std::true_type&); +template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const std::false_type&); template inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion); -template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const boost::true_type&); -template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const boost::false_type&); +template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const std::true_type&); +template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const std::false_type&); template inline void Serialize(Stream& os, const std::vector& v, int nType, int nVersion); -template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const boost::true_type&); -template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const boost::false_type&); +template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const std::true_type&); +template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const std::false_type&); template inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion); // others derived from vector @@ -449,14 +402,14 @@ template void Serialize(Stream& os, con template void Unserialize(Stream& is, std::pair& item, int nType, int nVersion); // 3 tuple -template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion); -template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion); -template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion); +template unsigned int GetSerializeSize(const std::tuple& item, int nType, int nVersion); +template void Serialize(Stream& os, const std::tuple& item, int nType, int nVersion); +template void Unserialize(Stream& is, std::tuple& item, int nType, int nVersion); // 4 tuple -template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion); -template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion); -template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion); +template unsigned int GetSerializeSize(const std::tuple& item, int nType, int nVersion); +template void Serialize(Stream& os, const std::tuple& item, int nType, int nVersion); +template void Unserialize(Stream& is, std::tuple& item, int nType, int nVersion); // map template unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion); @@ -532,13 +485,13 @@ void Unserialize(Stream& is, std::basic_string& str, int, int) // vector // template -unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::true_type&) +unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const std::true_type&) { return (unsigned int)(GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T)); } template -unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::false_type&) +unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const std::false_type&) { unsigned int nSize = GetSizeOfCompactSize(v.size()); for (typename std::vector::const_iterator vi = v.begin(); vi != v.end(); ++vi) @@ -549,12 +502,12 @@ unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nV template inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion) { - return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental()); + return GetSerializeSize_impl(v, nType, nVersion, std::is_fundamental()); } template -void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const boost::true_type&) +void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const std::true_type&) { WriteCompactSize(os, v.size()); if (!v.empty()) @@ -562,7 +515,7 @@ void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVers } template -void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const boost::false_type&) +void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const std::false_type&) { WriteCompactSize(os, v.size()); for (typename std::vector::const_iterator vi = v.begin(); vi != v.end(); ++vi) @@ -572,12 +525,12 @@ void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVers template inline void Serialize(Stream& os, const std::vector& v, int nType, int nVersion) { - Serialize_impl(os, v, nType, nVersion, boost::is_fundamental()); + Serialize_impl(os, v, nType, nVersion, std::is_fundamental()); } template -void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const boost::true_type&) +void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const std::true_type&) { // Limit size per read so bogus size value won't cause out of memory v.clear(); @@ -593,7 +546,7 @@ void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, } template -void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const boost::false_type&) +void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const std::false_type&) { v.clear(); unsigned int nSize = (unsigned int)(ReadCompactSize(is)); @@ -613,7 +566,7 @@ void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, template inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion) { - Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental()); + Unserialize_impl(is, v, nType, nVersion, std::is_fundamental()); } @@ -669,29 +622,29 @@ void Unserialize(Stream& is, std::pair& item, int nType, int nVersion) // 3 tuple // template -unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion) +unsigned int GetSerializeSize(const std::tuple& item, int nType, int nVersion) { unsigned int nSize = 0; - nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion); - nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion); - nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<0>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<1>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<2>(item), nType, nVersion); return nSize; } template -void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion) +void Serialize(Stream& os, const std::tuple& item, int nType, int nVersion) { - Serialize(os, boost::get<0>(item), nType, nVersion); - Serialize(os, boost::get<1>(item), nType, nVersion); - Serialize(os, boost::get<2>(item), nType, nVersion); + Serialize(os, std::get<0>(item), nType, nVersion); + Serialize(os, std::get<1>(item), nType, nVersion); + Serialize(os, std::get<2>(item), nType, nVersion); } template -void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion) +void Unserialize(Stream& is, std::tuple& item, int nType, int nVersion) { - Unserialize(is, boost::get<0>(item), nType, nVersion); - Unserialize(is, boost::get<1>(item), nType, nVersion); - Unserialize(is, boost::get<2>(item), nType, nVersion); + Unserialize(is, std::get<0>(item), nType, nVersion); + Unserialize(is, std::get<1>(item), nType, nVersion); + Unserialize(is, std::get<2>(item), nType, nVersion); } @@ -700,32 +653,32 @@ void Unserialize(Stream& is, boost::tuple& item, int nType, int nVer // 4 tuple // template -unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion) +unsigned int GetSerializeSize(const std::tuple& item, int nType, int nVersion) { unsigned int nSize = 0; - nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion); - nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion); - nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion); - nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<0>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<1>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<2>(item), nType, nVersion); + nSize += GetSerializeSize(std::get<3>(item), nType, nVersion); return nSize; } template -void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion) +void Serialize(Stream& os, const std::tuple& item, int nType, int nVersion) { - Serialize(os, boost::get<0>(item), nType, nVersion); - Serialize(os, boost::get<1>(item), nType, nVersion); - Serialize(os, boost::get<2>(item), nType, nVersion); - Serialize(os, boost::get<3>(item), nType, nVersion); + Serialize(os, std::get<0>(item), nType, nVersion); + Serialize(os, std::get<1>(item), nType, nVersion); + Serialize(os, std::get<2>(item), nType, nVersion); + Serialize(os, std::get<3>(item), nType, nVersion); } template -void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion) +void Unserialize(Stream& is, std::tuple& item, int nType, int nVersion) { - Unserialize(is, boost::get<0>(item), nType, nVersion); - Unserialize(is, boost::get<1>(item), nType, nVersion); - Unserialize(is, boost::get<2>(item), nType, nVersion); - Unserialize(is, boost::get<3>(item), nType, nVersion); + Unserialize(is, std::get<0>(item), nType, nVersion); + Unserialize(is, std::get<1>(item), nType, nVersion); + Unserialize(is, std::get<2>(item), nType, nVersion); + Unserialize(is, std::get<3>(item), nType, nVersion); } @@ -1106,10 +1059,7 @@ public: if (nReadPosNext >= vch.size()) { if (nReadPosNext > vch.size()) - { setstate(std::ios::failbit, "CDataStream::ignore() : end of data"); - nSize = (int)(vch.size() - nReadPos); - } nReadPos = 0; vch.clear(); return (*this); @@ -1334,7 +1284,7 @@ public: int nVersion; CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) : - src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0), + src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0), state(0), exceptmask(std::ios_base::badbit | std::ios_base::failbit), nType(nTypeIn), nVersion(nVersionIn) { } @@ -1360,7 +1310,7 @@ public: unsigned int pos = (unsigned int)(nReadPos % vchBuf.size()); size_t nNow = nSize; if (nNow + pos > vchBuf.size()) - nNow = (size_t)(vchBuf.size() - pos); + nNow = vchBuf.size() - pos; if (nNow + nReadPos > nSrcPos) nNow = (size_t)(nSrcPos - nReadPos); memcpy(pch, &vchBuf[pos], nNow); @@ -1405,7 +1355,7 @@ public: // prevent reading beyond a certain position // no argument removes the limit - bool SetLimit(uint64_t nPos = (uint64_t)(-1)) { + bool SetLimit(uint64_t nPos = std::numeric_limits::max()) { if (nPos < nReadPos) return false; nReadLimit = nPos; @@ -1421,7 +1371,7 @@ public: // search for a given byte in the stream, and remain positioned on it void FindByte(char ch) { - while (true) { + for ( ; ; ) { if (nReadPos == nSrcPos) Fill(); if (vchBuf[nReadPos % vchBuf.size()] == ch)