archive.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017-2022 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_serialization_archive_hpp__
6 #define __pinocchio_serialization_archive_hpp__
7 
8 #include "pinocchio/serialization/fwd.hpp"
9 #include "pinocchio/serialization/static-buffer.hpp"
10 
11 #include <fstream>
12 #include <string>
13 #include <sstream>
14 #include <stdexcept>
15 #include <boost/archive/text_oarchive.hpp>
16 #include <boost/archive/text_iarchive.hpp>
17 #include <boost/archive/xml_iarchive.hpp>
18 #include <boost/archive/xml_oarchive.hpp>
19 #include <boost/archive/binary_iarchive.hpp>
20 #include <boost/archive/binary_oarchive.hpp>
21 
22 #if BOOST_VERSION / 100 % 1000 == 78 && __APPLE__
23 // See https://github.com/qcscine/utilities/issues/5#issuecomment-1246897049 for further details
24 
25 #ifndef BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
26 #define DEFINE_BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
27 #define BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
28 #endif
29 
30 #include <boost/asio/streambuf.hpp>
31 
32 #ifdef DEFINE_BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
33 #undef BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC
34 #endif
35 
36 #else
37 #include <boost/asio/streambuf.hpp>
38 #endif
39 
40 #include <boost/iostreams/device/array.hpp>
41 #include <boost/iostreams/stream.hpp>
42 #include <boost/iostreams/stream_buffer.hpp>
43 
44 // Handle NAN inside TXT or XML archives
45 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
46 
47 namespace pinocchio
48 {
49  namespace serialization
50  {
51 
60  template<typename T>
61  inline void loadFromText(T & object,
62  const std::string & filename)
63  {
64  std::ifstream ifs(filename.c_str());
65  if(ifs)
66  {
67  std::locale const new_loc(ifs.getloc(), 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  }
72  else
73  {
74  const std::string exception_message(filename + " 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,
89  const std::string & filename)
90  {
91  std::ofstream ofs(filename.c_str());
92  if(ofs)
93  {
94  boost::archive::text_oarchive oa(ofs);
95  oa & object;
96  }
97  else
98  {
99  const std::string exception_message(filename + " does not seem to be a valid file.");
100  throw std::invalid_argument(exception_message);
101  }
102  }
103 
112  template<typename T>
113  inline void loadFromStringStream(T & object,
114  std::istringstream & is)
115  {
116  boost::archive::text_iarchive ia(is,boost::archive::no_codecvt);
117  ia >> object;
118  }
119 
128  template<typename T>
129  inline void saveToStringStream(const T & object,
130  std::stringstream & ss)
131  {
132  boost::archive::text_oarchive oa(ss);
133  oa & object;
134  }
135 
144  template<typename T>
145  inline void loadFromString(T & object,
146  const std::string & str)
147  {
148  std::istringstream is(str);
149  loadFromStringStream(object,is);
150  }
151 
161  template<typename T>
162  inline std::string saveToString(const T & object)
163  {
164  std::stringstream ss;
165  saveToStringStream(object,ss);
166  return ss.str();
167  }
168 
178  template<typename T>
179  inline void loadFromXML(T & object,
180  const std::string & filename,
181  const std::string & tag_name)
182  {
183  PINOCCHIO_CHECK_INPUT_ARGUMENT(!tag_name.empty());
184 
185  std::ifstream ifs(filename.c_str());
186  if(ifs)
187  {
188  std::locale const new_loc(ifs.getloc(), new boost::math::nonfinite_num_get<char>);
189  ifs.imbue(new_loc);
190  boost::archive::xml_iarchive ia(ifs,boost::archive::no_codecvt);
191  ia >> boost::serialization::make_nvp(tag_name.c_str(),object);
192  }
193  else
194  {
195  const std::string exception_message(filename + " does not seem to be a valid file.");
196  throw std::invalid_argument(exception_message);
197  }
198  }
199 
209  template<typename T>
210  inline void saveToXML(const T & object,
211  const std::string & filename,
212  const std::string & tag_name)
213  {
214  PINOCCHIO_CHECK_INPUT_ARGUMENT(!tag_name.empty());
215 
216  std::ofstream ofs(filename.c_str());
217  if(ofs)
218  {
219  boost::archive::xml_oarchive oa(ofs);
220  oa & boost::serialization::make_nvp(tag_name.c_str(),object);
221  }
222  else
223  {
224  const std::string exception_message(filename + " does not seem to be a valid file.");
225  throw std::invalid_argument(exception_message);
226  }
227  }
228 
237  template<typename T>
238  inline void loadFromBinary(T & object,
239  const std::string & filename)
240  {
241  std::ifstream ifs(filename.c_str(), std::ios::binary);
242  if(ifs)
243  {
244  boost::archive::binary_iarchive ia(ifs);
245  ia >> object;
246  }
247  else
248  {
249  const std::string exception_message(filename + " does not seem to be a valid file.");
250  throw std::invalid_argument(exception_message);
251  }
252  }
253 
262  template<typename T>
263  void saveToBinary(const T & object,
264  const std::string & filename)
265  {
266  std::ofstream ofs(filename.c_str(), std::ios::binary);
267  if(ofs)
268  {
269  boost::archive::binary_oarchive oa(ofs);
270  oa & object;
271  }
272  else
273  {
274  const std::string exception_message(filename + " does not seem to be a valid file.");
275  throw std::invalid_argument(exception_message);
276  }
277  }
278 
287  template<typename T>
288  inline void loadFromBinary(T & object,
289  boost::asio::streambuf & buffer)
290  {
291  boost::archive::binary_iarchive ia(buffer);
292  ia >> object;
293  }
294 
303  template<typename T>
304  void saveToBinary(const T & object,
305  boost::asio::streambuf & buffer)
306  {
307  boost::archive::binary_oarchive oa(buffer);
308  oa & object;
309  }
310 
320  template<typename T>
321  inline void loadFromBinary(T & object,
322  StaticBuffer & buffer)
323  {
324  boost::iostreams::stream_buffer< boost::iostreams::basic_array<char> > stream(buffer.data(), buffer.size());
325 
326  boost::archive::binary_iarchive ia(stream);
327  ia >> object;
328  }
329 
339  template<typename T>
340  inline void saveToBinary(const T & object,
341  StaticBuffer & buffer)
342  {
343  boost::iostreams::stream_buffer< boost::iostreams::basic_array<char> > stream(buffer.data(), buffer.size());
344 
345  boost::archive::binary_oarchive oa(stream);
346  oa & object;
347  }
348 
349  } // namespace serialization
350 } // namespace pinocchio
351 
352 #endif // ifndef __pinocchio_serialization_archive_hpp__
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition: archive.hpp:263
void loadFromXML(T &object, const std::string &filename, const std::string &tag_name)
Loads an object from a XML file.
Definition: archive.hpp:179
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition: archive.hpp:129
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition: archive.hpp:162
void saveToText(const T &object, const std::string &filename)
Saves an object inside a TXT file.
Definition: archive.hpp:88
char * data()
Returns the pointer on the data.
Static buffer with pre-allocated memory.
size_t size() const
Returns the current size of the buffer.
void loadFromText(T &object, const std::string &filename)
Loads an object from a TXT file.
Definition: archive.hpp:61
const nvp< typename pinocchio::container::aligned_vector< T >::vector_base > make_nvp(const char *name, pinocchio::container::aligned_vector< T > &t)
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition: archive.hpp:145
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition: archive.hpp:113
Main pinocchio namespace.
Definition: timings.cpp:28
void saveToXML(const T &object, const std::string &filename, const std::string &tag_name)
Saves an object inside a XML file.
Definition: archive.hpp:210
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition: archive.hpp:238
#define PINOCCHIO_CHECK_INPUT_ARGUMENT(...)
Macro to check an assert-like condition and throw a std::invalid_argument exception (with a message) ...
Definition: src/macros.hpp:127


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:28