archive.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017-2021 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 #include <boost/asio/streambuf.hpp>
23 #include <boost/iostreams/device/array.hpp>
24 #include <boost/iostreams/stream.hpp>
25 #include <boost/iostreams/stream_buffer.hpp>
26 
27 // Handle NAN inside TXT or XML archives
28 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
29 
30 namespace pinocchio
31 {
32  namespace serialization
33  {
34 
43  template<typename T>
44  inline void loadFromText(T & object,
45  const std::string & filename)
46  {
47  std::ifstream ifs(filename.c_str());
48  if(ifs)
49  {
50  std::locale const new_loc(ifs.getloc(), new boost::math::nonfinite_num_get<char>);
51  ifs.imbue(new_loc);
52  boost::archive::text_iarchive ia(ifs,boost::archive::no_codecvt);
53  ia >> object;
54  }
55  else
56  {
57  const std::string exception_message(filename + " does not seem to be a valid file.");
58  throw std::invalid_argument(exception_message);
59  }
60  }
61 
70  template<typename T>
71  inline void saveToText(const T & object,
72  const std::string & filename)
73  {
74  std::ofstream ofs(filename.c_str());
75  if(ofs)
76  {
77  boost::archive::text_oarchive oa(ofs);
78  oa & object;
79  }
80  else
81  {
82  const std::string exception_message(filename + " does not seem to be a valid file.");
83  throw std::invalid_argument(exception_message);
84  }
85  }
86 
95  template<typename T>
96  inline void loadFromStringStream(T & object,
97  std::istringstream & is)
98  {
99  boost::archive::text_iarchive ia(is,boost::archive::no_codecvt);
100  ia >> object;
101  }
102 
111  template<typename T>
112  inline void saveToStringStream(const T & object,
113  std::stringstream & ss)
114  {
115  boost::archive::text_oarchive oa(ss);
116  oa & object;
117  }
118 
127  template<typename T>
128  inline void loadFromString(T & object,
129  const std::string & str)
130  {
131  std::istringstream is(str);
132  loadFromStringStream(object,is);
133  }
134 
144  template<typename T>
145  inline std::string saveToString(const T & object)
146  {
147  std::stringstream ss;
148  saveToStringStream(object,ss);
149  return ss.str();
150  }
151 
161  template<typename T>
162  inline void loadFromXML(T & object,
163  const std::string & filename,
164  const std::string & tag_name)
165  {
166  PINOCCHIO_CHECK_INPUT_ARGUMENT(!tag_name.empty());
167 
168  std::ifstream ifs(filename.c_str());
169  if(ifs)
170  {
171  std::locale const new_loc(ifs.getloc(), new boost::math::nonfinite_num_get<char>);
172  ifs.imbue(new_loc);
173  boost::archive::xml_iarchive ia(ifs,boost::archive::no_codecvt);
174  ia >> boost::serialization::make_nvp(tag_name.c_str(),object);
175  }
176  else
177  {
178  const std::string exception_message(filename + " does not seem to be a valid file.");
179  throw std::invalid_argument(exception_message);
180  }
181  }
182 
192  template<typename T>
193  inline void saveToXML(const T & object,
194  const std::string & filename,
195  const std::string & tag_name)
196  {
197  PINOCCHIO_CHECK_INPUT_ARGUMENT(!tag_name.empty());
198 
199  std::ofstream ofs(filename.c_str());
200  if(ofs)
201  {
202  boost::archive::xml_oarchive oa(ofs);
203  oa & boost::serialization::make_nvp(tag_name.c_str(),object);
204  }
205  else
206  {
207  const std::string exception_message(filename + " does not seem to be a valid file.");
208  throw std::invalid_argument(exception_message);
209  }
210  }
211 
220  template<typename T>
221  inline void loadFromBinary(T & object,
222  const std::string & filename)
223  {
224  std::ifstream ifs(filename.c_str(), std::ios::binary);
225  if(ifs)
226  {
227  boost::archive::binary_iarchive ia(ifs);
228  ia >> object;
229  }
230  else
231  {
232  const std::string exception_message(filename + " does not seem to be a valid file.");
233  throw std::invalid_argument(exception_message);
234  }
235  }
236 
245  template<typename T>
246  void saveToBinary(const T & object,
247  const std::string & filename)
248  {
249  std::ofstream ofs(filename.c_str(), std::ios::binary);
250  if(ofs)
251  {
252  boost::archive::binary_oarchive oa(ofs);
253  oa & object;
254  }
255  else
256  {
257  const std::string exception_message(filename + " does not seem to be a valid file.");
258  throw std::invalid_argument(exception_message);
259  }
260  }
261 
270  template<typename T>
271  inline void loadFromBinary(T & object,
272  boost::asio::streambuf & buffer)
273  {
274  boost::archive::binary_iarchive ia(buffer);
275  ia >> object;
276  }
277 
286  template<typename T>
287  void saveToBinary(const T & object,
288  boost::asio::streambuf & buffer)
289  {
290  boost::archive::binary_oarchive oa(buffer);
291  oa & object;
292  }
293 
303  template<typename T>
304  inline void loadFromBinary(T & object,
305  StaticBuffer & buffer)
306  {
307  boost::iostreams::stream_buffer< boost::iostreams::basic_array<char> > stream(buffer.data(), buffer.size());
308 
309  boost::archive::binary_iarchive ia(stream);
310  ia >> object;
311  }
312 
322  template<typename T>
323  inline void saveToBinary(const T & object,
324  StaticBuffer & buffer)
325  {
326  boost::iostreams::stream_buffer< boost::iostreams::basic_array<char> > stream(buffer.data(), buffer.size());
327 
328  boost::archive::binary_oarchive oa(stream);
329  oa & object;
330  }
331 
332  } // namespace serialization
333 } // namespace pinocchio
334 
335 #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:246
void loadFromXML(T &object, const std::string &filename, const std::string &tag_name)
Loads an object from a XML file.
Definition: archive.hpp:162
void saveToStringStream(const T &object, std::stringstream &ss)
Saves an object inside a std::stringstream.
Definition: archive.hpp:112
std::string saveToString(const T &object)
Saves an object inside a std::string.
Definition: archive.hpp:145
void saveToText(const T &object, const std::string &filename)
Saves an object inside a TXT file.
Definition: archive.hpp:71
size_t size() const
Returns the current size of the buffer.
char * data()
Returns the pointer on the data.
Static buffer with pre-allocated memory.
void loadFromText(T &object, const std::string &filename)
Loads an object from a TXT file.
Definition: archive.hpp:44
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:128
void loadFromStringStream(T &object, std::istringstream &is)
Loads an object from a std::stringstream.
Definition: archive.hpp:96
Main pinocchio namespace.
Definition: timings.cpp:30
void saveToXML(const T &object, const std::string &filename, const std::string &tag_name)
Saves an object inside a XML file.
Definition: archive.hpp:193
void loadFromBinary(T &object, const std::string &filename)
Loads an object from a binary file.
Definition: archive.hpp:221
#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 Tue Jun 1 2021 02:45:02