Serializer simplifications after IMPLEMENT_SERIALIZE overhaul

This commit is contained in:
Pieter Wuille 2014-09-01 21:36:46 +02:00
parent 2e731f24b5
commit 47eb76597e
7 changed files with 43 additions and 66 deletions

View file

@ -257,14 +257,12 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
READWRITE(*const_cast<int32_t*>(&this->nVersion)); READWRITE(*const_cast<int32_t*>(&this->nVersion));
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(*const_cast<std::vector<CTxIn>*>(&vin)); READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
READWRITE(*const_cast<std::vector<CTxOut>*>(&vout)); READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
READWRITE(*const_cast<uint32_t*>(&nLockTime)); READWRITE(*const_cast<uint32_t*>(&nLockTime));
if (fRead) if (ser_action.ForRead())
UpdateHash(); UpdateHash();
} }
@ -346,8 +344,7 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead(); if (!ser_action.ForRead()) {
if (!fRead) {
uint64_t nVal = CompressAmount(txout.nValue); uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal)); READWRITE(VARINT(nVal));
} else { } else {

View file

@ -424,12 +424,10 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
READWRITE(nTransactions); READWRITE(nTransactions);
READWRITE(vHash); READWRITE(vHash);
std::vector<unsigned char> vBytes; std::vector<unsigned char> vBytes;
if (fRead) { if (ser_action.ForRead()) {
READWRITE(vBytes); READWRITE(vBytes);
CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree*>(this)); CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree*>(this));
us.vBits.resize(vBytes.size() * 8); us.vBits.resize(vBytes.size() * 8);

View file

@ -154,11 +154,10 @@ class CService : public CNetAddr
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
READWRITE(FLATDATA(ip)); READWRITE(FLATDATA(ip));
unsigned short portN = htons(port); unsigned short portN = htons(port);
READWRITE(portN); READWRITE(portN);
if (fRead) if (ser_action.ForRead())
port = ntohs(portN); port = ntohs(portN);
} }
}; };

View file

@ -89,16 +89,13 @@ class CAddress : public CService
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead(); if (ser_action.ForRead())
Init();
CAddress* pthis = const_cast<CAddress*>(this);
if (fRead)
pthis->Init();
if (nType & SER_DISK) if (nType & SER_DISK)
READWRITE(nVersion); READWRITE(nVersion);
if ((nType & SER_DISK) || if ((nType & SER_DISK) ||
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
READWRITE(nTime); READWRITE(nTime);
READWRITE(nServices); READWRITE(nServices);
READWRITE(*(CService*)this); READWRITE(*(CService*)this);
} }

View file

@ -28,19 +28,15 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
RecentRequestEntry* pthis = const_cast<RecentRequestEntry*>(this);
unsigned int nDate = date.toTime_t(); unsigned int nDate = date.toTime_t();
READWRITE(pthis->nVersion); READWRITE(this->nVersion);
nVersion = pthis->nVersion; nVersion = this->nVersion;
READWRITE(id); READWRITE(id);
READWRITE(nDate); READWRITE(nDate);
READWRITE(recipient); READWRITE(recipient);
if (fRead) if (ser_action.ForRead())
date = QDateTime::fromTime_t(nDate); date = QDateTime::fromTime_t(nDate);
} }
}; };

View file

@ -63,20 +63,16 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead(); std::string sAddress = address.toStdString();
std::string sLabel = label.toStdString();
SendCoinsRecipient* pthis = const_cast<SendCoinsRecipient*>(this); std::string sMessage = message.toStdString();
std::string sAddress = pthis->address.toStdString();
std::string sLabel = pthis->label.toStdString();
std::string sMessage = pthis->message.toStdString();
std::string sPaymentRequest; std::string sPaymentRequest;
if (!fRead && pthis->paymentRequest.IsInitialized()) if (!ser_action.ForRead() && paymentRequest.IsInitialized())
pthis->paymentRequest.SerializeToString(&sPaymentRequest); paymentRequest.SerializeToString(&sPaymentRequest);
std::string sAuthenticatedMerchant = pthis->authenticatedMerchant.toStdString(); std::string sAuthenticatedMerchant = authenticatedMerchant.toStdString();
READWRITE(pthis->nVersion); READWRITE(this->nVersion);
nVersion = pthis->nVersion; nVersion = this->nVersion;
READWRITE(sAddress); READWRITE(sAddress);
READWRITE(sLabel); READWRITE(sLabel);
READWRITE(amount); READWRITE(amount);
@ -84,14 +80,14 @@ public:
READWRITE(sPaymentRequest); READWRITE(sPaymentRequest);
READWRITE(sAuthenticatedMerchant); READWRITE(sAuthenticatedMerchant);
if (fRead) if (ser_action.ForRead())
{ {
pthis->address = QString::fromStdString(sAddress); address = QString::fromStdString(sAddress);
pthis->label = QString::fromStdString(sLabel); label = QString::fromStdString(sLabel);
pthis->message = QString::fromStdString(sMessage); message = QString::fromStdString(sMessage);
if (!sPaymentRequest.empty()) if (!sPaymentRequest.empty())
pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size())); paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant); authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
} }
} }
}; };

