Skip to content

Commit 0e85204

Browse files
committed
Add serialization for unique_ptr and shared_ptr
1 parent 44adf68 commit 0e85204

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/serialize.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <ios>
1414
#include <limits>
1515
#include <map>
16+
#include <memory>
1617
#include <set>
1718
#include <stdint.h>
1819
#include <string>
@@ -24,6 +25,20 @@
2425

2526
static const unsigned int MAX_SIZE = 0x02000000;
2627

28+
/**
29+
* Dummy data type to identify deserializing constructors.
30+
*
31+
* By convention, a constructor of a type T with signature
32+
*
33+
* template <typename Stream> T::T(deserialize_type, Stream& s)
34+
*
35+
* is a deserializing constructor, which builds the type by
36+
* deserializing it from s. If T contains const fields, this
37+
* is likely the only way to do so.
38+
*/
39+
struct deserialize_type {};
40+
constexpr deserialize_type deserialize {};
41+
2742
/**
2843
* Used to bypass the rule against non-const reference to temporary
2944
* where it makes sense with wrappers such as CFlatData or CTxDB
@@ -521,7 +536,17 @@ template<typename Stream, typename K, typename T, typename Pred, typename A> voi
521536
template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
522537
template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
523538

539+
/**
540+
* shared_ptr
541+
*/
542+
template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
543+
template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
524544

545+
/**
546+
* unique_ptr
547+
*/
548+
template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
549+
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
525550

526551

527552

@@ -775,6 +800,40 @@ void Unserialize(Stream& is, std::set<K, Pred, A>& m)
775800

776801

777802

803+
/**
804+
* unique_ptr
805+
*/
806+
template<typename Stream, typename T> void
807+
Serialize(Stream& os, const std::unique_ptr<const T>& p)
808+
{
809+
Serialize(os, *p);
810+
}
811+
812+
template<typename Stream, typename T>
813+
void Unserialize(Stream& is, std::unique_ptr<const T>& p)
814+
{
815+
p.reset(new T(deserialize, is));
816+
}
817+
818+
819+
820+
/**
821+
* shared_ptr
822+
*/
823+
template<typename Stream, typename T> void
824+
Serialize(Stream& os, const std::shared_ptr<const T>& p)
825+
{
826+
Serialize(os, *p);
827+
}
828+
829+
template<typename Stream, typename T>
830+
void Unserialize(Stream& is, std::shared_ptr<const T>& p)
831+
{
832+
p = std::make_shared<const T>(deserialize, is);
833+
}
834+
835+
836+
778837
/**
779838
* Support for ADD_SERIALIZE_METHODS and READWRITE macro
780839
*/

0 commit comments

Comments
 (0)