aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-07-07 16:06:56 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2018-03-20 17:08:06 -0700
commit818dc74ba2745872fd68d2158380fc8bd331210e (patch)
treef72178344d09e88a312da73abed27f1716b8cfdd /src/serialize.h
parent8ee5c7b747171e335793c74cd9d2f7491da58164 (diff)
downloadbitcoin-818dc74ba2745872fd68d2158380fc8bd331210e.tar.xz
Support serialization as another type without casting
This adds a READWRITEAS(type, obj) macro which serializes obj as if it were casted to (const type&) when const, and to (type&) when non-const. This makes it usable in serialization code that uses a single implementation for both serialization and deserializing, which doesn't know the constness of the object involved.
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/serialize.h b/src/serialize.h
index 91da6b0f80..341a138e8b 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -148,7 +148,12 @@ enum
SER_GETHASH = (1 << 2),
};
-#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
+//! Convert the reference base type to X, without changing constness or reference type.
+template<typename X> X& ReadWriteAsHelper(X& x) { return x; }
+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)))
/**
* Implement three methods for serializable objects. These are actually wrappers over