@@ -167,9 +167,9 @@ const Out& AsBase(const In& x)
167167 return x;
168168}
169169
170- #define READWRITE (...) (:: SerReadWriteMany(s, ser_action , __VA_ARGS__))
171- #define SER_READ (obj, code ) :: SerRead(s, ser_action , obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
172- #define SER_WRITE (obj, code ) :: SerWrite(s, ser_action , obj, [&](Stream& s, const Type& obj) { code; })
170+ #define READWRITE (...) (ser_action. SerReadWriteMany(s, __VA_ARGS__))
171+ #define SER_READ (obj, code ) ser_action. SerRead(s, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
172+ #define SER_WRITE (obj, code ) ser_action. SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
173173
174174/* *
175175 * Implement the Ser and Unser methods needed for implementing a formatter (see Using below).
@@ -1008,17 +1008,65 @@ void Unserialize(Stream& is, std::shared_ptr<const T>& p)
10081008 p = std::make_shared<const T>(deserialize, is);
10091009}
10101010
1011+ /* *
1012+ * Support for (un)serializing many things at once
1013+ */
1014+
1015+ template <typename Stream, typename ... Args>
1016+ void SerializeMany (Stream& s, const Args&... args)
1017+ {
1018+ (::Serialize (s, args), ...);
1019+ }
1020+
1021+ template <typename Stream, typename ... Args>
1022+ inline void UnserializeMany (Stream& s, Args&&... args)
1023+ {
1024+ (::Unserialize (s, args), ...);
1025+ }
10111026
10121027/* *
10131028 * Support for all macros providing or using the ser_action parameter of the SerializationOps method.
10141029 */
10151030struct ActionSerialize {
1016- constexpr bool ForRead () const { return false ; }
1031+ static constexpr bool ForRead () { return false ; }
1032+
1033+ template <typename Stream, typename ... Args>
1034+ static void SerReadWriteMany (Stream& s, const Args&... args)
1035+ {
1036+ ::SerializeMany (s, args...);
1037+ }
1038+
1039+ template <typename Stream, typename Type, typename Fn>
1040+ static void SerRead (Stream& s, Type&&, Fn&&)
1041+ {
1042+ }
1043+
1044+ template <typename Stream, typename Type, typename Fn>
1045+ static void SerWrite (Stream& s, Type&& obj, Fn&& fn)
1046+ {
1047+ fn (s, std::forward<Type>(obj));
1048+ }
10171049};
10181050struct ActionUnserialize {
1019- constexpr bool ForRead () const { return true ; }
1020- };
1051+ static constexpr bool ForRead () { return true ; }
10211052
1053+ template <typename Stream, typename ... Args>
1054+ static void SerReadWriteMany (Stream& s, Args&&... args)
1055+ {
1056+ ::UnserializeMany (s, args...);
1057+ }
1058+
1059+ template <typename Stream, typename Type, typename Fn>
1060+ static void SerRead (Stream& s, Type&& obj, Fn&& fn)
1061+ {
1062+ fn (s, std::forward<Type>(obj));
1063+ }
1064+
1065+ template <typename Stream, typename Type, typename Fn>
1066+ static void SerWrite (Stream& s, Type&&, Fn&&)
1067+ {
1068+ }
1069+ };
10221070
10231071/* ::GetSerializeSize implementations
10241072 *
@@ -1065,52 +1113,6 @@ class CSizeComputer
10651113 int GetVersion () const { return nVersion; }
10661114};
10671115
1068- template <typename Stream, typename ... Args>
1069- void SerializeMany (Stream& s, const Args&... args)
1070- {
1071- (::Serialize (s, args), ...);
1072- }
1073-
1074- template <typename Stream, typename ... Args>
1075- inline void UnserializeMany (Stream& s, Args&&... args)
1076- {
1077- (::Unserialize (s, args), ...);
1078- }
1079-
1080- template <typename Stream, typename ... Args>
1081- inline void SerReadWriteMany (Stream& s, ActionSerialize ser_action, const Args&... args)
1082- {
1083- ::SerializeMany (s, args...);
1084- }
1085-
1086- template <typename Stream, typename ... Args>
1087- inline void SerReadWriteMany (Stream& s, ActionUnserialize ser_action, Args&&... args)
1088- {
1089- ::UnserializeMany (s, args...);
1090- }
1091-
1092- template <typename Stream, typename Type, typename Fn>
1093- inline void SerRead (Stream& s, ActionSerialize ser_action, Type&&, Fn&&)
1094- {
1095- }
1096-
1097- template <typename Stream, typename Type, typename Fn>
1098- inline void SerRead (Stream& s, ActionUnserialize ser_action, Type&& obj, Fn&& fn)
1099- {
1100- fn (s, std::forward<Type>(obj));
1101- }
1102-
1103- template <typename Stream, typename Type, typename Fn>
1104- inline void SerWrite (Stream& s, ActionSerialize ser_action, Type&& obj, Fn&& fn)
1105- {
1106- fn (s, std::forward<Type>(obj));
1107- }
1108-
1109- template <typename Stream, typename Type, typename Fn>
1110- inline void SerWrite (Stream& s, ActionUnserialize ser_action, Type&&, Fn&&)
1111- {
1112- }
1113-
11141116template <typename I>
11151117inline void WriteVarInt (CSizeComputer &s, I n)
11161118{
@@ -1161,12 +1163,11 @@ class ParamsStream
11611163template <typename Params, typename T>
11621164class ParamsWrapper
11631165{
1164- static_assert (std::is_lvalue_reference<T>::value, " ParamsWrapper needs an lvalue reference type T" );
11651166 const Params& m_params;
1166- T m_object;
1167+ T& m_object;
11671168
11681169public:
1169- explicit ParamsWrapper (const Params& params, T obj) : m_params{params}, m_object{obj} {}
1170+ explicit ParamsWrapper (const Params& params, T& obj) : m_params{params}, m_object{obj} {}
11701171
11711172 template <typename Stream>
11721173 void Serialize (Stream& s) const
@@ -1190,7 +1191,20 @@ class ParamsWrapper
11901191template <typename Params, typename T>
11911192static auto WithParams (const Params& params, T&& t)
11921193{
1193- return ParamsWrapper<Params, T& >{params, t};
1194+ return ParamsWrapper<Params, T>{params, t};
11941195}
11951196
1197+ /* *
1198+ * Helper macro for SerParams structs
1199+ *
1200+ * Allows you define SerParams instances and then apply them directly
1201+ * to an object via function call syntax, eg:
1202+ *
1203+ * constexpr SerParams FOO{....};
1204+ * ss << FOO(obj);
1205+ */
1206+ #define SER_PARAMS_OPFUNC \
1207+ template <typename T> \
1208+ auto operator ()(T&& t) const { return WithParams (*this , t); }
1209+
11961210#endif // BITCOIN_SERIALIZE_H
0 commit comments