coal/serialization/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 
51 namespace coal {
52 namespace serialization {
53 
62 template <typename T>
63 inline 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 
86 template <typename T>
87 inline 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 
108 template <typename T>
109 inline 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 
126 template <typename T>
127 inline void saveToStringStream(const T& object, std::stringstream& ss) {
128  boost::archive::text_oarchive oa(ss);
129  oa & object;
130 }
131 
140 template <typename T>
141 inline void loadFromString(T& object, const std::string& str) {
142  std::istringstream is(str);
143  loadFromStringStream(object, is);
144 }
145 
155 template <typename T>
156 inline std::string saveToString(const T& object) {
157  std::stringstream ss;
158  saveToStringStream(object, ss);
159  return ss.str();
160 }
161 
171 template <typename T>
172 inline 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 
201 template <typename T>
202 inline 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 
227 template <typename T>
228 inline 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 
248 template <typename T>
249 void 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 
269 template <typename T>
270 inline void loadFromBuffer(T& object, boost::asio::streambuf& buffer) {
271  boost::archive::binary_iarchive ia(buffer);
272  ia >> object;
273 }
274 
283 template <typename T>
284 void 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
coal::serialization::loadFromBinary
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition: coal/serialization/archive.h:228
coal::serialization::loadFromString
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition: coal/serialization/archive.h:141
coal
Main namespace.
Definition: coal/broadphase/broadphase_bruteforce.h:44
coal::serialization::loadFromText
void loadFromText(T &object, const std::string &filename)
Loads an object from a TXT file.
Definition: coal/serialization/archive.h:63
coal::serialization::saveToXML
void saveToXML(const T &object, const std::string &filename, const std::string &tag_name)
Saves an object inside a XML file.
Definition: coal/serialization/archive.h:202
collision-bench.filename
filename
Definition: collision-bench.py:6
fwd.hh
COAL_THROW_PRETTY
#define COAL_THROW_PRETTY(message, exception)
Definition: include/coal/fwd.hh:64
coal::serialization::saveToText
void saveToText(const T &object, const std::string &filename)
Saves an object inside a TXT file.
Definition: coal/serialization/archive.h:87
coal::serialization::saveToStringStream
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition: coal/serialization/archive.h:127
coal::serialization::saveToBinary
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition: coal/serialization/archive.h:249
coal::serialization::loadFromStringStream
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition: coal/serialization/archive.h:109
coal::serialization::saveToString
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition: coal/serialization/archive.h:156
str
const char * str()
Definition: doxygen_xml_parser.py:885
coal::serialization::loadFromXML
void loadFromXML(T &object, const std::string &filename, const std::string &tag_name)
Loads an object from a XML file.
Definition: coal/serialization/archive.h:172
coal::serialization::saveToBuffer
void saveToBuffer(const T &object, boost::asio::streambuf &buffer)
Saves an object to a binary buffer.
Definition: coal/serialization/archive.h:284
coal::serialization::loadFromBuffer
void loadFromBuffer(T &object, boost::asio::streambuf &buffer)
Loads an object from a binary buffer.
Definition: coal/serialization/archive.h:270


hpp-fcl
Author(s):
autogenerated on Sat Nov 23 2024 03:44:57