proxsuite
0.7.3
The Advanced Proximal Optimization Toolbox
Toggle main menu visibility
Loading...
Searching...
No Matches
archive.hpp
Go to the documentation of this file.
1
//
2
// Copyright (c) 2022 INRIA
3
//
7
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
19
namespace
proxsuite
{
20
namespace
serialization
{
21
31
template
<
typename
T>
32
inline
void
33
loadFromStringStream
(T&
object
, std::istringstream& is)
34
{
35
cereal::JSONInputArchive ia(is);
36
ia(
object
);
37
}
38
48
template
<
typename
T>
49
inline
void
50
saveToStringStream
(
const
T&
object
, std::stringstream& ss)
51
{
52
cereal::JSONOutputArchive oa(ss);
53
oa(
object
);
54
}
55
64
template
<
typename
T>
65
inline
void
66
loadFromString
(T&
object
,
const
std::string& str)
67
{
68
std::istringstream is(str);
69
loadFromStringStream
(
object
, is);
70
}
71
81
template
<
typename
T>
82
inline
std::string
83
saveToString
(
const
T&
object
)
84
{
85
std::stringstream ss;
86
saveToStringStream
(
object
, ss);
87
return
ss.str();
88
}
89
98
template
<
typename
T>
99
inline
void
100
loadFromBinary
(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
121
template
<
typename
T>
122
void
123
saveToBinary
(
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
144
template
<
typename
T>
145
inline
void
146
loadFromJSON
(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
167
template
<
typename
T>
168
void
169
saveToJSON
(
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
190
template
<
typename
T>
191
inline
void
192
loadFromXML
(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
213
template
<
typename
T>
214
void
215
saveToXML
(
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 */
proxsuite::serialization
Definition
archive.hpp:20
proxsuite::serialization::loadFromBinary
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition
archive.hpp:100
proxsuite::serialization::saveToXML
void saveToXML(const T &object, const std::string &filename)
Saves an object inside a XML file.
Definition
archive.hpp:215
proxsuite::serialization::saveToJSON
void saveToJSON(const T &object, const std::string &filename)
Saves an object inside a JSON file.
Definition
archive.hpp:169
proxsuite::serialization::saveToBinary
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition
archive.hpp:123
proxsuite::serialization::loadFromString
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition
archive.hpp:66
proxsuite::serialization::loadFromXML
void loadFromXML(T &object, const std::string &filename)
Loads an object from a XML file.
Definition
archive.hpp:192
proxsuite::serialization::saveToString
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition
archive.hpp:83
proxsuite::serialization::loadFromJSON
void loadFromJSON(T &object, const std::string &filename)
Loads an object from a JSON file.
Definition
archive.hpp:146
proxsuite::serialization::loadFromStringStream
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition
archive.hpp:33
proxsuite::serialization::saveToStringStream
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition
archive.hpp:50
proxsuite
Definition
common.hpp:14
include
proxsuite
serialization
archive.hpp
Generated by
1.17.0