aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2018-03-15 16:48:42 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2018-03-15 16:57:55 -0700
commit7be9a9a570c1140048f8781ced1111e1d930e517 (patch)
treee5c1fe3eb3607fb5d8997805990fc633d553c4dd /src/serialize.h
parent2bac3e484114c30548e286972525dd799dbd0a5b (diff)
parent172f5fa738d419efda99542e2ad2a0f4db5be580 (diff)
downloadbitcoin-7be9a9a570c1140048f8781ced1111e1d930e517.tar.xz
Merge #12683: Fix more constness violations in serialization code
172f5fa738 Support deserializing into temporaries (Pieter Wuille) 2761bca997 Merge READWRITEMANY into READWRITE (Pieter Wuille) Pull request description: This is another fragment of improvements from #10785. The current serialization code does not support serializing/deserializing from/to temporaries (like `s >> CFlatData(script)`). As a result, there are many invocations of the `REF` macro which in addition to changing the reference type also changes the constness. This is unnecessary in C++11 as we can use rvalue references now instead. The first commit is an extra simplification we can make that removes the duplication of code between `READWRITE` and `READWRITEMANY` (and related functions). Tree-SHA512: babfa9cb268cc3bc39917e4f0a90e4651c33d85032161e16547a07f3b257b7ca7940e0cbfd69f09439d26fafbb1a6cf6359101043407e2c7aeececf7f20b6eed
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h52
1 files changed, 13 insertions, 39 deletions
diff --git a/src/serialize.h b/src/serialize.h
index dcc8d8691e..c454ba16b7 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -148,8 +148,7 @@ enum
SER_GETHASH = (1 << 2),
};
-#define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
-#define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
+#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
/**
* Implement three methods for serializable objects. These are actually wrappers over
@@ -351,10 +350,10 @@ I ReadVarInt(Stream& is)
}
}
-#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
-#define VARINT(obj) REF(WrapVarInt(REF(obj)))
-#define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
-#define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
+#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
+#define VARINT(obj) WrapVarInt(REF(obj))
+#define COMPACTSIZE(obj) CCompactSize(REF(obj))
+#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
/**
* Wrapper for serializing arrays and POD.
@@ -539,7 +538,7 @@ inline void Serialize(Stream& os, const T& a)
}
template<typename Stream, typename T>
-inline void Unserialize(Stream& is, T& a)
+inline void Unserialize(Stream& is, T&& a)
{
a.Unserialize(is);
}
@@ -825,19 +824,6 @@ struct CSerActionUnserialize
constexpr bool ForRead() const { return true; }
};
-template<typename Stream, typename T>
-inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
-{
- ::Serialize(s, obj);
-}
-
-template<typename Stream, typename T>
-inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
-{
- ::Unserialize(s, obj);
-}
-
-
@@ -897,17 +883,11 @@ void SerializeMany(Stream& s)
{
}
-template<typename Stream, typename Arg>
-void SerializeMany(Stream& s, Arg&& arg)
-{
- ::Serialize(s, std::forward<Arg>(arg));
-}
-
template<typename Stream, typename Arg, typename... Args>
-void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
+void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
{
- ::Serialize(s, std::forward<Arg>(arg));
- ::SerializeMany(s, std::forward<Args>(args)...);
+ ::Serialize(s, arg);
+ ::SerializeMany(s, args...);
}
template<typename Stream>
@@ -915,27 +895,21 @@ inline void UnserializeMany(Stream& s)
{
}
-template<typename Stream, typename Arg>
-inline void UnserializeMany(Stream& s, Arg& arg)
-{
- ::Unserialize(s, arg);
-}
-
template<typename Stream, typename Arg, typename... Args>
-inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
+inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
{
::Unserialize(s, arg);
::UnserializeMany(s, args...);
}
template<typename Stream, typename... Args>
-inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
+inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
{
- ::SerializeMany(s, std::forward<Args>(args)...);
+ ::SerializeMany(s, args...);
}
template<typename Stream, typename... Args>
-inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
+inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
{
::UnserializeMany(s, args...);
}