Use CSizeComputer to avoid counting sizes in SerializationOp

This commit is contained in:
Pieter Wuille 2014-08-21 00:49:32 +02:00 committed by Kamil Domanski
parent 84881f8c47
commit 31e9a8384a
13 changed files with 81 additions and 184 deletions

View file

@ -49,14 +49,11 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; READWRITE(*(CAddress*)this);
CAddress* pthis = (CAddress*)(this);
READWRITE(*pthis);
READWRITE(source); READWRITE(source);
READWRITE(nLastSuccess); READWRITE(nLastSuccess);
READWRITE(nAttempts); READWRITE(nAttempts);
return nSerSize;
} }
void Init() void Init()

View file

@ -49,8 +49,7 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(nRelayUntil); READWRITE(nRelayUntil);
@ -66,7 +65,6 @@ public:
READWRITE(LIMITED_STRING(strComment, 65536)); READWRITE(LIMITED_STRING(strComment, 65536));
READWRITE(LIMITED_STRING(strStatusBar, 256)); READWRITE(LIMITED_STRING(strStatusBar, 256));
READWRITE(LIMITED_STRING(strReserved, 256)); READWRITE(LIMITED_STRING(strReserved, 256));
return nSerSize;
} }
void SetNull(); void SetNull();
@ -89,11 +87,9 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(vchMsg); READWRITE(vchMsg);
READWRITE(vchSig); READWRITE(vchSig);
return nSerSize;
} }
void SetNull(); void SetNull();

View file

@ -65,13 +65,11 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(vData); READWRITE(vData);
READWRITE(nHashFuncs); READWRITE(nHashFuncs);
READWRITE(nTweak); READWRITE(nTweak);
READWRITE(nFlags); READWRITE(nFlags);
return nSerSize;
} }
void insert(const std::vector<unsigned char>& vKey); void insert(const std::vector<unsigned char>& vKey);

View file

