coal 3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
archive.h
Go to the documentation of this file.
1//
2// Copyright (c) 2017-2024 CNRS INRIA
3// This file was borrowed from the Pinocchio library:
4// https://github.com/stack-of-tasks/pinocchio
5//
6
7#ifndef COAL_SERIALIZATION_ARCHIVE_H
8#define COAL_SERIALIZATION_ARCHIVE_H
9
10#include "coal/fwd.hh"
11
12#include <boost/serialization/nvp.hpp>
13#include <boost/archive/text_iarchive.hpp>
14#include <boost/archive/text_oarchive.hpp>
15#include <boost/archive/xml_iarchive.hpp>
16#include <boost/archive/xml_oarchive.hpp>
17#include <boost/archive/binary_iarchive.hpp>
18#include <boost/archive/binary_oarchive.hpp>
19
20#include <fstream>
21#include <string>
22#include <sstream>
23#include <stdexcept>
24
25#if BOOST_VERSION / 100 % 1000 == 78 && __APPLE__
26// See https://github.com/qcscine/utilities/issues/5#issuecomment-1246897049 for
27// further details
28
29#ifndef BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
30#define DEFINE_BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
31#define BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
32#endif
33
34#include <boost/asio/streambuf.hpp>
35
36#ifdef DEFINE_BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
37#undef BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
38#endif
39
40#else
41#include <boost/asio/streambuf.hpp>
42#endif
43
44#include <boost/iostreams/device/array.hpp>
45#include <boost/iostreams/stream.hpp>
46#include <boost/iostreams/stream_buffer.hpp>
47
48// Handle NAN inside TXT or XML archives
49#include <boost/math/special_functions/nonfinite_num_facets.hpp>
50
51namespace coal {
52namespace serialization {
53
62template <typename T>
63inline void loadFromText(T& object, const std::string& filename) {
64 std::ifstream ifs(filename.c_str());
65 if (ifs) {
66 std::locale const new_loc(ifs.getloc(),
67 new boost::math::nonfinite_num_get<char>);
68 ifs.imbue(new_loc);
69 boost::archive::text_iarchive ia(ifs, boost::archive::no_codecvt);
70 ia >> object;
71 } else {
72 const std::string exception_message(filename +
73 " does not seem to be a valid file.");
74 throw std::invalid_argument(exception_message);
75 }
76}
77
86template <typename T>
87inline void saveToText(const T& object, const std::string& filename) {
88 std::ofstream ofs(filename.c_str());
89 if (ofs) {
90 boost::archive::text_oarchive oa(ofs);
91 oa & object;
92 } else {
93 const std::string exception_message(filename +
94 " does not seem to be a valid file.");
95 throw std::invalid_argument(exception_message);
96 }
97}
98
108template <typename T>
109inline void loadFromStringStream(T& object, std::istringstream& is) {
110 std::locale const new_loc(is.getloc(),
111 new boost::math::nonfinite_num_get<char>);
112 is.imbue(new_loc);
113 boost::archive::text_iarchive ia(is, boost::archive::no_codecvt);
114 ia >> object;
115}
116
126template <typename T>
127inline void saveToStringStream(const T& object, std::stringstream& ss) {
128 boost::archive::text_oarchive oa(ss);
129 oa & object;
130}
131
140template <typename T>
141inline void loadFromString(T& object, const std::string& str) {
142 std::istringstream is(str);
143 loadFromStringStream(object, is);
144}
145
155template <typename T>
156inline std::string saveToString(const T& object) {
157 std::stringstream ss;
158 saveToStringStream(object, ss);
159 return ss.str();
160}
161
171template <typename T>
172inline void loadFromXML(T& object, const std::string& filename,
173 const std::string& tag_name) {
174 if (filename.empty()) {
175 COAL_THROW_PRETTY("Tag name should not be empty.", std::invalid_argument);
176 }
177
178 std::ifstream ifs(filename.c_str());
179 if (ifs) {
180 std::locale const new_loc(ifs.getloc(),
181 new boost::math::nonfinite_num_get<char>);
182 ifs.imbue(new_loc);
183 boost::archive::xml_iarchive ia(ifs, boost::archive::no_codecvt);
184 ia >> boost::serialization::make_nvp(tag_name.c_str(), object);
185 } else {
186 const std::string exception_message(filename +
187 " does not seem to be a valid file.");
188 throw std::invalid_argument(exception_message);
189 }
190}
191
201template <typename T>
202inline void saveToXML(const T& object, const std::string& filename,
203 const std::string& tag_name) {
204 if (filename.empty()) {
205 COAL_THROW_PRETTY("Tag name should not be empty.", std::invalid_argument);
206 }
207
208 std::ofstream ofs(filename.c_str());
209 if (ofs) {
210 boost::archive::xml_oarchive oa(ofs);
211 oa& boost::serialization::make_nvp(tag_name.c_str(), object);
212 } else {
213 const std::string exception_message(filename +
214 " does not seem to be a valid file.");
215 throw std::invalid_argument(exception_message);
216 }
217}
218
227template <typename T>
228inline void loadFromBinary(T& object, const std::string& filename) {
229 std::ifstream ifs(filename.c_str(), std::ios::binary);
230 if (ifs) {
231 boost::archive::binary_iarchive ia(ifs);
232 ia >> object;
233 } else {
234 const std::string exception_message(filename +
235 " does not seem to be a valid file.");
236 throw std::invalid_argument(exception_message);
237 }
238}
239
248template <typename T>
249void saveToBinary(const T& object, const std::string& filename) {
250 std::ofstream ofs(filename.c_str(), std::ios::binary);
251 if (ofs) {
252 boost::archive::binary_oarchive oa(ofs);
253 oa & object;
254 } else {
255 const std::string exception_message(filename +
256 " does not seem to be a valid file.");
257 throw std::invalid_argument(exception_message);
258 }
259}
260
269template <typename T>
270inline void loadFromBuffer(T& object, boost::asio::streambuf& buffer) {
271 boost::archive::binary_iarchive ia(buffer);
272 ia >> object;
273}
274
283template <typename T>
284void saveToBuffer(const T& object, boost::asio::streambuf& buffer) {
285 boost::archive::binary_oarchive oa(buffer);
286 oa & object;
287}
288
289} // namespace serialization
290} // namespace coal
291
292#endif // ifndef COAL_SERIALIZATION_ARCHIVE_H
#define COAL_THROW_PRETTY(message, exception)
Definition fwd.hh:64
void loadFromBuffer(T &object, boost::asio::streambuf &buffer)
Loads an object from a binary buffer.
Definition archive.h:270
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition archive.h:228
void loadFromText(T &object, const std::string &filename)
Loads an object from a TXT file.
Definition archive.h:63
void loadFromXML(T &object, const std::string &filename, const std::string &tag_name)
Loads an object from a XML file.
Definition archive.h:172
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition archive.h:141
void saveToXML(const T &object, const std::string &filename, const std::string &tag_name)
Saves an object inside a XML file.
Definition archive.h:202
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition archive.h:109
void saveToText(const T &object, const std::string &filename)
Saves an object inside a TXT file.
Definition archive.h:87
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition archive.h:127
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition archive.h:249
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition archive.h:156
void saveToBuffer(const T &object, boost::asio::streambuf &buffer)
Saves an object to a binary buffer.
Definition archive.h:284
Main namespace.
Definition broadphase_bruteforce.h:44