pointer_vector_wrapper.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_CEREAL_POINTER_VECTOR_WRAPPER_HPP
14 #define MLPACK_CORE_CEREAL_POINTER_VECTOR_WRAPPER_HPP
15 
16 #include <cereal/archives/json.hpp>
17 #include <cereal/archives/portable_binary.hpp>
18 #include <cereal/archives/xml.hpp>
19 #include <cereal/types/vector.hpp>
20 
21 #include "pointer_wrapper.hpp"
22 
23 namespace cereal {
24 
37 template<class T>
39 {
40  public:
41  PointerVectorWrapper(std::vector<T*>& pointerVec)
42  : pointerVector(pointerVec)
43  {}
44 
45  template<class Archive>
46  void save(Archive& ar) const
47  {
48  size_t vecSize = pointerVector.size();
49  ar(CEREAL_NVP(vecSize));
50  for (size_t i = 0; i < pointerVector.size(); ++i)
51  {
52  ar(CEREAL_POINTER(pointerVector.at(i)));
53  }
54  }
55 
56  template<class Archive>
57  void load(Archive& ar)
58  {
59  size_t vecSize = 0;
60  ar(CEREAL_NVP(vecSize));
61  pointerVector.resize(vecSize);
62  for (size_t i = 0; i < pointerVector.size(); ++i)
63  {
64  ar(CEREAL_POINTER(pointerVector.at(i)));
65  }
66  }
67 
68  private:
69  std::vector<T*>& pointerVector;
70 };
71 
78 template<class T>
80 make_pointer_vector(std::vector<T*>& t)
81 {
82  return PointerVectorWrapper<T>(t);
83 }
84 
93 #define CEREAL_VECTOR_POINTER(T) cereal::make_pointer_vector(T)
94 
95 } // namespace cereal
96 
97 #endif // CEREAL_POINTER_VECTOR_WRAPPER_HPP
The objective of this class is to create a wrapper for std::vector that hold pointers by adding also ...
#define CEREAL_POINTER(T)
Cereal does not support the serialization of raw pointer.
PointerVectorWrapper(std::vector< T *> &pointerVec)
PointerVectorWrapper< T > make_pointer_vector(std::vector< T *> &t)
Serialize an std::vector that holds raw pointer object by encapsulating them into a smart pointer...