base/serialization.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
21 #pragma once
22 
23 #include <Eigen/Core>
24 #include <fstream>
25 #include <sstream>
26 #include <string>
27 
28 // includes for standard serialization types
29 #include <boost/serialization/version.hpp>
30 #include <boost/serialization/optional.hpp>
31 #include <boost/serialization/shared_ptr.hpp>
32 #include <boost/serialization/vector.hpp>
33 #include <boost/serialization/map.hpp>
34 #include <boost/serialization/list.hpp>
35 #include <boost/serialization/deque.hpp>
36 #include <boost/serialization/weak_ptr.hpp>
37 
38 #include <boost/archive/text_oarchive.hpp>
39 #include <boost/archive/text_iarchive.hpp>
40 #include <boost/archive/xml_iarchive.hpp>
41 #include <boost/archive/xml_oarchive.hpp>
42 #include <boost/archive/binary_iarchive.hpp>
43 #include <boost/archive/binary_oarchive.hpp>
44 #include <boost/serialization/export.hpp>
45 
46 // Workaround a bug in GCC >= 7 and C++17
47 // ref. https://gitlab.com/libeigen/eigen/-/issues/1676
48 #ifdef __GNUC__
49 #if __GNUC__ >= 7 && __cplusplus >= 201703L
50 namespace boost { namespace serialization { struct U; } }
51 namespace Eigen { namespace internal {
52 template<> struct traits<boost::serialization::U> {enum {Flags=0};};
53 } }
54 #endif
55 #endif
56 
57 namespace gtsam {
58 
62 template <class T>
65 void serializeToStream(const T& input, std::ostream& out_archive_stream) {
66  boost::archive::text_oarchive out_archive(out_archive_stream);
67  out_archive << input;
68 }
69 
71 template <class T>
72 void deserializeFromStream(std::istream& in_archive_stream, T& output) {
73  boost::archive::text_iarchive in_archive(in_archive_stream);
74  in_archive >> output;
75 }
76 
78 template <class T>
79 std::string serializeToString(const T& input) {
80  std::ostringstream out_archive_stream;
81  serializeToStream(input, out_archive_stream);
82  return out_archive_stream.str();
83 }
84 
86 template <class T>
87 void deserializeFromString(const std::string& serialized, T& output) {
88  std::istringstream in_archive_stream(serialized);
89  deserializeFromStream(in_archive_stream, output);
90 }
91 
93 template <class T>
94 bool serializeToFile(const T& input, const std::string& filename) {
95  std::ofstream out_archive_stream(filename.c_str());
96  if (!out_archive_stream.is_open()) return false;
97  serializeToStream(input, out_archive_stream);
98  out_archive_stream.close();
99  return true;
100 }
101 
103 template <class T>
104 bool deserializeFromFile(const std::string& filename, T& output) {
105  std::ifstream in_archive_stream(filename.c_str());
106  if (!in_archive_stream.is_open()) return false;
107  deserializeFromStream(in_archive_stream, output);
108  in_archive_stream.close();
109  return true;
110 }
111 
113 template <class T>
114 std::string serialize(const T& input) {
115  return serializeToString(input);
116 }
117 
119 template <class T>
120 void deserialize(const std::string& serialized, T& output) {
121  deserializeFromString(serialized, output);
122 }
124 
128 template <class T>
131 void serializeToXMLStream(const T& input, std::ostream& out_archive_stream,
132  const std::string& name = "data") {
133  boost::archive::xml_oarchive out_archive(out_archive_stream);
134  out_archive << boost::serialization::make_nvp(name.c_str(), input);
135 }
136 
138 template <class T>
139 void deserializeFromXMLStream(std::istream& in_archive_stream, T& output,
140  const std::string& name = "data") {
141  boost::archive::xml_iarchive in_archive(in_archive_stream);
142  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
143 }
144 
146 template <class T>
147 std::string serializeToXMLString(const T& input,
148  const std::string& name = "data") {
149  std::ostringstream out_archive_stream;
150  serializeToXMLStream(input, out_archive_stream, name);
151  return out_archive_stream.str();
152 }
153 
155 template <class T>
156 void deserializeFromXMLString(const std::string& serialized, T& output,
157  const std::string& name = "data") {
158  std::istringstream in_archive_stream(serialized);
159  deserializeFromXMLStream(in_archive_stream, output, name);
160 }
161 
163 template <class T>
164 bool serializeToXMLFile(const T& input, const std::string& filename,
165  const std::string& name = "data") {
166  std::ofstream out_archive_stream(filename.c_str());
167  if (!out_archive_stream.is_open()) return false;
168  serializeToXMLStream(input, out_archive_stream, name);
169  out_archive_stream.close();
170  return true;
171 }
172 
174 template <class T>
175 bool deserializeFromXMLFile(const std::string& filename, T& output,
176  const std::string& name = "data") {
177  std::ifstream in_archive_stream(filename.c_str());
178  if (!in_archive_stream.is_open()) return false;
179  deserializeFromXMLStream(in_archive_stream, output, name);
180  in_archive_stream.close();
181  return true;
182 }
183 
185 template <class T>
186 std::string serializeXML(const T& input,
187  const std::string& name = "data") {
188  return serializeToXMLString(input, name);
189 }
190 
192 template <class T>
193 void deserializeXML(const std::string& serialized, T& output,
194  const std::string& name = "data") {
195  deserializeFromXMLString(serialized, output, name);
196 }
198 
202 template <class T>
205 void serializeToBinaryStream(const T& input, std::ostream& out_archive_stream,
206  const std::string& name = "data") {
207  boost::archive::binary_oarchive out_archive(out_archive_stream);
208  out_archive << boost::serialization::make_nvp(name.c_str(), input);
209 }
210 
212 template <class T>
213 void deserializeFromBinaryStream(std::istream& in_archive_stream, T& output,
214  const std::string& name = "data") {
215  boost::archive::binary_iarchive in_archive(in_archive_stream);
216  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
217 }
218 
220 template <class T>
221 std::string serializeToBinaryString(const T& input,
222  const std::string& name = "data") {
223  std::ostringstream out_archive_stream;
224  serializeToBinaryStream(input, out_archive_stream, name);
225  return out_archive_stream.str();
226 }
227 
229 template <class T>
230 void deserializeFromBinaryString(const std::string& serialized, T& output,
231  const std::string& name = "data") {
232  std::istringstream in_archive_stream(serialized);
233  deserializeFromBinaryStream(in_archive_stream, output, name);
234 }
235 
237 template <class T>
238 bool serializeToBinaryFile(const T& input, const std::string& filename,
239  const std::string& name = "data") {
240  std::ofstream out_archive_stream(filename.c_str());
241  if (!out_archive_stream.is_open()) return false;
242  serializeToBinaryStream(input, out_archive_stream, name);
243  out_archive_stream.close();
244  return true;
245 }
246 
248 template <class T>
249 bool deserializeFromBinaryFile(const std::string& filename, T& output,
250  const std::string& name = "data") {
251  std::ifstream in_archive_stream(filename.c_str());
252  if (!in_archive_stream.is_open()) return false;
253  deserializeFromBinaryStream(in_archive_stream, output, name);
254  in_archive_stream.close();
255  return true;
256 }
257 
259 template <class T>
260 std::string serializeBinary(const T& input,
261  const std::string& name = "data") {
262  return serializeToBinaryString(input, name);
263 }
264 
266 template <class T>
267 void deserializeBinary(const std::string& serialized, T& output,
268  const std::string& name = "data") {
269  deserializeFromBinaryString(serialized, output, name);
270 }
272 
273 } // namespace gtsam
274 #endif
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
name
Annotation for function names.
Definition: attr.h:51
boost
Definition: boostmultiprec.cpp:109
relicense.filename
filename
Definition: relicense.py:57
Eigen::Triplet
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:162
gtsam
traits
Definition: SFMdata.h:40
U
@ U
Definition: testDecisionTree.cpp:349
internal
Definition: BandTriangularSolver.h:13


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:35:15