@ -34,10 +34,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(FLATDATA(*this)); READWRITE(FLATDATA(*this));
return nSerSize;
} }
void SetNull() { hash = 0; n = (uint32_t) -1; } void SetNull() { hash = 0; n = (uint32_t) -1; }
@ -96,12 +94,10 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(prevout); READWRITE(prevout);
READWRITE(scriptSig); READWRITE(scriptSig);
READWRITE(nSequence); READWRITE(nSequence);
return nSerSize;
} }
bool IsFinal() const bool IsFinal() const
@ -152,10 +148,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(nSatoshisPerK); READWRITE(nSatoshisPerK);
return nSerSize;
} }
}; };
@ -179,11 +173,9 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(nValue); READWRITE(nValue);
READWRITE(scriptPubKey); READWRITE(scriptPubKey);
return nSerSize;
} }
void SetNull() void SetNull()
@ -264,9 +256,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
READWRITE(*const_cast<int32_t*>(&this->nVersion)); READWRITE(*const_cast<int32_t*>(&this->nVersion));
nVersion = this->nVersion; nVersion = this->nVersion;
@ -275,8 +266,6 @@ public:
READWRITE(*const_cast<uint32_t*>(&nLockTime)); READWRITE(*const_cast<uint32_t*>(&nLockTime));
if (fRead) if (fRead)
UpdateHash(); UpdateHash();
return nSerSize;
} }
bool IsNull() const { bool IsNull() const {
@ -327,16 +316,12 @@ struct CMutableTransaction
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(vin); READWRITE(vin);
READWRITE(vout); READWRITE(vout);
READWRITE(nLockTime); READWRITE(nLockTime);
return nSerSize;
} }
/** Compute the hash of this CMutableTransaction. This is computed on the /** Compute the hash of this CMutableTransaction. This is computed on the
@ -360,9 +345,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = boost::is_same<Operation, CSerActionUnserialize>(); bool fRead = ser_action.ForRead();
size_t nSerSize = 0;
if (!fRead) { if (!fRead) {
uint64_t nVal = CompressAmount(txout.nValue); uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal)); READWRITE(VARINT(nVal));
@ -373,7 +357,6 @@ public:
} }
CScriptCompressor cscript(REF(txout.scriptPubKey)); CScriptCompressor cscript(REF(txout.scriptPubKey));
READWRITE(cscript); READWRITE(cscript);
return nSerSize;
} }
}; };
@ -430,10 +413,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(vprevout); READWRITE(vprevout);
return nSerSize;
} }
}; };
@ -465,9 +446,7 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(hashPrevBlock); READWRITE(hashPrevBlock);
@ -475,8 +454,6 @@ public:
READWRITE(nTime); READWRITE(nTime);
READWRITE(nBits); READWRITE(nBits);
READWRITE(nNonce); READWRITE(nNonce);
return nSerSize;
} }
void SetNull() void SetNull()
@ -526,13 +503,9 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(*(CBlockHeader*)this); READWRITE(*(CBlockHeader*)this);
READWRITE(vtx); READWRITE(vtx);
return nSerSize;
} }
void SetNull() void SetNull()
@ -580,14 +553,10 @@ struct CBlockLocator
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(vHave); READWRITE(vHave);
return nSerSize;
} }
void SetNull() void SetNull()

View file

@ -46,14 +46,12 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(vchCryptedKey); READWRITE(vchCryptedKey);
READWRITE(vchSalt); READWRITE(vchSalt);
READWRITE(nDerivationMethod); READWRITE(nDerivationMethod);
READWRITE(nDeriveIterations); READWRITE(nDeriveIterations);
READWRITE(vchOtherDerivationParameters); READWRITE(vchOtherDerivationParameters);
return nSerSize;
} }
CMasterKey() CMasterKey()

View file

@ -200,11 +200,9 @@ struct CDiskBlockPos
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(VARINT(nFile)); READWRITE(VARINT(nFile));
READWRITE(VARINT(nPos)); READWRITE(VARINT(nPos));
return nSerSize;
} }
CDiskBlockPos() { CDiskBlockPos() {
@ -235,11 +233,9 @@ struct CDiskTxPos : public CDiskBlockPos
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(*(CDiskBlockPos*)this); READWRITE(*(CDiskBlockPos*)this);
READWRITE(VARINT(nTxOffset)); READWRITE(VARINT(nTxOffset));
return nSerSize;
} }
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) { CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
@ -320,10 +316,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(vtxundo); READWRITE(vtxundo);
return nSerSize;
} }
bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock); bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock);
@ -429,9 +423,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
READWRITE(nTransactions); READWRITE(nTransactions);
READWRITE(vHash); READWRITE(vHash);
@ -449,8 +442,6 @@ public:
vBytes[p / 8] |= vBits[p] << (p % 8); vBytes[p / 8] |= vBits[p] << (p % 8);
READWRITE(vBytes); READWRITE(vBytes);
} }
return nSerSize;
} }
// Construct a partial merkle tree from a list of transaction id's, and a mask that selects a subset of them // Construct a partial merkle tree from a list of transaction id's, and a mask that selects a subset of them
@ -510,9 +501,7 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(VARINT(nBlocks)); READWRITE(VARINT(nBlocks));
READWRITE(VARINT(nSize)); READWRITE(VARINT(nSize));
READWRITE(VARINT(nUndoSize)); READWRITE(VARINT(nUndoSize));
@ -520,8 +509,6 @@ public:
READWRITE(VARINT(nHeightLast)); READWRITE(VARINT(nHeightLast));
READWRITE(VARINT(nTimeFirst)); READWRITE(VARINT(nTimeFirst));
READWRITE(VARINT(nTimeLast)); READWRITE(VARINT(nTimeLast));
return nSerSize;
} }
void SetNull() { void SetNull() {
@ -788,9 +775,7 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(VARINT(nVersion)); READWRITE(VARINT(nVersion));
@ -811,8 +796,6 @@ public:
READWRITE(nTime); READWRITE(nTime);
READWRITE(nBits); READWRITE(nBits);
READWRITE(nNonce); READWRITE(nNonce);
return nSerSize;
} }
uint256 GetBlockHash() const uint256 GetBlockHash() const
@ -1014,11 +997,9 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(header); READWRITE(header);
READWRITE(txn); READWRITE(txn);
return nSerSize;
} }
}; };

View file

@ -91,10 +91,8 @@ class CNetAddr
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(FLATDATA(ip)); READWRITE(FLATDATA(ip));
return nSerSize;
} }
}; };
@ -155,16 +153,13 @@ class CService : public CNetAddr
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = boost::is_same<Operation, CSerActionUnserialize>(); bool fRead = ser_action.ForRead();
size_t nSerSize = 0;
CService* pthis = const_cast<CService*>(this);
READWRITE(FLATDATA(ip)); READWRITE(FLATDATA(ip));
unsigned short portN = htons(port); unsigned short portN = htons(port);
READWRITE(portN); READWRITE(portN);
if (fRead) if (fRead)
pthis->port = ntohs(portN); port = ntohs(portN);
return nSerSize;
} }
}; };

View file

@ -38,13 +38,11 @@ class CMessageHeader
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(FLATDATA(pchMessageStart)); READWRITE(FLATDATA(pchMessageStart));
READWRITE(FLATDATA(pchCommand)); READWRITE(FLATDATA(pchCommand));
READWRITE(nMessageSize); READWRITE(nMessageSize);
READWRITE(nChecksum); READWRITE(nChecksum);
return nSerSize;
} }
// TODO: make private (improves encapsulation) // TODO: make private (improves encapsulation)
@ -90,12 +88,10 @@ class CAddress : public CService
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
CAddress* pthis = const_cast<CAddress*>(this); CAddress* pthis = const_cast<CAddress*>(this);
CService* pip = (CService*)pthis;
if (fRead) if (fRead)
pthis->Init(); pthis->Init();
if (nType & SER_DISK) if (nType & SER_DISK)
@ -104,9 +100,7 @@ class CAddress : public CService
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
READWRITE(nTime); READWRITE(nTime);
READWRITE(nServices); READWRITE(nServices);
READWRITE(*pip); READWRITE(*(CService*)this);
return nSerSize;
} }
// TODO: make private (improves encapsulation) // TODO: make private (improves encapsulation)
@ -131,11 +125,9 @@ class CInv
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(type); READWRITE(type);
READWRITE(hash); READWRITE(hash);
return nSerSize;
} }
friend bool operator<(const CInv& a, const CInv& b); friend bool operator<(const CInv& a, const CInv& b);

View file

@ -27,9 +27,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
RecentRequestEntry* pthis = const_cast<RecentRequestEntry*>(this); RecentRequestEntry* pthis = const_cast<RecentRequestEntry*>(this);
@ -42,9 +41,7 @@ public:
READWRITE(recipient); READWRITE(recipient);
if (fRead) if (fRead)
pthis->date = QDateTime::fromTime_t(nDate); date = QDateTime::fromTime_t(nDate);
return nSerSize;
} }
}; };

View file

@ -62,9 +62,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
SendCoinsRecipient* pthis = const_cast<SendCoinsRecipient*>(this); SendCoinsRecipient* pthis = const_cast<SendCoinsRecipient*>(this);
@ -94,8 +93,6 @@ public:
pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size())); pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant); pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
} }
return nSerSize;
} }
}; };

View file

@ -22,7 +22,6 @@
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
#include <boost/type_traits/is_fundamental.hpp> #include <boost/type_traits/is_fundamental.hpp>
#include <boost/typeof/typeof.hpp>
class CAutoFile; class CAutoFile;
class CDataStream; class CDataStream;
@ -88,24 +87,25 @@ enum
SER_GETHASH = (1 << 2), SER_GETHASH = (1 << 2),
}; };
#define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action)) #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
/* Implement three methods for serializable objects. These are actually wrappers over /* Implement three methods for serializable objects. These are actually wrappers over
* "SerializationOp" template, which implements the body of each class' serialization * "SerializationOp" template, which implements the body of each class' serialization
* code. Adding "IMPLEMENT_SERIALIZE" in the body of the class causes these wrappers to be * code. Adding "IMPLEMENT_SERIALIZE" in the body of the class causes these wrappers to be
* added as members. */ * added as members. */
#define IMPLEMENT_SERIALIZE \ #define IMPLEMENT_SERIALIZE \
size_t GetSerializeSize(int nType, int nVersion) const { \ size_t GetSerializeSize(int nType, int nVersion) const { \
ser_streamplaceholder s; \ CSizeComputer s(nType, nVersion); \
return NCONST_PTR(this)->SerializationOp(s, CSerActionGetSerializeSize(), nType, nVersion); \ NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
} \ return s.size(); \
template<typename Stream> \ } \
void Serialize(Stream& s, int nType, int nVersion) const { \ template<typename Stream> \
NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion); \ void Serialize(Stream& s, int nType, int nVersion) const { \
} \ NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
template<typename Stream> \ } \
void Unserialize(Stream& s, int nType, int nVersion) { \ template<typename Stream> \
SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \ void Unserialize(Stream& s, int nType, int nVersion) { \
SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
} }
@ -809,32 +809,27 @@ void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
// //
// Support for IMPLEMENT_SERIALIZE and READWRITE macro // Support for IMPLEMENT_SERIALIZE and READWRITE macro
// //
class CSerActionGetSerializeSize { }; struct CSerActionSerialize
class CSerActionSerialize { };
class CSerActionUnserialize { };
template<typename Stream, typename T>
inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
{ {
return ::GetSerializeSize(obj, nType, nVersion); bool ForRead() const { return false; }
} };
struct CSerActionUnserialize
{
bool ForRead() const { return true; }
};
template<typename Stream, typename T> template<typename Stream, typename T>
inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action) inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
{ {
::Serialize(s, obj, nType, nVersion); ::Serialize(s, obj, nType, nVersion);
return 0;
} }
template<typename Stream, typename T> template<typename Stream, typename T>
inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action) inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
{ {
::Unserialize(s, obj, nType, nVersion); ::Unserialize(s, obj, nType, nVersion);
return 0;
} }
struct ser_streamplaceholder { };

