archive.hpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2018, CNRS
2 // Authors: Justin Carpentier <jcarpent@laas.fr>
3 
4 #ifndef __curves_serialization_archive_hpp__
5 #define __curves_serialization_archive_hpp__
6 #include <boost/archive/binary_iarchive.hpp>
7 #include <boost/archive/binary_oarchive.hpp>
8 #include <boost/archive/text_iarchive.hpp>
9 #include <boost/archive/text_oarchive.hpp>
10 #include <boost/archive/xml_iarchive.hpp>
11 #include <boost/archive/xml_oarchive.hpp>
12 #include <boost/serialization/version.hpp>
13 #include <fstream>
14 #include <stdexcept>
15 #include <string>
16 
17 /* Define the current version number for the serialization
18  * Must be increased everytime the save() method of a class is modified
19  * Or when a change is made to register_types()
20  * */
21 const unsigned int CURVES_API_VERSION = 1;
22 
23 #define SINGLE_ARG(...) \
24  __VA_ARGS__ // Macro used to be able to put comma in the following macro
25  // arguments
26 // Macro used to define the serialization version of a templated class
27 #define DEFINE_CLASS_TEMPLATE_VERSION(Template, Type) \
28  namespace boost { \
29  namespace serialization { \
30  template <Template> \
31  struct version<Type> { \
32  static constexpr unsigned int value = CURVES_API_VERSION; \
33  }; \
34  template <Template> \
35  constexpr unsigned int version<Type>::value; \
36  } \
37  }
38 
39 namespace ndcurves {
40 namespace serialization {
41 struct Serializable {
42  private:
43  template <class Derived>
44  Derived& derived() {
45  return *static_cast<Derived*>(this);
46  }
47  template <class Derived>
48  const Derived& derived() const {
49  return *static_cast<const Derived*>(this);
50  }
51 
52  public:
54  template <class Derived>
55  void loadFromText(const std::string& filename) {
56  std::ifstream ifs(filename.c_str());
57  if (ifs) {
58  boost::archive::text_iarchive ia(ifs);
59  ia >> derived<Derived>();
60  } else {
61  const std::string exception_message(filename +
62  " does not seem to be a valid file.");
63  throw std::invalid_argument(exception_message);
64  }
65  }
66 
68  template <class Derived>
69  void saveAsText(const std::string& filename) const {
70  std::ofstream ofs(filename.c_str());
71  if (ofs) {
72  boost::archive::text_oarchive oa(ofs);
73  oa << derived<Derived>();
74  } else {
75  const std::string exception_message(filename +
76  " does not seem to be a valid file.");
77  throw std::invalid_argument(exception_message);
78  }
79  }
80 
82  template <class Derived>
83  void loadFromXML(const std::string& filename, const std::string& tag_name) {
84  if (tag_name.empty()) {
85  throw std::invalid_argument("tag_name cannot be empty.");
86  }
87  std::ifstream ifs(filename.c_str());
88  if (ifs) {
89  boost::archive::xml_iarchive ia(ifs);
90  ia >>
91  boost::serialization::make_nvp(tag_name.c_str(), derived<Derived>());
92  } else {
93  const std::string exception_message(filename +
94  " does not seem to be a valid file.");
95  throw std::invalid_argument(exception_message);
96  }
97  }
98 
100  template <class Derived>
101  void saveAsXML(const std::string& filename,
102  const std::string& tag_name) const {
103  if (tag_name.empty()) {
104  throw std::invalid_argument("tag_name cannot be empty.");
105  }
106  std::ofstream ofs(filename.c_str());
107  if (ofs) {
108  boost::archive::xml_oarchive oa(ofs);
109  oa << boost::serialization::make_nvp(tag_name.c_str(),
110  derived<Derived>());
111  } else {
112  const std::string exception_message(filename +
113  " does not seem to be a valid file.");
114  throw std::invalid_argument(exception_message);
115  }
116  }
117 
119  template <class Derived>
120  void loadFromBinary(const std::string& filename) {
121  std::ifstream ifs(filename.c_str());
122  if (ifs) {
123  boost::archive::binary_iarchive ia(ifs);
124  ia >> derived<Derived>();
125  } else {
126  const std::string exception_message(filename +
127  " does not seem to be a valid file.");
128  throw std::invalid_argument(exception_message);
129  }
130  }
131 
133  template <class Derived>
134  void saveAsBinary(const std::string& filename) const {
135  std::ofstream ofs(filename.c_str());
136  if (ofs) {
137  boost::archive::binary_oarchive oa(ofs);
138  oa << derived<Derived>();
139  } else {
140  const std::string exception_message(filename +
141  " does not seem to be a valid file.");
142  throw std::invalid_argument(exception_message);
143  }
144  }
145 }; // End struct Serializable
146 
147 } // namespace serialization
148 
149 } // namespace ndcurves
150 
151 #endif // ifndef __multicontact_api_serialization_archive_hpp__
Definition: archive.hpp:41
Definition: bernstein.h:20
void loadFromBinary(const std::string &filename)
Loads a Derived object from an binary file.
Definition: archive.hpp:120
const unsigned int CURVES_API_VERSION
Definition: archive.hpp:21
void loadFromXML(const std::string &filename, const std::string &tag_name)
Loads a Derived object from an XML file.
Definition: archive.hpp:83
void saveAsBinary(const std::string &filename) const
Saved a Derived object as an binary file.
Definition: archive.hpp:134
void loadFromText(const std::string &filename)
Loads a Derived object from a text file.
Definition: archive.hpp:55
void saveAsXML(const std::string &filename, const std::string &tag_name) const
Saved a Derived object as an XML file.
Definition: archive.hpp:101
void saveAsText(const std::string &filename) const
Saved a Derived object as a text file.
Definition: archive.hpp:69