array_wrapper.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_CEREAL_ARRAY_WRAPPER_HPP
15 #define MLPACK_CORE_CEREAL_ARRAY_WRAPPER_HPP
16 
17 #include <cereal/archives/binary.hpp>
18 #include <cereal/archives/portable_binary.hpp>
19 #include <cereal/archives/xml.hpp>
20 #include <cereal/archives/json.hpp>
21 
22 namespace cereal {
23 
27 template<class T>
29 {
30  public:
31  ArrayWrapper(T*& addr, std::size_t& size) :
32  arrayAddress(addr),
33  arraySize(size)
34  {}
35 
36  template<class Archive>
37  void save(Archive& ar) const
38  {
39  ar(CEREAL_NVP(arraySize));
40  for (size_t i = 0; i < arraySize; ++i)
41  ar(cereal::make_nvp("item", arrayAddress[i]));
42  }
43 
44  template<class Archive>
45  void load(Archive& ar)
46  {
47  ar(CEREAL_NVP(arraySize));
48  delete[] arrayAddress;
49  if (arraySize == 0)
50  {
51  arrayAddress = NULL;
52  return;
53  }
54  arrayAddress = new T[arraySize];
55  for (size_t i = 0; i < arraySize; ++i)
56  ar(cereal::make_nvp("item", arrayAddress[i]));
57  }
58 
59  private:
60  ArrayWrapper& operator=(ArrayWrapper rhs);
61 
62  T*& arrayAddress;
63  size_t& arraySize;
64 };
65 
72 template<class T, class S>
73 inline
75 {
76  return ArrayWrapper<T>(t, s);
77 }
78 
87 #define CEREAL_POINTER_ARRAY(T, S) cereal::make_array(T, S)
88 
89 } // namespace cereal
90 
91 #endif // CEREAL_ARRAY_WRAPPER_HPP
ArrayWrapper< T > make_array(T *&t, S &s)
This function is used to serialized old c-style array.
This class is used as a shim for cereal to be able to serialize a raw pointer array.
ArrayWrapper(T *&addr, std::size_t &size)
void load(Archive &ar)
void save(Archive &ar) const