hpp-fcl  3.0.0
HPP fork of FCL -- The Flexible Collision Library
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 HPP_FCL_SERIALIZATION_ARCHIVE_H
8 #define HPP_FCL_SERIALIZATION_ARCHIVE_H
9 
10 #include "hpp/fcl/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 hpp {
52 namespace fcl {
53 namespace serialization {
54 
63 template <typename T>
64 inline void loadFromText(T& object, const std::string& filename) {
65  std::ifstream ifs(filename.c_str());
66  if (ifs) {
67  std::locale const new_loc(ifs.getloc(),
68  new boost::math::nonfinite_num_get<char>);
69  ifs.imbue(new_loc);
70  boost::archive::text_iarchive ia(ifs, boost::archive::no_codecvt);
71  ia >> object;
72  } else {
73  const std::string exception_message(filename +
74  " does not seem to be a valid file.");
75  throw std::invalid_argument(exception_message);
76  }
77 }
78 
87 template <typename T>
88 inline void saveToText(const T& object, const std::string& filename) {
89  std::ofstream ofs(filename.c_str());
90  if (ofs) {
91  boost::archive::text_oarchive oa(ofs);
92  oa & object;
93  } else {
94  const std::string exception_message(filename +
95  " does not seem to be a valid file.");
96  throw std::invalid_argument(exception_message);
97  }
98 }
99 
109 template <typename T>
110 inline void loadFromStringStream(T& object, std::istringstream& is) {
111  std::locale const new_loc(is.getloc(),
112  new boost::math::nonfinite_num_get<char>);
113  is.imbue(new_loc);
114  boost::archive::text_iarchive ia(is, boost::archive::no_codecvt);
115  ia >> object;
116 }
117 
127 template <typename T>
128 inline void saveToStringStream(const T& object, std::stringstream& ss) {
129  boost::archive::text_oarchive oa(ss);
130  oa & object;
131 }
132 
141 template <typename T>
142 inline void loadFromString(T& object, const std::string& str) {
143  std::istringstream is(str);
144  loadFromStringStream(object, is);
145 }
146 
156 template <typename T>
157 inline std::string saveToString(const T& object) {
158  std::stringstream ss;
159  saveToStringStream(object, ss);
160  return ss.str();
161 }
162 
172 template <typename T>
173 inline void loadFromXML(T& object, const std::string& filename,
174  const std::string& tag_name) {
175  if (filename.empty()) {
176  HPP_FCL_THROW_PRETTY("Tag name should not be empty.",
177  std::invalid_argument);
178  }
179 
180  std::ifstream ifs(filename.c_str());
181  if (ifs) {
182  std::locale const new_loc(ifs.getloc(),
183  new boost::math::nonfinite_num_get<char>);
184  ifs.imbue(new_loc);
185  boost::archive::xml_iarchive ia(ifs, boost::archive::no_codecvt);
186  ia >> boost::serialization::make_nvp(tag_name.c_str(), object);
187  } else {
188  const std::string exception_message(filename +
189  " does not seem to be a valid file.");
190  throw std::invalid_argument(exception_message);
191  }
192 }
193 
203 template <typename T>
204 inline void saveToXML(const T& object, const std::string& filename,
205  const std::string& tag_name) {
206  if (filename.empty()) {
207  HPP_FCL_THROW_PRETTY("Tag name should not be empty.",
208  std::invalid_argument);
209  }
210 
211  std::ofstream ofs(filename.c_str());
212  if (ofs) {
213  boost::archive::xml_oarchive oa(ofs);
214  oa& boost::serialization::make_nvp(tag_name.c_str(), object);
215  } else {
216  const std::string exception_message(filename +
217  " does not seem to be a valid file.");
218  throw std::invalid_argument(exception_message);
219  }
220 }
221 
230 template <typename T>
231 inline void loadFromBinary(T& object, const std::string& filename) {
232  std::ifstream ifs(filename.c_str(), std::ios::binary);
233  if (ifs) {
234  boost::archive::binary_iarchive ia(ifs);
235  ia >> object;
236  } else {
237  const std::string exception_message(filename +
238  " does not seem to be a valid file.");
239  throw std::invalid_argument(exception_message);
240  }
241 }
242 
251 template <typename T>
252 void saveToBinary(const T& object, const std::string& filename) {
253  std::ofstream ofs(filename.c_str(), std::ios::binary);
254  if (ofs) {
255  boost::archive::binary_oarchive oa(ofs);
256  oa & object;
257  } else {
258  const std::string exception_message(filename +
259  " does not seem to be a valid file.");
260  throw std::invalid_argument(exception_message);
261  }
262 }
263 
272 template <typename T>
273 inline void loadFromBuffer(T& object, boost::asio::streambuf& buffer) {
274  boost::archive::binary_iarchive ia(buffer);
275  ia >> object;
276 }
277 
286 template <typename T>
287 void saveToBuffer(const T& object, boost::asio::streambuf& buffer) {
288  boost::archive::binary_oarchive oa(buffer);
289  oa & object;
290 }
291 
292 } // namespace serialization
293 } // namespace fcl
294 } // namespace hpp
295 
296 #endif // ifndef HPP_FCL_SERIALIZATION_ARCHIVE_H
#define HPP_FCL_THROW_PRETTY(message, exception)
Definition: fwd.hh:64
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition: archive.h:157
void saveToXML(const T &object, const std::string &filename, const std::string &tag_name)
Saves an object inside a XML file.
Definition: archive.h:204
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition: archive.h:252
void loadFromText(T &object, const std::string &filename)
Loads an object from a TXT file.
Definition: archive.h:64
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition: archive.h:231
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition: archive.h:128
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition: archive.h:142
void loadFromXML(T &object, const std::string &filename, const std::string &tag_name)
Loads an object from a XML file.
Definition: archive.h:173
void loadFromBuffer(T &object, boost::asio::streambuf &buffer)
Loads an object from a binary buffer.
Definition: archive.h:273
void saveToText(const T &object, const std::string &filename)
Saves an object inside a TXT file.
Definition: archive.h:88
void saveToBuffer(const T &object, boost::asio::streambuf &buffer)
Saves an object to a binary buffer.
Definition: archive.h:287
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition: archive.h:110
Main namespace.
Definition: broadphase_bruteforce.h:44