From 0e85204a1001397f11bad94f1c9c2cb11f8fb4af Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 10 Nov 2016 10:54:14 -0800 Subject: [PATCH] Add serialization for unique_ptr and shared_ptr --- src/serialize.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/serialize.h b/src/serialize.h index 91864e1b6..e28ca548c 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,20 @@ static const unsigned int MAX_SIZE = 0x02000000; +/** + * Dummy data type to identify deserializing constructors. + * + * By convention, a constructor of a type T with signature + * + * template T::T(deserialize_type, Stream& s) + * + * is a deserializing constructor, which builds the type by + * deserializing it from s. If T contains const fields, this + * is likely the only way to do so. + */ +struct deserialize_type {}; +constexpr deserialize_type deserialize {}; + /** * Used to bypass the rule against non-const reference to temporary * where it makes sense with wrappers such as CFlatData or CTxDB @@ -521,7 +536,17 @@ template voi template void Serialize(Stream& os, const std::set& m); template void Unserialize(Stream& is, std::set& m); +/** + * shared_ptr + */ +template void Serialize(Stream& os, const std::shared_ptr& p); +template void Unserialize(Stream& os, std::shared_ptr& p); +/** + * unique_ptr + */ +template void Serialize(Stream& os, const std::unique_ptr& p); +template void Unserialize(Stream& os, std::unique_ptr& p); @@ -775,6 +800,40 @@ void Unserialize(Stream& is, std::set& m) +/** + * unique_ptr + */ +template void +Serialize(Stream& os, const std::unique_ptr& p) +{ + Serialize(os, *p); +} + +template +void Unserialize(Stream& is, std::unique_ptr& p) +{ + p.reset(new T(deserialize, is)); +} + + + +/** + * shared_ptr + */ +template void +Serialize(Stream& os, const std::shared_ptr& p) +{ + Serialize(os, *p); +} + +template +void Unserialize(Stream& is, std::shared_ptr& p) +{ + p = std::make_shared(deserialize, is); +} + + + /** * Support for ADD_SERIALIZE_METHODS and READWRITE macro */