From 47eb76597efb7dadb36dd98bc20bd80b2db9cd50 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 1 Sep 2014 21:36:46 +0200 Subject: [PATCH] Serializer simplifications after IMPLEMENT_SERIALIZE overhaul --- src/core.h | 7 ++--- src/main.h | 4 +-- src/netbase.h | 3 +-- src/protocol.h | 9 +++---- src/qt/recentrequeststablemodel.h | 10 +++---- src/qt/walletmodel.h | 32 ++++++++++------------ src/wallet.h | 44 +++++++++++++------------------ 7 files changed, 43 insertions(+), 66 deletions(-) diff --git a/src/core.h b/src/core.h index 34c00c4142..ed37f7a3f8 100644 --- a/src/core.h +++ b/src/core.h @@ -257,14 +257,12 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - READWRITE(*const_cast(&this->nVersion)); nVersion = this->nVersion; READWRITE(*const_cast*>(&vin)); READWRITE(*const_cast*>(&vout)); READWRITE(*const_cast(&nLockTime)); - if (fRead) + if (ser_action.ForRead()) UpdateHash(); } @@ -346,8 +344,7 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - if (!fRead) { + if (!ser_action.ForRead()) { uint64_t nVal = CompressAmount(txout.nValue); READWRITE(VARINT(nVal)); } else { diff --git a/src/main.h b/src/main.h index 31a1131b83..1d1a9df10f 100644 --- a/src/main.h +++ b/src/main.h @@ -424,12 +424,10 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - READWRITE(nTransactions); READWRITE(vHash); std::vector vBytes; - if (fRead) { + if (ser_action.ForRead()) { READWRITE(vBytes); CPartialMerkleTree &us = *(const_cast(this)); us.vBits.resize(vBytes.size() * 8); diff --git a/src/netbase.h b/src/netbase.h index 9b52c0a415..a061a91556 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -154,11 +154,10 @@ class CService : public CNetAddr template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); READWRITE(FLATDATA(ip)); unsigned short portN = htons(port); READWRITE(portN); - if (fRead) + if (ser_action.ForRead()) port = ntohs(portN); } }; diff --git a/src/protocol.h b/src/protocol.h index e4b0991774..ddf096aeac 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -89,16 +89,13 @@ class CAddress : public CService template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - - CAddress* pthis = const_cast(this); - if (fRead) - pthis->Init(); + if (ser_action.ForRead()) + Init(); if (nType & SER_DISK) READWRITE(nVersion); if ((nType & SER_DISK) || (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) - READWRITE(nTime); + READWRITE(nTime); READWRITE(nServices); READWRITE(*(CService*)this); } diff --git a/src/qt/recentrequeststablemodel.h b/src/qt/recentrequeststablemodel.h index a558aa4942..cdb7e47f62 100644 --- a/src/qt/recentrequeststablemodel.h +++ b/src/qt/recentrequeststablemodel.h @@ -28,19 +28,15 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - - RecentRequestEntry* pthis = const_cast(this); - unsigned int nDate = date.toTime_t(); - READWRITE(pthis->nVersion); - nVersion = pthis->nVersion; + READWRITE(this->nVersion); + nVersion = this->nVersion; READWRITE(id); READWRITE(nDate); READWRITE(recipient); - if (fRead) + if (ser_action.ForRead()) date = QDateTime::fromTime_t(nDate); } }; diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index 2a9ac4650f..bd8a25bb94 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -63,20 +63,16 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - - SendCoinsRecipient* pthis = const_cast(this); - - std::string sAddress = pthis->address.toStdString(); - std::string sLabel = pthis->label.toStdString(); - std::string sMessage = pthis->message.toStdString(); + std::string sAddress = address.toStdString(); + std::string sLabel = label.toStdString(); + std::string sMessage = message.toStdString(); std::string sPaymentRequest; - if (!fRead && pthis->paymentRequest.IsInitialized()) - pthis->paymentRequest.SerializeToString(&sPaymentRequest); - std::string sAuthenticatedMerchant = pthis->authenticatedMerchant.toStdString(); + if (!ser_action.ForRead() && paymentRequest.IsInitialized()) + paymentRequest.SerializeToString(&sPaymentRequest); + std::string sAuthenticatedMerchant = authenticatedMerchant.toStdString(); - READWRITE(pthis->nVersion); - nVersion = pthis->nVersion; + READWRITE(this->nVersion); + nVersion = this->nVersion; READWRITE(sAddress); READWRITE(sLabel); READWRITE(amount); @@ -84,14 +80,14 @@ public: READWRITE(sPaymentRequest); READWRITE(sAuthenticatedMerchant); - if (fRead) + if (ser_action.ForRead()) { - pthis->address = QString::fromStdString(sAddress); - pthis->label = QString::fromStdString(sLabel); - pthis->message = QString::fromStdString(sMessage); + address = QString::fromStdString(sAddress); + label = QString::fromStdString(sLabel); + message = QString::fromStdString(sMessage); if (!sPaymentRequest.empty()) - pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size())); - pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant); + paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size())); + authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant); } } }; diff --git a/src/wallet.h b/src/wallet.h index cea61afed3..2fc953c516 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -609,21 +609,18 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - - CWalletTx* pthis = const_cast(this); - if (fRead) - pthis->Init(NULL); + if (ser_action.ForRead()) + Init(NULL); 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) - pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart); + mapValue["timesmart"] = strprintf("%u", nTimeSmart); } READWRITE(*(CMerkleTx*)this); @@ -636,13 +633,13 @@ public: READWRITE(fFromMe); 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"); @@ -979,9 +976,6 @@ public: template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - bool fRead = ser_action.ForRead(); - - CAccountingEntry& me = *const_cast(this); if (!(nType & SER_GETHASH)) READWRITE(nVersion); // Note: strAccount is serialized as part of the key, not here. @@ -989,9 +983,9 @@ public: READWRITE(nTime); READWRITE(LIMITED_STRING(strOtherAccount, 65536)); - if (!fRead) + if (!ser_action.ForRead()) { - WriteOrderPos(nOrderPos, me.mapValue); + WriteOrderPos(nOrderPos, mapValue); if (!(mapValue.empty() && _ssExtra.empty())) { @@ -999,26 +993,26 @@ public: ss.insert(ss.begin(), '\0'); ss << mapValue; ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end()); - me.strComment.append(ss.str()); + strComment.append(ss.str()); } } READWRITE(LIMITED_STRING(strComment, 65536)); size_t nSepPos = strComment.find("\0", 0, 1); - if (fRead) + if (ser_action.ForRead()) { - me.mapValue.clear(); + mapValue.clear(); if (std::string::npos != nSepPos) { CDataStream ss(std::vector(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion); - ss >> me.mapValue; - me._ssExtra = std::vector(ss.begin(), ss.end()); + ss >> mapValue; + _ssExtra = std::vector(ss.begin(), ss.end()); } - ReadOrderPos(me.nOrderPos, me.mapValue); + ReadOrderPos(nOrderPos, mapValue); } if (std::string::npos != nSepPos) - me.strComment.erase(nSepPos); + strComment.erase(nSepPos); mapValue.erase("n"); }