Loading...
Searching...
No Matches
archive.hpp
Go to the documentation of this file.
1#ifndef __parametric_curves_serialization_archive_hpp__
2#define __parametric_curves_serialization_archive_hpp__
3
4#include <boost/archive/binary_iarchive.hpp>
5#include <boost/archive/binary_oarchive.hpp>
6#include <boost/archive/text_iarchive.hpp>
7#include <boost/archive/text_oarchive.hpp>
8#include <boost/archive/xml_iarchive.hpp>
9#include <boost/archive/xml_oarchive.hpp>
10#include <fstream>
11#include <stdexcept>
12#include <string>
13
14namespace serialization {
15template <class Derived>
17 private:
18 Derived& derived() { return *static_cast<Derived*>(this); }
19 const Derived& derived() const { return *static_cast<const Derived*>(this); }
20
21 public:
23 void loadFromText(const std::string& filename) {
24 std::ifstream ifs(filename.c_str());
25 if (ifs) {
26 boost::archive::text_iarchive ia(ifs);
27 ia >> derived();
28 } else {
29 const std::string exception_message(filename +
30 " does not seem to be a valid file.");
31 throw std::invalid_argument(exception_message);
32 }
33 }
34
36 void saveAsText(const std::string& filename) const {
37 std::ofstream ofs(filename.c_str());
38 if (ofs) {
39 boost::archive::text_oarchive oa(ofs);
40 oa << derived();
41 } else {
42 const std::string exception_message(filename +
43 " does not seem to be a valid file.");
44 throw std::invalid_argument(exception_message);
45 }
46 }
47
49 void loadFromXML(const std::string& filename, const std::string& tag_name) {
50 assert(!tag_name.empty());
51 std::ifstream ifs(filename.c_str());
52 if (ifs) {
53 boost::archive::xml_iarchive ia(ifs);
54 ia >> boost::serialization::make_nvp(tag_name.c_str(), derived());
55 } else {
56 const std::string exception_message(filename +
57 " does not seem to be a valid file.");
58 throw std::invalid_argument(exception_message);
59 }
60 }
61
63 void saveAsXML(const std::string& filename,
64 const std::string& tag_name) const {
65 assert(!tag_name.empty());
66 std::ofstream ofs(filename.c_str());
67 if (ofs) {
68 boost::archive::xml_oarchive oa(ofs);
69 oa << boost::serialization::make_nvp(tag_name.c_str(), derived());
70 } else {
71 const std::string exception_message(filename +
72 " does not seem to be a valid file.");
73 throw std::invalid_argument(exception_message);
74 }
75 }
76
78 void loadFromBinary(const std::string& filename) {
79 std::ifstream ifs(filename.c_str());
80 if (ifs) {
81 boost::archive::binary_iarchive ia(ifs);
82 ia >> derived();
83 } else {
84 const std::string exception_message(filename +
85 " does not seem to be a valid file.");
86 throw std::invalid_argument(exception_message);
87 }
88 }
89
91 void saveAsBinary(const std::string& filename) const {
92 std::ofstream ofs(filename.c_str());
93 if (ofs) {
94 boost::archive::binary_oarchive oa(ofs);
95 oa << derived();
96 } else {
97 const std::string exception_message(filename +
98 " does not seem to be a valid file.");
99 throw std::invalid_argument(exception_message);
100 }
101 }
102};
103
104} // namespace serialization
105
106#endif // ifndef __parametric_curves_serialization_archive_hpp__
Definition archive.hpp:14
Definition archive.hpp:16
void loadFromBinary(const std::string &filename)
Loads a Derived object from an binary file.
Definition archive.hpp:78
void saveAsBinary(const std::string &filename) const
Saved a Derived object as an binary file.
Definition archive.hpp:91
void loadFromXML(const std::string &filename, const std::string &tag_name)
Loads a Derived object from an XML file.
Definition archive.hpp:49
void saveAsText(const std::string &filename) const
Saved a Derived object as a text file.
Definition archive.hpp:36
void loadFromText(const std::string &filename)
Loads a Derived object from a text file.
Definition archive.hpp:23
void saveAsXML(const std::string &filename, const std::string &tag_name) const
Saved a Derived object as an XML file.
Definition archive.hpp:63