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 __multicontact_api_serialization_archive_hpp__
5 #define __multicontact_api_serialization_archive_hpp__
6 
7 #include <boost/archive/binary_iarchive.hpp>
8 #include <boost/archive/binary_oarchive.hpp>
9 #include <boost/archive/text_iarchive.hpp>
10 #include <boost/archive/text_oarchive.hpp>
11 #include <boost/archive/xml_iarchive.hpp>
12 #include <boost/archive/xml_oarchive.hpp>
13 #include <boost/serialization/version.hpp>
14 #include <fstream>
15 #include <stdexcept>
16 #include <string>
17 
18 const unsigned int API_VERSION =
19  2; // must be increased everytime the save() method of a class is modified
20 
21 // Macro used to define the serialization version of a templated class
22 #define DEFINE_CLASS_TEMPLATE_VERSION(Template, Type) \
23  namespace boost { \
24  namespace serialization { \
25  template <Template> \
26  struct version<Type> { \
27  static constexpr unsigned int value = API_VERSION; \
28  }; \
29  template <Template> \
30  constexpr unsigned int version<Type>::value; \
31  } \
32  }
33 
34 namespace multicontact_api {
35 namespace serialization {
36 
37 template <class Derived>
38 struct Serializable {
39  private:
40  Derived& derived() { return *static_cast<Derived*>(this); }
41  const Derived& derived() const { return *static_cast<const Derived*>(this); }
42 
43  public:
45  void loadFromText(const std::string& filename) {
46  std::ifstream ifs(filename.c_str());
47  if (ifs) {
48  boost::archive::text_iarchive ia(ifs);
49  ia >> derived();
50  } else {
51  const std::string exception_message(filename +
52  " does not seem to be a valid file.");
53  throw std::invalid_argument(exception_message);
54  }
55  }
56 
58  void saveAsText(const std::string& filename) const {
59  std::ofstream ofs(filename.c_str());
60  if (ofs) {
61  boost::archive::text_oarchive oa(ofs);
62  oa << derived();
63  } else {
64  const std::string exception_message(filename +
65  " does not seem to be a valid file.");
66  throw std::invalid_argument(exception_message);
67  }
68  }
69 
71  void loadFromXML(const std::string& filename, const std::string& tag_name) {
72  assert(!tag_name.empty());
73  std::ifstream ifs(filename.c_str());
74  if (ifs) {
75  boost::archive::xml_iarchive ia(ifs);
76  ia >> boost::serialization::make_nvp(tag_name.c_str(), derived());
77  } else {
78  const std::string exception_message(filename +
79  " does not seem to be a valid file.");
80  throw std::invalid_argument(exception_message);
81  }
82  }
83 
85  void saveAsXML(const std::string& filename,
86  const std::string& tag_name) const {
87  assert(!tag_name.empty());
88  std::ofstream ofs(filename.c_str());
89  if (ofs) {
90  boost::archive::xml_oarchive oa(ofs);
91  oa << boost::serialization::make_nvp(tag_name.c_str(), 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  void loadFromBinary(const std::string& filename) {
101  std::ifstream ifs(filename.c_str());
102  if (ifs) {
103  boost::archive::binary_iarchive ia(ifs);
104  ia >> derived();
105  } else {
106  const std::string exception_message(filename +
107  " does not seem to be a valid file.");
108  throw std::invalid_argument(exception_message);
109  }
110  }
111 
113  void saveAsBinary(const std::string& filename) const {
114  std::ofstream ofs(filename.c_str());
115  if (ofs) {
116  boost::archive::binary_oarchive oa(ofs);
117  oa << derived();
118  } else {
119  const std::string exception_message(filename +
120  " does not seem to be a valid file.");
121  throw std::invalid_argument(exception_message);
122  }
123  }
124 };
125 
126 } // namespace serialization
127 
128 } // namespace multicontact_api
129 
130 #endif // ifndef __multicontact_api_serialization_archive_hpp__
void loadFromBinary(const std::string &filename)
Loads a Derived object from an binary file.
Definition: archive.hpp:100
void saveAsBinary(const std::string &filename) const
Saved a Derived object as an binary file.
Definition: archive.hpp:113
void loadFromXML(const std::string &filename, const std::string &tag_name)
Loads a Derived object from an XML file.
Definition: archive.hpp:71
void saveAsText(const std::string &filename) const
Saved a Derived object as a text file.
Definition: archive.hpp:58
void saveAsXML(const std::string &filename, const std::string &tag_name) const
Saved a Derived object as an XML file.
Definition: archive.hpp:85
Definition: ellipsoid.hpp:12
const unsigned int API_VERSION
Definition: archive.hpp:18
void loadFromText(const std::string &filename)
Loads a Derived object from a text file.
Definition: archive.hpp:45