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 #pragma once
21 
22 #include <gtsam/config.h>
23 
24 #if GTSAM_ENABLE_BOOST_SERIALIZATION
25 
26 #include <Eigen/Core>
27 #include <fstream>
28 #include <sstream>
29 #include <string>
30 
31 // includes for standard serialization types
32 #include <boost/serialization/version.hpp>
33 #include <boost/serialization/optional.hpp>
34 #include <boost/serialization/shared_ptr.hpp>
35 #include <boost/serialization/vector.hpp>
36 #include <boost/serialization/map.hpp>
37 #include <boost/serialization/list.hpp>
38 #include <boost/serialization/deque.hpp>
39 #include <boost/serialization/weak_ptr.hpp>
40 
41 #include <boost/archive/text_oarchive.hpp>
42 #include <boost/archive/text_iarchive.hpp>
43 #include <boost/archive/xml_iarchive.hpp>
44 #include <boost/archive/xml_oarchive.hpp>
45 #include <boost/archive/binary_iarchive.hpp>
46 #include <boost/archive/binary_oarchive.hpp>
47 #include <boost/serialization/export.hpp>
48 
49 // Workaround a bug in GCC >= 7 and C++17
50 // ref. https://gitlab.com/libeigen/eigen/-/issues/1676
51 #ifdef __GNUC__
52 #if __GNUC__ >= 7 && __cplusplus >= 201703L
53 namespace boost { namespace serialization { struct U; } }
54 namespace Eigen { namespace internal {
55 template<> struct traits<boost::serialization::U> {enum {Flags=0};};
56 } }
57 #endif
58 #endif
59 
60 namespace gtsam {
61 
65 template <class T>
68 void serializeToStream(const T& input, std::ostream& out_archive_stream) {
69  boost::archive::text_oarchive out_archive(out_archive_stream);
70  out_archive << input;
71 }
72 
74 template <class T>
75 void deserializeFromStream(std::istream& in_archive_stream, T& output) {
76  boost::archive::text_iarchive in_archive(in_archive_stream);
77  in_archive >> output;
78 }
79 
81 template <class T>
82 std::string serializeToString(const T& input) {
83  std::ostringstream out_archive_stream;
84  serializeToStream(input, out_archive_stream);
85  return out_archive_stream.str();
86 }
87 
89 template <class T>
90 void deserializeFromString(const std::string& serialized, T& output) {
91  std::istringstream in_archive_stream(serialized);
92  deserializeFromStream(in_archive_stream, output);
93 }
94 
96 template <class T>
97 bool serializeToFile(const T& input, const std::string& filename) {
98  std::ofstream out_archive_stream(filename.c_str());
99  if (!out_archive_stream.is_open()) return false;
100  serializeToStream(input, out_archive_stream);
101  out_archive_stream.close();
102  return true;
103 }
104 
106 template <class T>
107 bool deserializeFromFile(const std::string& filename, T& output) {
108  std::ifstream in_archive_stream(filename.c_str());
109  if (!in_archive_stream.is_open()) return false;
110  deserializeFromStream(in_archive_stream, output);
111  in_archive_stream.close();
112  return true;
113 }
114 
116 template <class T>
117 std::string serialize(const T& input) {
118  return serializeToString(input);
119 }
120 
122 template <class T>
123 void deserialize(const std::string& serialized, T& output) {
124  deserializeFromString(serialized, output);
125 }
127 
131 template <class T>
134 void serializeToXMLStream(const T& input, std::ostream& out_archive_stream,
135  const std::string& name = "data") {
136  boost::archive::xml_oarchive out_archive(out_archive_stream);
137  out_archive << boost::serialization::make_nvp(name.c_str(), input);
138 }
139 
141 template <class T>
142 void deserializeFromXMLStream(std::istream& in_archive_stream, T& output,
143  const std::string& name = "data") {
144  boost::archive::xml_iarchive in_archive(in_archive_stream);
145  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
146 }
147 
149 template <class T>
150 std::string serializeToXMLString(const T& input,
151  const std::string& name = "data") {
152  std::ostringstream out_archive_stream;
153  serializeToXMLStream(input, out_archive_stream, name);
154  return out_archive_stream.str();
155 }
156 
158 template <class T>
159 void deserializeFromXMLString(const std::string& serialized, T& output,
160  const std::string& name = "data") {
161  std::istringstream in_archive_stream(serialized);
162  deserializeFromXMLStream(in_archive_stream, output, name);
163 }
164 
166 template <class T>
167 bool serializeToXMLFile(const T& input, const std::string& filename,
168  const std::string& name = "data") {
169  std::ofstream out_archive_stream(filename.c_str());
170  if (!out_archive_stream.is_open()) return false;
171  serializeToXMLStream(input, out_archive_stream, name);
172  out_archive_stream.close();
173  return true;
174 }
175 
177 template <class T>
178 bool deserializeFromXMLFile(const std::string& filename, T& output,
179  const std::string& name = "data") {
180  std::ifstream in_archive_stream(filename.c_str());
181  if (!in_archive_stream.is_open()) return false;
182  deserializeFromXMLStream(in_archive_stream, output, name);
183  in_archive_stream.close();
184  return true;
185 }
186 
188 template <class T>
189 std::string serializeXML(const T& input,
190  const std::string& name = "data") {
191  return serializeToXMLString(input, name);
192 }
193 
195 template <class T>
196 void deserializeXML(const std::string& serialized, T& output,
197  const std::string& name = "data") {
198  deserializeFromXMLString(serialized, output, name);
199 }
201 
205 template <class T>
208 void serializeToBinaryStream(const T& input, std::ostream& out_archive_stream,
209  const std::string& name = "data") {
210  boost::archive::binary_oarchive out_archive(out_archive_stream);
211  out_archive << boost::serialization::make_nvp(name.c_str(), input);
212 }
213 
215 template <class T>
216 void deserializeFromBinaryStream(std::istream& in_archive_stream, T& output,
217  const std::string& name = "data") {
218  boost::archive::binary_iarchive in_archive(in_archive_stream);
219  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
220 }
221 
223 template <class T>
224 std::string serializeToBinaryString(const T& input,
225  const std::string& name = "data") {
226  std::ostringstream out_archive_stream;
227  serializeToBinaryStream(input, out_archive_stream, name);
228  return out_archive_stream.str();
229 }
230 
232 template <class T>
233 void deserializeFromBinaryString(const std::string& serialized, T& output,
234  const std::string& name = "data") {
235  std::istringstream in_archive_stream(serialized);
236  deserializeFromBinaryStream(in_archive_stream, output, name);
237 }
238 
240 template <class T>
241 bool serializeToBinaryFile(const T& input, const std::string& filename,
242  const std::string& name = "data") {
243  std::ofstream out_archive_stream(filename.c_str());
244  if (!out_archive_stream.is_open()) return false;
245  serializeToBinaryStream(input, out_archive_stream, name);
246  out_archive_stream.close();
247  return true;
248 }
249 
251 template <class T>
252 bool deserializeFromBinaryFile(const std::string& filename, T& output,
253  const std::string& name = "data") {
254  std::ifstream in_archive_stream(filename.c_str());
255  if (!in_archive_stream.is_open()) return false;
256  deserializeFromBinaryStream(in_archive_stream, output, name);
257  in_archive_stream.close();
258  return true;
259 }
260 
262 template <class T>
263 std::string serializeBinary(const T& input,
264  const std::string& name = "data") {
265  return serializeToBinaryString(input, name);
266 }
267 
269 template <class T>
270 void deserializeBinary(const std::string& serialized, T& output,
271  const std::string& name = "data") {
272  deserializeFromBinaryString(serialized, output, name);
273 }
275 
276 } // namespace gtsam
277 #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:342
internal
Definition: BandTriangularSolver.h:13


gtsam
Author(s):
autogenerated on Tue Jan 7 2025 04:04:05