|
13 | 13 | #include <ios> |
14 | 14 | #include <limits> |
15 | 15 | #include <map> |
| 16 | +#include <memory> |
16 | 17 | #include <set> |
17 | 18 | #include <stdint.h> |
18 | 19 | #include <string.h> |
|
29 | 30 |
|
30 | 31 | static const unsigned int MAX_SIZE = 0x02000000; |
31 | 32 |
|
| 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 | + |
32 | 47 | /** |
33 | 48 | * Used to bypass the rule against non-const reference to temporary |
34 | 49 | * 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 |
607 | 622 | template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m); |
608 | 623 | template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m); |
609 | 624 |
|
| 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 | + |
610 | 638 |
|
611 | 639 | /** |
612 | 640 | * 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) |
848 | 876 | } |
849 | 877 |
|
850 | 878 |
|
| 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 | + |
851 | 914 | /** |
852 | 915 | * Support for ADD_SERIALIZE_METHODS and READWRITE macro |
853 | 916 | */ |
|
0 commit comments