View file

@ -609,21 +609,18 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead(); if (ser_action.ForRead())
Init(NULL);
CWalletTx* pthis = const_cast<CWalletTx*>(this);
if (fRead)
pthis->Init(NULL);
char fSpent = false; char fSpent = false;
if (!fRead) if (!ser_action.ForRead())
{ {
pthis->mapValue["fromaccount"] = pthis->strFromAccount; mapValue["fromaccount"] = strFromAccount;
WriteOrderPos(pthis->nOrderPos, pthis->mapValue); WriteOrderPos(nOrderPos, mapValue);
if (nTimeSmart) if (nTimeSmart)
pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart); mapValue["timesmart"] = strprintf("%u", nTimeSmart);
} }
READWRITE(*(CMerkleTx*)this); READWRITE(*(CMerkleTx*)this);
@ -636,13 +633,13 @@ public:
READWRITE(fFromMe); READWRITE(fFromMe);
READWRITE(fSpent); READWRITE(fSpent);
if (fRead) if (ser_action.ForRead())
{ {
pthis->strFromAccount = pthis->mapValue["fromaccount"]; strFromAccount = mapValue["fromaccount"];
ReadOrderPos(pthis->nOrderPos, pthis->mapValue); ReadOrderPos(nOrderPos, mapValue);
pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0; nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(mapValue["timesmart"]) : 0;
} }
mapValue.erase("fromaccount"); mapValue.erase("fromaccount");
@ -979,9 +976,6 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
// Note: strAccount is serialized as part of the key, not here. // Note: strAccount is serialized as part of the key, not here.
@ -989,9 +983,9 @@ public:
READWRITE(nTime); READWRITE(nTime);
READWRITE(LIMITED_STRING(strOtherAccount, 65536)); READWRITE(LIMITED_STRING(strOtherAccount, 65536));
if (!fRead) if (!ser_action.ForRead())
{ {
WriteOrderPos(nOrderPos, me.mapValue); WriteOrderPos(nOrderPos, mapValue);
if (!(mapValue.empty() && _ssExtra.empty())) if (!(mapValue.empty() && _ssExtra.empty()))
{ {
@ -999,26 +993,26 @@ public:
ss.insert(ss.begin(), '\0'); ss.insert(ss.begin(), '\0');
ss << mapValue; ss << mapValue;
ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end()); ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
me.strComment.append(ss.str()); strComment.append(ss.str());
} }
} }
READWRITE(LIMITED_STRING(strComment, 65536)); READWRITE(LIMITED_STRING(strComment, 65536));
size_t nSepPos = strComment.find("\0", 0, 1); size_t nSepPos = strComment.find("\0", 0, 1);
if (fRead) if (ser_action.ForRead())
{ {
me.mapValue.clear(); mapValue.clear();
if (std::string::npos != nSepPos) if (std::string::npos != nSepPos)
{ {
CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion); CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
ss >> me.mapValue; ss >> mapValue;
me._ssExtra = std::vector<char>(ss.begin(), ss.end()); _ssExtra = std::vector<char>(ss.begin(), ss.end());
} }
ReadOrderPos(me.nOrderPos, me.mapValue); ReadOrderPos(nOrderPos, mapValue);
} }
if (std::string::npos != nSepPos) if (std::string::npos != nSepPos)
me.strComment.erase(nSepPos); strComment.erase(nSepPos);
mapValue.erase("n"); mapValue.erase("n");
} }