Skip to content

Commit 818dc74

Browse files
committed
Support serialization as another type without casting
This adds a READWRITEAS(type, obj) macro which serializes obj as if it were casted to (const type&) when const, and to (type&) when non-const. This makes it usable in serialization code that uses a single implementation for both serialization and deserializing, which doesn't know the constness of the object involved.
1 parent 8ee5c7b commit 818dc74

File tree

7 files changed

+13
-8
lines changed

7 files changed

+13
-8
lines changed

src/addrman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CAddrInfo : public CAddress
5959

6060
template <typename Stream, typename Operation>
6161
inline void SerializationOp(Stream& s, Operation ser_action) {
62-
READWRITE(*static_cast<CAddress*>(this));
62+
READWRITEAS(CAddress, *this);
6363
READWRITE(source);
6464
READWRITE(nLastSuccess);
6565
READWRITE(nAttempts);

src/primitives/block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class CBlock : public CBlockHeader
9393

9494
template <typename Stream, typename Operation>
9595
inline void SerializationOp(Stream& s, Operation ser_action) {
96-
READWRITE(*static_cast<CBlockHeader*>(this));
96+
READWRITEAS(CBlockHeader, *this);
9797
READWRITE(vtx);
9898
}
9999

src/protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class CAddress : public CService
349349
uint64_t nServicesInt = nServices;
350350
READWRITE(nServicesInt);
351351
nServices = static_cast<ServiceFlags>(nServicesInt);
352-
READWRITE(*static_cast<CService*>(this));
352+
READWRITEAS(CService, *this);
353353
}
354354

355355
// TODO: make private (improves encapsulation)

src/script/script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class CScript : public CScriptBase
415415

416416
template <typename Stream, typename Operation>
417417
inline void SerializationOp(Stream& s, Operation ser_action) {
418-
READWRITE(static_cast<CScriptBase&>(*this));
418+
READWRITEAS(CScriptBase, *this);
419419
}
420420

421421
CScript& operator+=(const CScript& b)

src/serialize.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ enum
148148
SER_GETHASH = (1 << 2),
149149
};
150150

151-
#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
151+
//! Convert the reference base type to X, without changing constness or reference type.
152+
template<typename X> X& ReadWriteAsHelper(X& x) { return x; }
153+
template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
154+
155+
#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
156+
#define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
152157

153158
/**
154159
* Implement three methods for serializable objects. These are actually wrappers over

src/txdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct CDiskTxPos : public CDiskBlockPos
4646

4747
template <typename Stream, typename Operation>
4848
inline void SerializationOp(Stream& s, Operation ser_action) {
49-
READWRITE(*static_cast<CDiskBlockPos*>(this));
49+
READWRITEAS(CDiskBlockPos, *this);
5050
READWRITE(VARINT(nTxOffset));
5151
}
5252

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class CWalletTx : public CMerkleTx
395395
mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
396396
}
397397

398-
s << *static_cast<const CMerkleTx*>(this);
398+
s << static_cast<const CMerkleTx&>(*this);
399399
std::vector<CMerkleTx> vUnused; //!< Used to be vtxPrev
400400
s << vUnused << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << fSpent;
401401
}
@@ -406,7 +406,7 @@ class CWalletTx : public CMerkleTx
406406
Init(nullptr);
407407
char fSpent;
408408

409-
s >> *static_cast<CMerkleTx*>(this);
409+
s >> static_cast<CMerkleTx&>(*this);
410410
std::vector<CMerkleTx> vUnused; //!< Used to be vtxPrev
411411
s >> vUnused >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> fSpent;
412412

0 commit comments

Comments
 (0)