In commit “Convert everything except wallet/qt to new serialization” (4a185971f495ab89d869a4744d36aa1a8c9aea3f)
Is it safe to drop the Init call here? It seems needed to avoid leaving nTime uninitialized if READWRITE(obj.nTime) is skipped below.
I was experimenting a little bit with how to be able to bring back if (ser_action.ForRead())
ability in the new framework now that the object type can vary and may be const.
One option might be to have SER_READ
/ SER_WRITE
macros that contain code (similar to WITH_LOCK
) and only execute when reading or writing:
0--- a/src/protocol.h
1+++ b/src/protocol.h
2@@ -316,6 +316,7 @@ public:
3
4 SERIALIZE_METHODS(CAddress, obj)
5 {
6+ SER_READ(obj, obj.Init());
7 int nVersion = s.GetVersion();
8 if (s.GetType() & SER_DISK) {
9 READWRITE(nVersion);
10diff --git a/src/serialize.h b/src/serialize.h
11index 43f35f7ebe6..ae6a028ce97 100644
12--- a/src/serialize.h
13+++ b/src/serialize.h
14@@ -179,6 +179,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
15
16 #define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
17 #define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
18+#define SER_READ(obj, code) ::SerRead(s, ser_action, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
19
20 /**
21 * Implement three methods for serializable objects. These are actually wrappers over
22@@ -1089,6 +1090,17 @@ inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&
23 ::UnserializeMany(s, args...);
24 }
25
26+template<typename Stream, typename Type, typename Fn>
27+inline void SerRead(Stream& s, CSerActionSerialize ser_action, Type&&, Fn&&)
28+{
29+}
30+
31+template<typename Stream, typename Type, typename Fn>
32+inline void SerRead(Stream& s, CSerActionUnserialize ser_action, Type&& obj, Fn&& fn)
33+{
34+ fn(s, std::forward<Type>(obj));
35+}
36+
37 template<typename I>
38 inline void WriteVarInt(CSizeComputer &s, I n)
39 {