gtsam 4.1.1
gtsam
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 <Eigen/Core>
23#include <fstream>
24#include <sstream>
25#include <string>
26
27// includes for standard serialization types
28#include <boost/serialization/optional.hpp>
29#include <boost/serialization/shared_ptr.hpp>
30#include <boost/serialization/vector.hpp>
31#include <boost/serialization/map.hpp>
32#include <boost/serialization/list.hpp>
33#include <boost/serialization/deque.hpp>
34#include <boost/serialization/weak_ptr.hpp>
35
36#include <boost/archive/text_oarchive.hpp>
37#include <boost/archive/text_iarchive.hpp>
38#include <boost/archive/xml_iarchive.hpp>
39#include <boost/archive/xml_oarchive.hpp>
40#include <boost/archive/binary_iarchive.hpp>
41#include <boost/archive/binary_oarchive.hpp>
42#include <boost/serialization/export.hpp>
43
44// Workaround a bug in GCC >= 7 and C++17
45// ref. https://gitlab.com/libeigen/eigen/-/issues/1676
46#ifdef __GNUC__
47#if __GNUC__ >= 7 && __cplusplus >= 201703L
48namespace boost { namespace serialization { struct U; } }
49namespace Eigen { namespace internal {
50template<> struct traits<boost::serialization::U> {enum {Flags=0};};
51} }
52#endif
53#endif
54
55namespace gtsam {
56
62template <class T>
63void serializeToStream(const T& input, std::ostream& out_archive_stream) {
64 boost::archive::text_oarchive out_archive(out_archive_stream);
65 out_archive << input;
66}
67
69template <class T>
70void deserializeFromStream(std::istream& in_archive_stream, T& output) {
71 boost::archive::text_iarchive in_archive(in_archive_stream);
72 in_archive >> output;
73}
74
76template <class T>
77std::string serializeToString(const T& input) {
78 std::ostringstream out_archive_stream;
79 serializeToStream(input, out_archive_stream);
80 return out_archive_stream.str();
81}
82
84template <class T>
85void deserializeFromString(const std::string& serialized, T& output) {
86 std::istringstream in_archive_stream(serialized);
87 deserializeFromStream(in_archive_stream, output);
88}
89
91template <class T>
92bool serializeToFile(const T& input, const std::string& filename) {
93 std::ofstream out_archive_stream(filename.c_str());
94 if (!out_archive_stream.is_open()) return false;
95 serializeToStream(input, out_archive_stream);
96 out_archive_stream.close();
97 return true;
98}
99
101template <class T>
102bool deserializeFromFile(const std::string& filename, T& output) {
103 std::ifstream in_archive_stream(filename.c_str());
104 if (!in_archive_stream.is_open()) return false;
105 deserializeFromStream(in_archive_stream, output);
106 in_archive_stream.close();
107 return true;
108}
109
111template <class T>
112std::string serialize(const T& input) {
113 return serializeToString(input);
114}
115
117template <class T>
118void deserialize(const std::string& serialized, T& output) {
119 deserializeFromString(serialized, output);
120}
122
128template <class T>
129void serializeToXMLStream(const T& input, std::ostream& out_archive_stream,
130 const std::string& name = "data") {
131 boost::archive::xml_oarchive out_archive(out_archive_stream);
132 out_archive << boost::serialization::make_nvp(name.c_str(), input);
133}
134
136template <class T>
137void deserializeFromXMLStream(std::istream& in_archive_stream, T& output,
138 const std::string& name = "data") {
139 boost::archive::xml_iarchive in_archive(in_archive_stream);
140 in_archive >> boost::serialization::make_nvp(name.c_str(), output);
141}
142
144template <class T>
145std::string serializeToXMLString(const T& input,
146 const std::string& name = "data") {
147 std::ostringstream out_archive_stream;
148 serializeToXMLStream(input, out_archive_stream, name);
149 return out_archive_stream.str();
150}
151
153template <class T>
154void deserializeFromXMLString(const std::string& serialized, T& output,
155 const std::string& name = "data") {
156 std::istringstream in_archive_stream(serialized);
157 deserializeFromXMLStream(in_archive_stream, output, name);
158}
159
161template <class T>
162bool serializeToXMLFile(const T& input, const std::string& filename,
163 const std::string& name = "data") {
164 std::ofstream out_archive_stream(filename.c_str());
165 if (!out_archive_stream.is_open()) return false;
166 serializeToXMLStream(input, out_archive_stream, name);
167 out_archive_stream.close();
168 return true;
169}
170
172template <class T>
173bool deserializeFromXMLFile(const std::string& filename, T& output,
174 const std::string& name = "data") {
175 std::ifstream in_archive_stream(filename.c_str());
176 if (!in_archive_stream.is_open()) return false;
177 deserializeFromXMLStream(in_archive_stream, output, name);
178 in_archive_stream.close();
179 return true;
180}
181
183template <class T>
184std::string serializeXML(const T& input,
185 const std::string& name = "data") {
186 return serializeToXMLString(input, name);
187}
188
190template <class T>
191void deserializeXML(const std::string& serialized, T& output,
192 const std::string& name = "data") {
193 deserializeFromXMLString(serialized, output, name);
194}
196
202template <class T>
203void serializeToBinaryStream(const T& input, std::ostream& out_archive_stream,
204 const std::string& name = "data") {
205 boost::archive::binary_oarchive out_archive(out_archive_stream);
206 out_archive << boost::serialization::make_nvp(name.c_str(), input);
207}
208
210template <class T>
211void deserializeFromBinaryStream(std::istream& in_archive_stream, T& output,
212 const std::string& name = "data") {
213 boost::archive::binary_iarchive in_archive(in_archive_stream);
214 in_archive >> boost::serialization::make_nvp(name.c_str(), output);
215}
216
218template <class T>
219std::string serializeToBinaryString(const T& input,
220 const std::string& name = "data") {
221 std::ostringstream out_archive_stream;
222 serializeToBinaryStream(input, out_archive_stream, name);
223 return out_archive_stream.str();
224}
225
227template <class T>
228void deserializeFromBinaryString(const std::string& serialized, T& output,
229 const std::string& name = "data") {
230 std::istringstream in_archive_stream(serialized);
231 deserializeFromBinaryStream(in_archive_stream, output, name);
232}
233
235template <class T>
236bool serializeToBinaryFile(const T& input, const std::string& filename,
237 const std::string& name = "data") {
238 std::ofstream out_archive_stream(filename.c_str());
239 if (!out_archive_stream.is_open()) return false;
240 serializeToBinaryStream(input, out_archive_stream, name);
241 out_archive_stream.close();
242 return true;
243}
244
246template <class T>
247bool deserializeFromBinaryFile(const std::string& filename, T& output,
248 const std::string& name = "data") {
249 std::ifstream in_archive_stream(filename.c_str());
250 if (!in_archive_stream.is_open()) return false;
251 deserializeFromBinaryStream(in_archive_stream, output, name);
252 in_archive_stream.close();
253 return true;
254}
255
257template <class T>
258std::string serializeBinary(const T& input,
259 const std::string& name = "data") {
260 return serializeToBinaryString(input, name);
261}
262
264template <class T>
265void deserializeBinary(const std::string& serialized, T& output,
266 const std::string& name = "data") {
267 deserializeFromBinaryString(serialized, output, name);
268}
270
271} // namespace gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
void serializeToStream(const T &input, std::ostream &out_archive_stream)
Definition: serialization.h:63
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
Definition: serialization.h:173
std::string serializeToXMLString(const T &input, const std::string &name="data")
serializes to a string in XML
Definition: serialization.h:145
void deserializeFromString(const std::string &serialized, T &output)
deserializes from a string
Definition: serialization.h:85
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:112
void deserializeFromBinaryString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition: serialization.h:228
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition: serialization.h:265
void deserializeFromXMLString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition: serialization.h:154
std::string serializeToBinaryString(const T &input, const std::string &name="data")
serializes to a string in binary
Definition: serialization.h:219
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition: serialization.h:191
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Definition: serialization.h:162
void deserializeFromXMLStream(std::istream &in_archive_stream, T &output, const std::string &name="data")
deserializes from a stream in XML
Definition: serialization.h:137
void deserializeFromBinaryStream(std::istream &in_archive_stream, T &output, const std::string &name="data")
deserializes from a stream in binary
Definition: serialization.h:211
std::string serializeXML(const T &input, const std::string &name="data")
serializes to a string in XML
Definition: serialization.h:184
std::string serializeToString(const T &input)
serializes to a string
Definition: serialization.h:77
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
Definition: serialization.h:92
void deserialize(const std::string &serialized, T &output)
deserializes from a string
Definition: serialization.h:118
void serializeToBinaryStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
Definition: serialization.h:203
void deserializeFromStream(std::istream &in_archive_stream, T &output)
deserializes from a stream
Definition: serialization.h:70
std::string serializeBinary(const T &input, const std::string &name="data")
serializes to a string in binary
Definition: serialization.h:258
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
Definition: serialization.h:236
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
Definition: serialization.h:102
void serializeToXMLStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
Definition: serialization.h:129
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
Definition: serialization.h:247