proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
archive.hpp
Go to the documentation of this file.
1//
2// Copyright (c) 2022 INRIA
3//
8#ifndef PROXSUITE_SERIALIZATION_ARCHIVE_HPP
9#define PROXSUITE_SERIALIZATION_ARCHIVE_HPP
10
11#include <fstream>
12#include <string>
13
14#include <cereal/cereal.hpp>
15#include <cereal/archives/binary.hpp>
16#include <cereal/archives/json.hpp>
17#include <cereal/archives/xml.hpp>
18
19namespace proxsuite {
20namespace serialization {
21
31template<typename T>
32inline void
33loadFromStringStream(T& object, std::istringstream& is)
34{
35 cereal::JSONInputArchive ia(is);
36 ia(object);
37}
38
48template<typename T>
49inline void
50saveToStringStream(const T& object, std::stringstream& ss)
51{
52 cereal::JSONOutputArchive oa(ss);
53 oa(object);
54}
55
64template<typename T>
65inline void
66loadFromString(T& object, const std::string& str)
67{
68 std::istringstream is(str);
69 loadFromStringStream(object, is);
70}
71
81template<typename T>
82inline std::string
83saveToString(const T& object)
84{
85 std::stringstream ss;
86 saveToStringStream(object, ss);
87 return ss.str();
88}
89
98template<typename T>
99inline void
100loadFromBinary(T& object, const std::string& filename)
101{
102 std::ifstream ifs(filename.c_str(), std::ios::binary);
103 if (ifs) {
104 cereal::BinaryInputArchive ia(ifs);
105 ia(object);
106 } else {
107 const std::string exception_message(filename +
108 " does not seem to be a valid file.");
109 throw std::invalid_argument(exception_message);
110 }
111}
112
121template<typename T>
122void
123saveToBinary(const T& object, const std::string& filename)
124{
125 std::ofstream ofs(filename.c_str(), std::ios::binary);
126 if (ofs) {
127 cereal::BinaryOutputArchive oa(ofs);
128 oa(object);
129 } else {
130 const std::string exception_message(filename +
131 " does not seem to be a valid file.");
132 throw std::invalid_argument(exception_message);
133 }
134}
135
144template<typename T>
145inline void
146loadFromJSON(T& object, const std::string& filename)
147{
148 std::ifstream ifs(filename.c_str());
149 if (ifs) {
150 cereal::JSONInputArchive ia(ifs);
151 ia(object);
152 } else {
153 const std::string exception_message(filename +
154 " does not seem to be a valid file.");
155 throw std::invalid_argument(exception_message);
156 }
157}
158
167template<typename T>
168void
169saveToJSON(const T& object, const std::string& filename)
170{
171 std::ofstream ofs(filename.c_str());
172 if (ofs) {
173 cereal::JSONOutputArchive oa(ofs);
174 oa(object);
175 } else {
176 const std::string exception_message(filename +
177 " does not seem to be a valid file.");
178 throw std::invalid_argument(exception_message);
179 }
180}
181
190template<typename T>
191inline void
192loadFromXML(T& object, const std::string& filename)
193{
194 std::ifstream ifs(filename.c_str());
195 if (ifs) {
196 cereal::XMLInputArchive ia(ifs);
197 ia(object);
198 } else {
199 const std::string exception_message(filename +
200 " does not seem to be a valid file.");
201 throw std::invalid_argument(exception_message);
202 }
203}
204
213template<typename T>
214void
215saveToXML(const T& object, const std::string& filename)
216{
217 std::ofstream ofs(filename.c_str());
218 if (ofs) {
219 cereal::XMLOutputArchive oa(ofs);
220 oa(object);
221 } else {
222 const std::string exception_message(filename +
223 " does not seem to be a valid file.");
224 throw std::invalid_argument(exception_message);
225 }
226}
227
228}
229}
230
231#endif /* end of include guard PROXSUITE_SERIALIZATION_ARCHIVE_HPP */
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition archive.hpp:100
void saveToXML(const T &object, const std::string &filename)
Saves an object inside a XML file.
Definition archive.hpp:215
void saveToJSON(const T &object, const std::string &filename)
Saves an object inside a JSON file.
Definition archive.hpp:169
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition archive.hpp:123
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition archive.hpp:66
void loadFromXML(T &object, const std::string &filename)
Loads an object from a XML file.
Definition archive.hpp:192
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition archive.hpp:83
void loadFromJSON(T &object, const std::string &filename)
Loads an object from a JSON file.
Definition archive.hpp:146
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition archive.hpp:33
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition archive.hpp:50