From 5e5c8f86b60a8018e8801fb44bbe56ce97d9deef Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Mon, 11 Sep 2023 18:36:11 +1000 Subject: [PATCH] serialize: add SER_PARAMS_OPFUNC --- src/netaddress.h | 1 + src/protocol.h | 1 + src/serialize.h | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/src/netaddress.h b/src/netaddress.h index a0944c886fc..7d3659a11af 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -218,6 +218,7 @@ public: }; struct SerParams { const Encoding enc; + SER_PARAMS_OPFUNC }; static constexpr SerParams V1{Encoding::V1}; static constexpr SerParams V2{Encoding::V2}; diff --git a/src/protocol.h b/src/protocol.h index a7ca0c6f3e3..e1caba766ff 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -396,6 +396,7 @@ public: }; struct SerParams : CNetAddr::SerParams { const Format fmt; + SER_PARAMS_OPFUNC }; static constexpr SerParams V1_NETWORK{{CNetAddr::Encoding::V1}, Format::Network}; static constexpr SerParams V2_NETWORK{{CNetAddr::Encoding::V2}, Format::Network}; diff --git a/src/serialize.h b/src/serialize.h index 7e4788a5ca2..04b29d1ded2 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1192,4 +1192,17 @@ static auto WithParams(const Params& params, T&& t) return ParamsWrapper{params, t}; } +/** + * Helper macro for SerParams structs + * + * Allows you define SerParams instances and then apply them directly + * to an object via function call syntax, eg: + * + * constexpr SerParams FOO{....}; + * ss << FOO(obj); + */ +#define SER_PARAMS_OPFUNC \ + template \ + auto operator()(T&& t) const { return WithParams(*this, t); } + #endif // BITCOIN_SERIALIZE_H