Skip to content

Commit 5f90940

Browse files
sipafurszy
authored andcommitted
Add serialization for unique_ptr and shared_ptr
1 parent a9946da commit 5f90940

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/serialize.h

Lines changed: 63 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.h>
@@ -29,6 +30,20 @@
2930

3031
static const unsigned int MAX_SIZE = 0x02000000;
3132

33+
/**
34+
* Dummy data type to identify deserializing constructors.
35+
*
36+
* By convention, a constructor of a type T with signature
37+
*
38+
* template <typename Stream> T::T(deserialize_type, Stream& s)
39+
*
40+
* is a deserializing constructor, which builds the type by
41+
* deserializing it from s. If T contains const fields, this
42+
* is likely the only way to do so.
43+
*/
44+
struct deserialize_type {};
45+
constexpr deserialize_type deserialize {};
46+
3247
/**
3348
* Used to bypass the rule against non-const reference to temporary
3449
* where it makes sense with wrappers such as CFlatData or CTxDB
@@ -607,6 +622,19 @@ template<typename Stream, typename K, typename T, typename Pred, typename A> voi
607622
template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
608623
template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
609624

625+
/**
626+
* shared_ptr
627+
*/
628+
template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
629+
template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
630+
631+
/**
632+
* unique_ptr
633+
*/
634+
template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
635+
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
636+
637+
610638

611639
/**
612640
* If none of the specialized versions above matched, default to calling member function.
@@ -848,6 +876,41 @@ void Unserialize(Stream& is, std::set<K, Pred, A>& m)
848876
}
849877

850878

879+
880+
/**
881+
* unique_ptr
882+
*/
883+
template<typename Stream, typename T> void
884+
Serialize(Stream& os, const std::unique_ptr<const T>& p)
885+
{
886+
Serialize(os, *p);
887+
}
888+
889+
template<typename Stream, typename T>
890+
void Unserialize(Stream& is, std::unique_ptr<const T>& p)
891+
{
892+
p.reset(new T(deserialize, is));
893+
}
894+
895+
896+
897+
/**
898+
* shared_ptr
899+
*/
900+
template<typename Stream, typename T> void
901+
Serialize(Stream& os, const std::shared_ptr<const T>& p)
902+
{
903+
Serialize(os, *p);
904+
}
905+
906+
template<typename Stream, typename T>
907+
void Unserialize(Stream& is, std::shared_ptr<const T>& p)
908+
{
909+
p = std::make_shared<const T>(deserialize, is);
910+
}
911+
912+
913+
851914
/**
852915
* Support for ADD_SERIALIZE_METHODS and READWRITE macro
853916
*/

0 commit comments

Comments
 (0)