archive.hpp 3.63 KB
Newer Older
Justin Carpentier's avatar
Justin Carpentier committed
1
2
3
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>

Guilhem Saurel's avatar
Guilhem Saurel committed
4
5
#ifndef __multicontact_api_serialization_archive_hpp__
#define __multicontact_api_serialization_archive_hpp__
Justin Carpentier's avatar
Justin Carpentier committed
6
7
8
9
10
11
12
13
14
15
16

#include <fstream>
#include <string>
#include <stdexcept>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>

Guilhem Saurel's avatar
Guilhem Saurel committed
17
18
namespace multicontact_api {
namespace serialization {
Justin Carpentier's avatar
Justin Carpentier committed
19

Guilhem Saurel's avatar
Guilhem Saurel committed
20
21
22
23
24
template <class Derived>
struct Serializable {
 private:
  Derived& derived() { return *static_cast<Derived*>(this); }
  const Derived& derived() const { return *static_cast<const Derived*>(this); }
Guilhem Saurel's avatar
Guilhem Saurel committed
25

Guilhem Saurel's avatar
Guilhem Saurel committed
26
27
28
29
30
31
32
33
34
35
36
37
 public:
  /// \brief Loads a Derived object from a text file.
  void loadFromText(const std::string& filename) throw(std::invalid_argument) {
    std::ifstream ifs(filename.c_str());
    if (ifs) {
      boost::archive::text_iarchive ia(ifs);
      ia >> derived();
    } else {
      const std::string exception_message(filename + " does not seem to be a valid file.");
      throw std::invalid_argument(exception_message);
    }
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
38

Guilhem Saurel's avatar
Guilhem Saurel committed
39
40
41
42
43
44
45
46
47
48
49
  /// \brief Saved a Derived object as a text file.
  void saveAsText(const std::string& filename) const throw(std::invalid_argument) {
    std::ofstream ofs(filename.c_str());
    if (ofs) {
      boost::archive::text_oarchive oa(ofs);
      oa << derived();
    } else {
      const std::string exception_message(filename + " does not seem to be a valid file.");
      throw std::invalid_argument(exception_message);
    }
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
50

Guilhem Saurel's avatar
Guilhem Saurel committed
51
52
53
54
55
56
57
58
59
60
61
62
  /// \brief Loads a Derived object from an XML file.
  void loadFromXML(const std::string& filename, const std::string& tag_name) throw(std::invalid_argument) {
    assert(!tag_name.empty());
    std::ifstream ifs(filename.c_str());
    if (ifs) {
      boost::archive::xml_iarchive ia(ifs);
      ia >> boost::serialization::make_nvp(tag_name.c_str(), derived());
    } else {
      const std::string exception_message(filename + " does not seem to be a valid file.");
      throw std::invalid_argument(exception_message);
    }
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
63

Guilhem Saurel's avatar
Guilhem Saurel committed
64
65
66
67
68
69
70
71
72
73
74
75
  /// \brief Saved a Derived object as an XML file.
  void saveAsXML(const std::string& filename, const std::string& tag_name) const throw(std::invalid_argument) {
    assert(!tag_name.empty());
    std::ofstream ofs(filename.c_str());
    if (ofs) {
      boost::archive::xml_oarchive oa(ofs);
      oa << boost::serialization::make_nvp(tag_name.c_str(), derived());
    } else {
      const std::string exception_message(filename + " does not seem to be a valid file.");
      throw std::invalid_argument(exception_message);
    }
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
76

Guilhem Saurel's avatar
Guilhem Saurel committed
77
78
79
80
81
82
83
84
85
86
87
  /// \brief Loads a Derived object from an binary file.
  void loadFromBinary(const std::string& filename) throw(std::invalid_argument) {
    std::ifstream ifs(filename.c_str());
    if (ifs) {
      boost::archive::binary_iarchive ia(ifs);
      ia >> derived();
    } else {
      const std::string exception_message(filename + " does not seem to be a valid file.");
      throw std::invalid_argument(exception_message);
    }
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
88

Guilhem Saurel's avatar
Guilhem Saurel committed
89
90
91
92
93
94
95
96
97
98
  /// \brief Saved a Derived object as an binary file.
  void saveAsBinary(const std::string& filename) const throw(std::invalid_argument) {
    std::ofstream ofs(filename.c_str());
    if (ofs) {
      boost::archive::binary_oarchive oa(ofs);
      oa << derived();
    } else {
      const std::string exception_message(filename + " does not seem to be a valid file.");
      throw std::invalid_argument(exception_message);
    }
Justin Carpentier's avatar
Justin Carpentier committed
99
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
100
101
102
};

}  // namespace serialization
Guilhem Saurel's avatar
Guilhem Saurel committed
103

Guilhem Saurel's avatar
Guilhem Saurel committed
104
}  // namespace multicontact_api
Justin Carpentier's avatar
Justin Carpentier committed
105

Guilhem Saurel's avatar
Guilhem Saurel committed
106
#endif  // ifndef __multicontact_api_serialization_archive_hpp__