pointer_wrapper.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_CEREAL_POINTER_WRAPPER_HPP
14 #define MLPACK_CORE_CEREAL_POINTER_WRAPPER_HPP
15 
16 #include <cereal/archives/binary.hpp>
17 #include <cereal/archives/json.hpp>
18 #include <cereal/archives/portable_binary.hpp>
19 #include <cereal/archives/xml.hpp>
20 #include <cereal/types/memory.hpp>
21 
22 #if __cplusplus <= 201103L && !defined(_MSC_VER)
23 namespace std {
24 template<typename T, typename... Args>
25 std::unique_ptr<T> make_unique(Args&&... args)
26 {
27  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
28 }
29 } // namepace std
30 #endif
31 
32 namespace cereal {
33 
43 template<class T>
45 {
46  public:
47  PointerWrapper(T*& pointer)
48  : localPointer(pointer)
49  {}
50 
51  template<class Archive>
52  void save(Archive& ar, const uint32_t /*version*/) const
53  {
54  std::unique_ptr<T> smartPointer;
55  if (this->localPointer != NULL)
56  smartPointer = std::unique_ptr<T>(localPointer);
57  ar(CEREAL_NVP(smartPointer));
58  localPointer = smartPointer.release();
59  }
60 
61  template<class Archive>
62  void load(Archive& ar, const uint32_t /*version*/)
63  {
64  std::unique_ptr<T> smartPointer;
65  ar(CEREAL_NVP(smartPointer));
66  localPointer = smartPointer.release();
67  }
68 
69  T*& release() { return localPointer; }
70 
71  private:
72  T*& localPointer;
73 };
74 
81 template<class T>
82 inline PointerWrapper<T>
84 {
85  return PointerWrapper<T>(t);
86 }
87 
96 #define CEREAL_POINTER(T) cereal::make_pointer(T)
97 
98 } // namespace cereal
99 
100 #endif // CEREAL_POINTER_WRAPPER_HPP
void load(Archive &ar, const uint32_t)
The objective of this class is to create a wrapper for raw pointer by encapsulating them in a smart p...
void save(Archive &ar, const uint32_t) const
std::unique_ptr< T > make_unique(Args &&... args)
PointerWrapper< T > make_pointer(T *&t)
Serialize raw pointer object by encapsulating the pointer into a smart pointer.