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:
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -316,6 +316,7 @@ public:
SERIALIZE_METHODS(CAddress, obj)
{
+ SER_READ(obj, obj.Init());
int nVersion = s.GetVersion();
if (s.GetType() & SER_DISK) {
READWRITE(nVersion);
diff --git a/src/serialize.h b/src/serialize.h
index 43f35f7ebe6..ae6a028ce97 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -179,6 +179,7 @@ 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; })
/**
* Implement three methods for serializable objects. These are actually wrappers over
@@ -1089,6 +1090,17 @@ 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 I>
inline void WriteVarInt(CSizeComputer &s, I n)
{