Add serialization for unique_ptr and shared_ptr

This commit is contained in:
Pieter Wuille 2016-11-10 10:54:14 -08:00 committed by Jack Grigg
parent 87a5975ca0
commit 40cc9aa7dd
No known key found for this signature in database
GPG Key ID: 665DBCD284F7DAFF
1 changed files with 59 additions and 0 deletions

View File

@ -14,6 +14,7 @@
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <stdint.h>
#include <string>
@ -28,6 +29,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 <typename Stream> 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
@ -543,7 +558,17 @@ template<typename Stream, typename K, typename Pred, typename A> void Unserializ
template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m);
template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m);
/**
* shared_ptr
*/
template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
/**
* unique_ptr
*/
template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
@ -881,6 +906,40 @@ void Unserialize(Stream& is, std::list<T, A>& l)
/**
* unique_ptr
*/
template<typename Stream, typename T> void
Serialize(Stream& os, const std::unique_ptr<const T>& p)
{
Serialize(os, *p);
}
template<typename Stream, typename T>
void Unserialize(Stream& is, std::unique_ptr<const T>& p)
{
p.reset(new T(deserialize, is));
}
/**
* shared_ptr
*/
template<typename Stream, typename T> void
Serialize(Stream& os, const std::shared_ptr<const T>& p)
{
Serialize(os, *p);
}
template<typename Stream, typename T>
void Unserialize(Stream& is, std::shared_ptr<const T>& p)
{
p = std::make_shared<const T>(deserialize, is);
}
/**
* Support for ADD_SERIALIZE_METHODS and READWRITE macro
*/