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 
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, const std::string & filename)
62  {
63  std::ifstream ifs(filename.c_str());
64  if (ifs)
65  {
66  std::locale const new_loc(ifs.getloc(), new boost::math::nonfinite_num_get<char>);
67  ifs.imbue(new_loc);
68  boost::archive::text_iarchive ia(ifs, boost::archive::no_codecvt);
69  ia >> object;
70  }
71  else
72  {
73  const std::string exception_message(filename + " 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  {
89  std::ofstream ofs(filename.c_str());
90  if (ofs)
91  {
92  boost::archive::text_oarchive oa(ofs);
93  oa & object;
94  }
95  else
96  {
97  const std::string exception_message(filename + " does not seem to be a valid file.");
98  throw std::invalid_argument(exception_message);
99  }
100  }
101 
110  template<typename T>
111  inline void loadFromStringStream(T & object, std::istringstream & is)
112  {
113  boost::archive::text_iarchive ia(is, boost::archive::no_codecvt);
114  ia >> object;
115  }
116 
125  template<typename T>
126  inline void saveToStringStream(const T & object, std::stringstream & ss)
127  {
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  {
143  std::istringstream is(str);
144  loadFromStringStream(object, is);
145  }
146 
156  template<typename T>
157  inline std::string saveToString(const T & object)
158  {
159  std::stringstream ss;
160  saveToStringStream(object, ss);
161  return ss.str();
162  }
163 
173  template<typename T>
174  inline void loadFromXML(T & object, const std::string & filename, const std::string & tag_name)
175  {
176  PINOCCHIO_CHECK_INPUT_ARGUMENT(!tag_name.empty());
177 
178  std::ifstream ifs(filename.c_str());
179  if (ifs)
180  {
181  std::locale const new_loc(ifs.getloc(), 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  }
186  else
187  {
188  const std::string exception_message(filename + " does not seem to be a valid file.");
189  throw std::invalid_argument(exception_message);
190  }
191  }
192 
202  template<typename T>
203  inline void
204  saveToXML(const T & object, const std::string & filename, const std::string & tag_name)
205  {
206  PINOCCHIO_CHECK_INPUT_ARGUMENT(!tag_name.empty());
207 
208  std::ofstream ofs(filename.c_str());
209  if (ofs)
210  {
211  boost::archive::xml_oarchive oa(ofs);
212  oa & boost::serialization::make_nvp(tag_name.c_str(), object);
213  }
214  else
215  {
216  const std::string exception_message(filename + " does not seem to be a valid file.");
217  throw std::invalid_argument(exception_message);
218  }
219  }
220 
229  template<typename T>
230  inline void loadFromBinary(T & object, const std::string & filename)
231  {
232  std::ifstream ifs(filename.c_str(), std::ios::binary);
233  if (ifs)
234  {
235  boost::archive::binary_iarchive ia(ifs);
236  ia >> object;
237  }
238  else
239  {
240  const std::string exception_message(filename + " does not seem to be a valid file.");
241  throw std::invalid_argument(exception_message);
242  }
243  }
244 
253  template<typename T>
254  void saveToBinary(const T & object, const std::string & filename)
255  {
256  std::ofstream ofs(filename.c_str(), std::ios::binary);
257  if (ofs)
258  {
259  boost::archive::binary_oarchive oa(ofs);
260  oa & object;
261  }
262  else
263  {
264  const std::string exception_message(filename + " does not seem to be a valid file.");
265  throw std::invalid_argument(exception_message);
266  }
267  }
268 
277  template<typename T>
278  inline void loadFromBinary(T & object, boost::asio::streambuf & buffer)
279  {
280  boost::archive::binary_iarchive ia(buffer);
281  ia >> object;
282  }
283 
292  template<typename T>
293  void saveToBinary(const T & object, boost::asio::streambuf & buffer)
294  {
295  boost::archive::binary_oarchive oa(buffer);
296  oa & object;
297  }
298 
308  template<typename T>
309  inline void loadFromBinary(T & object, StaticBuffer & buffer)
310  {
311  boost::iostreams::stream_buffer<boost::iostreams::basic_array<char>> stream(
312  buffer.data(), buffer.size());
313 
314  boost::archive::binary_iarchive ia(stream);
315  ia >> object;
316  }
317 
327  template<typename T>
328  inline void saveToBinary(const T & object, StaticBuffer & buffer)
329  {
330  boost::iostreams::stream_buffer<boost::iostreams::basic_array<char>> stream(
331  buffer.data(), buffer.size());
332 
333  boost::archive::binary_oarchive oa(stream);
334  oa & object;
335  }
336 
337  } // namespace serialization
338 } // namespace pinocchio
339 
340 #endif // ifndef __pinocchio_serialization_archive_hpp__
simulation-contact-dynamics.T
int T
Definition: simulation-contact-dynamics.py:89
pinocchio::serialization::loadFromXML
void loadFromXML(T &object, const std::string &filename, const std::string &tag_name)
Loads an object from a XML file.
Definition: archive.hpp:174
PINOCCHIO_CHECK_INPUT_ARGUMENT
#define PINOCCHIO_CHECK_INPUT_ARGUMENT(...)
Macro to check an assert-like condition and throw a std::invalid_argument exception (with a message) ...
Definition: include/pinocchio/macros.hpp:192
pinocchio::serialization::loadFromString
void loadFromString(T &object, const std::string &str)
Loads an object from a std::string.
Definition: archive.hpp:141
pinocchio::serialization::saveToBinary
void saveToBinary(const T &object, const std::string &filename)
Saves an object inside a binary file.
Definition: archive.hpp:254
pinocchio::serialization::StaticBuffer
Static buffer with pre-allocated memory.
Definition: static-buffer.hpp:16
boost::serialization::make_nvp
const nvp< pinocchio::container::aligned_vector< T > > make_nvp(const char *name, pinocchio::container::aligned_vector< T > &t)
Definition: serialization/aligned-vector.hpp:37
pinocchio::serialization::StaticBuffer::size
size_t size() const
Returns the current size of the buffer.
Definition: static-buffer.hpp:27
pinocchio::serialization::saveToString
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition: archive.hpp:157
pinocchio::serialization::saveToText
void saveToText(const T &object, const std::string &filename)
Saves an object inside a TXT file.
Definition: archive.hpp:87
filename
filename
pinocchio::serialization::loadFromStringStream
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition: archive.hpp:111
pinocchio::serialization::saveToStringStream
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition: archive.hpp:126
pinocchio::serialization::saveToXML
void saveToXML(const T &object, const std::string &filename, const std::string &tag_name)
Saves an object inside a XML file.
Definition: archive.hpp:204
pinocchio::serialization::loadFromBinary
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition: archive.hpp:230
serialization
Definition: serialization.py:1
pinocchio::serialization::loadFromText
void loadFromText(T &object, const std::string &filename)
Loads an object from a TXT file.
Definition: archive.hpp:61
static-buffer.hpp
str
str
fwd.hpp
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
pinocchio::serialization::StaticBuffer::data
char * data()
Returns the pointer on the data.
Definition: static-buffer.hpp:33


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:06