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>
23 #define SINGLE_ARG(...) \
27 #define DEFINE_CLASS_TEMPLATE_VERSION(Template, Type) \
29 namespace serialization { \
31 struct version<Type> { \
32 static constexpr unsigned int value = CURVES_API_VERSION; \
35 constexpr unsigned int version<Type>::value; \
40 namespace serialization {
43 template <
class Derived>
45 return *
static_cast<Derived*
>(
this);
47 template <
class Derived>
48 const Derived& derived()
const {
49 return *
static_cast<const Derived*
>(
this);
54 template <
class Derived>
56 std::ifstream ifs(filename.c_str());
58 boost::archive::text_iarchive ia(ifs);
59 ia >> derived<Derived>();
61 const std::string exception_message(filename +
62 " does not seem to be a valid file.");
63 throw std::invalid_argument(exception_message);
68 template <
class Derived>
70 std::ofstream ofs(filename.c_str());
72 boost::archive::text_oarchive oa(ofs);
73 oa << derived<Derived>();
75 const std::string exception_message(filename +
76 " does not seem to be a valid file.");
77 throw std::invalid_argument(exception_message);
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.");
87 std::ifstream ifs(filename.c_str());
89 boost::archive::xml_iarchive ia(ifs);
91 boost::serialization::make_nvp(tag_name.c_str(), derived<Derived>());
93 const std::string exception_message(filename +
94 " does not seem to be a valid file.");
95 throw std::invalid_argument(exception_message);
100 template <
class Derived>
102 const std::string& tag_name)
const {
103 if (tag_name.empty()) {
104 throw std::invalid_argument(
"tag_name cannot be empty.");
106 std::ofstream ofs(filename.c_str());
108 boost::archive::xml_oarchive oa(ofs);
109 oa << boost::serialization::make_nvp(tag_name.c_str(),
112 const std::string exception_message(filename +
113 " does not seem to be a valid file.");
114 throw std::invalid_argument(exception_message);
119 template <
class Derived>
121 std::ifstream ifs(filename.c_str());
123 boost::archive::binary_iarchive ia(ifs);
124 ia >> derived<Derived>();
126 const std::string exception_message(filename +
127 " does not seem to be a valid file.");
128 throw std::invalid_argument(exception_message);
133 template <
class Derived>
135 std::ofstream ofs(filename.c_str());
137 boost::archive::binary_oarchive oa(ofs);
138 oa << derived<Derived>();
140 const std::string exception_message(filename +
141 " does not seem to be a valid file.");
142 throw std::invalid_argument(exception_message);
const unsigned int CURVES_API_VERSION
Definition: archive.hpp:21
Definition: bernstein.h:20
Definition: archive.hpp:41
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 saveAsText(const std::string &filename) const
Saved a Derived object as a text file.
Definition: archive.hpp:69
void loadFromBinary(const std::string &filename)
Loads a Derived object from an binary file.
Definition: archive.hpp:120
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 loadFromText(const std::string &filename)
Loads a Derived object from a text file.
Definition: archive.hpp:55