diff options
author | Russell Yanofsky <russ@yanofsky.org> | 2020-03-30 15:22:07 -0700 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2020-03-30 15:36:16 -0700 |
commit | d06fedd1bc26bf5bf2b203d4445aeaebccca780e (patch) | |
tree | f649ceffbb078b0f18bf9265f33a72f44e78a909 /src/serialize.h | |
parent | 6f9a1e5ad0a270d3b5a715f3e3ea0911193bf244 (diff) |
Add SER_READ and SER_WRITE for read/write-dependent statements
Extracted and extended by Pieter Wuille from a comment by Russ
Yanofsky (see
https://github.com/bitcoin/bitcoin/pull/18317#discussion_r398625457).
Diffstat (limited to 'src/serialize.h')
-rw-r--r-- | src/serialize.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/serialize.h b/src/serialize.h index a44cc2c001..82c332f92f 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -190,6 +190,8 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { 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; }) /** * Implement three methods for serializable objects. These are actually wrappers over @@ -1100,6 +1102,28 @@ inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&& ::UnserializeMany(s, args...); } +template<typename Stream, typename Type, typename Fn> +inline void SerRead(Stream& s, CSerActionSerialize ser_action, Type&&, Fn&&) +{ +} + +template<typename Stream, typename Type, typename Fn> +inline void SerRead(Stream& s, CSerActionUnserialize ser_action, Type&& obj, Fn&& fn) +{ + fn(s, std::forward<Type>(obj)); +} + +template<typename Stream, typename Type, typename Fn> +inline void SerWrite(Stream& s, CSerActionSerialize ser_action, Type&& obj, Fn&& fn) +{ + fn(s, std::forward<Type>(obj)); +} + +template<typename Stream, typename Type, typename Fn> +inline void SerWrite(Stream& s, CSerActionUnserialize ser_action, Type&&, Fn&&) +{ +} + template<typename I> inline void WriteVarInt(CSizeComputer &s, I n) { |