aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2018-04-10 19:56:22 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2018-04-10 20:54:33 +0200
commit0a8054e7cd5c76d01e4ac7234e3883d05f6f5fdd (patch)
treeff45a44e6f9bf3a76df3f5f23a80208b550d7a96 /src/serialize.h
parenta84b056d5f1bc28b08937af3485124e68bb93024 (diff)
parent818dc74ba2745872fd68d2158380fc8bd331210e (diff)
downloadbitcoin-0a8054e7cd5c76d01e4ac7234e3883d05f6f5fdd.tar.xz
Merge #12731: Support serialization as another type without casting
818dc74 Support serialization as another type without casting (Pieter Wuille) Pull request description: This adds a `READWRITEAS(type, obj)` macro which serializes `obj` as if it were converted to `const type&` when `const`, and to `type&` when non-`const`. No actual cast is involved, so this only works when this conversion can be done automatically. 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. This is a redo of #12712, using a slightly different interface. Tree-SHA512: 262f0257284ff99b5ffaec9b997c194e221522ba35c3ac8eaa9bb344449d7ea0a314de254dc77449fa7aaa600f8cd9a24da65aade8c1ec6aa80c6e9a7bba5ca7
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 e90b041cc2..57e434faf1 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -155,7 +155,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