MultiversX Tracker is Live!

what is SERIALIZE_METHODS macro in the Bitcoin Core?

Bitcoin Stack Exchange

Bitcoin News / Bitcoin Stack Exchange 173 Views

 * Implement the Serialize and Unserialize methods by delegating to a single templated * static method that takes the to-be-(de)serialized object as a parameter. This approach * has the advantage that the constness of the object becomes a template parameter, and * thus allows a single implementation that sees the object as const for serializing * and non-const for deserializing, without casts. */
#define SERIALIZE_METHODS(cls, obj) \ template<typename Stream> \ void Serialize(Stream& s) const \ { \ static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \ Ser(s, *this); \ } \ template<typename Stream> \ void Unserialize(Stream& s) \ { \ static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \ Unser(s, *this); \ } \ FORMATTER_METHODS(cls, obj)

This macro is used frequently in the Bitcoin Core. As an example in CBlockFileInfo:

class CBlockFileInfo
{
public: unsigned int nBlocks; //!< number of blocks stored in file unsigned int nSize; //!< number of used bytes of block file unsigned int nUndoSize; //!< number of used bytes in the undo file unsigned int nHeightFirst; //!< lowest height of block in file unsigned int nHeightLast; //!< highest height of block in file uint64_t nTimeFirst; //!< earliest time of block in file uint64_t nTimeLast; //!< latest time of block in file SERIALIZE_METHODS(CBlockFileInfo, obj) { READWRITE(VARINT(obj.nBlocks)); READWRITE(VARINT(obj.nSize)); READWRITE(VARINT(obj.nUndoSize)); READWRITE(VARINT(obj.nHeightFirst)); READWRITE(VARINT(obj.nHeightLast)); READWRITE(VARINT(obj.nTimeFirst)); READWRITE(VARINT(obj.nTimeLast)); }

Can someone explain in simple language what does this macro actually do? I tried to figure out what it does by reading the comments above it and its implementation but didn't understand anything useful.


Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments