Replace READWRITEAS macro with AsBase wrapping function

Co-authored-by: Pieter Wuille <pieter@wuille.net>
This commit is contained in:
MarcoFalke 2020-07-11 09:55:38 +02:00
parent 1c1a02bbd2
commit aaaa3fa947
No known key found for this signature in database
7 changed files with 38 additions and 14 deletions

View file

@ -65,8 +65,7 @@ public:
SERIALIZE_METHODS(AddrInfo, obj)
{
READWRITEAS(CAddress, obj);
READWRITE(obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
READWRITE(AsBase<CAddress>(obj), obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
}
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)

View file

@ -14,8 +14,7 @@ struct CDiskTxPos : public FlatFilePos
SERIALIZE_METHODS(CDiskTxPos, obj)
{
READWRITEAS(FlatFilePos, obj);
READWRITE(VARINT(obj.nTxOffset));
READWRITE(AsBase<FlatFilePos>(obj), VARINT(obj.nTxOffset));
}
CDiskTxPos(const FlatFilePos &blockIn, unsigned int nTxOffsetIn) : FlatFilePos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {

View file

@ -540,8 +540,7 @@ public:
SERIALIZE_METHODS(CService, obj)
{
READWRITEAS(CNetAddr, obj);
READWRITE(Using<BigEndianFormatter<2>>(obj.port));
READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
}
friend class CServiceHash;

View file

@ -87,8 +87,7 @@ public:
SERIALIZE_METHODS(CBlock, obj)
{
READWRITEAS(CBlockHeader, obj);
READWRITE(obj.vtx);
READWRITE(AsBase<CBlockHeader>(obj), obj.vtx);
}
void SetNull()

View file

@ -433,7 +433,7 @@ public:
}
// Invoke V1/V2 serializer for CService parent object.
OverrideStream<Stream> os(&s, s.GetType(), use_v2 ? ADDRV2_FORMAT : 0);
SerReadWriteMany(os, ser_action, ReadWriteAsHelper<CService>(obj));
SerReadWriteMany(os, ser_action, AsBase<CService>(obj));
}
//! Always included in serialization. The behavior is unspecified if the value is not representable as uint32_t.

View file

@ -434,7 +434,7 @@ public:
CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
SERIALIZE_METHODS(CScript, obj) { READWRITEAS(CScriptBase, obj); }
SERIALIZE_METHODS(CScript, obj) { READWRITE(AsBase<CScriptBase>(obj)); }
explicit CScript(int64_t b) { operator<<(b); }
explicit CScript(opcodetype b) { operator<<(b); }

View file

@ -133,12 +133,40 @@ enum
SER_GETHASH = (1 << 2),
};
//! Convert the reference base type to X, without changing constness or reference type.
template<typename X> X& ReadWriteAsHelper(X& x) { return x; }
template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
/**
* Convert any argument to a reference to X, maintaining constness.
*
* This can be used in serialization code to invoke a base class's
* serialization routines.
*
* Example use:
* class Base { ... };
* class Child : public Base {
* int m_data;
* public:
* SERIALIZE_METHODS(Child, obj) {
* READWRITE(AsBase<Base>(obj), obj.m_data);
* }
* };
*
* static_cast cannot easily be used here, as the type of Obj will be const Child&
* during serialization and Child& during deserialization. AsBase will convert to
* const Base& and Base& appropriately.
*/
template <class Out, class In>
Out& AsBase(In& x)
{
static_assert(std::is_base_of_v<Out, In>);
return x;
}
template <class Out, class In>
const Out& AsBase(const In& x)
{
static_assert(std::is_base_of_v<Out, In>);
return x;
}
#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
#define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
#define SER_READ(obj, code) ::SerRead(s, ser_action, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
#define SER_WRITE(obj, code) ::SerWrite(s, ser_action, obj, [&](Stream& s, const Type& obj) { code; })