View file

@ -66,13 +66,11 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(nTime); READWRITE(nTime);
READWRITE(vchPubKey); READWRITE(vchPubKey);
return nSerSize;
} }
}; };
@ -496,16 +494,12 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; READWRITE(*(CTransaction*)this);
nSerSize += SerReadWrite(s, *(CTransaction*)this, nType, nVersion, ser_action);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(hashBlock); READWRITE(hashBlock);
READWRITE(vMerkleBranch); READWRITE(vMerkleBranch);
READWRITE(nIndex); READWRITE(nIndex);
return nSerSize;
} }
int SetMerkleBranch(const CBlock* pblock=NULL); int SetMerkleBranch(const CBlock* pblock=NULL);
@ -613,9 +607,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
CWalletTx* pthis = const_cast<CWalletTx*>(this); CWalletTx* pthis = const_cast<CWalletTx*>(this);
if (fRead) if (fRead)
@ -632,7 +625,7 @@ public:
pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart); pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
} }
nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action); READWRITE(*(CMerkleTx*)this);
std::vector<CMerkleTx> vUnused; // Used to be vtxPrev std::vector<CMerkleTx> vUnused; // Used to be vtxPrev
READWRITE(vUnused); READWRITE(vUnused);
READWRITE(mapValue); READWRITE(mapValue);
@ -651,13 +644,11 @@ public:
pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0; pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
} }
pthis->mapValue.erase("fromaccount"); mapValue.erase("fromaccount");
pthis->mapValue.erase("version"); mapValue.erase("version");
pthis->mapValue.erase("spent"); mapValue.erase("spent");
pthis->mapValue.erase("n"); mapValue.erase("n");
pthis->mapValue.erase("timesmart"); mapValue.erase("timesmart");
return nSerSize;
} }
// make sure balances are recalculated // make sure balances are recalculated
@ -907,15 +898,13 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(vchPrivKey); READWRITE(vchPrivKey);
READWRITE(nTimeCreated); READWRITE(nTimeCreated);
READWRITE(nTimeExpires); READWRITE(nTimeExpires);
READWRITE(LIMITED_STRING(strComment, 65536)); READWRITE(LIMITED_STRING(strComment, 65536));
return nSerSize;
} }
}; };
@ -945,12 +934,10 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(vchPubKey); READWRITE(vchPubKey);
return nSerSize;
} }
}; };
@ -990,9 +977,8 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0; bool fRead = ser_action.ForRead();
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
CAccountingEntry& me = *const_cast<CAccountingEntry*>(this); CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
@ -1033,9 +1019,7 @@ public:
if (std::string::npos != nSepPos) if (std::string::npos != nSepPos)
me.strComment.erase(nSepPos); me.strComment.erase(nSepPos);
me.mapValue.erase("n"); mapValue.erase("n");
return nSerSize;
} }
private: private:

View file

@ -57,12 +57,10 @@ public:
IMPLEMENT_SERIALIZE; IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(nCreateTime); READWRITE(nCreateTime);
return nSerSize;
} }
void SetNull() void SetNull()