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 <Eigen/Core>
23 #include <fstream>
24 #include <sstream>
25 #include <string>
26 
27 // includes for standard serialization types
28 #include <boost/serialization/version.hpp>
29 #include <boost/serialization/optional.hpp>
30 #include <boost/serialization/shared_ptr.hpp>
31 #include <boost/serialization/vector.hpp>
32 #include <boost/serialization/map.hpp>
33 #include <boost/serialization/list.hpp>
34 #include <boost/serialization/deque.hpp>
35 #include <boost/serialization/weak_ptr.hpp>
36 
37 #include <boost/archive/text_oarchive.hpp>
38 #include <boost/archive/text_iarchive.hpp>
39 #include <boost/archive/xml_iarchive.hpp>
40 #include <boost/archive/xml_oarchive.hpp>
41 #include <boost/archive/binary_iarchive.hpp>
42 #include <boost/archive/binary_oarchive.hpp>
43 #include <boost/serialization/export.hpp>
44 
45 // Workaround a bug in GCC >= 7 and C++17
46 // ref. https://gitlab.com/libeigen/eigen/-/issues/1676
47 #ifdef __GNUC__
48 #if __GNUC__ >= 7 && __cplusplus >= 201703L
49 namespace boost { namespace serialization { struct U; } }
50 namespace Eigen { namespace internal {
51 template<> struct traits<boost::serialization::U> {enum {Flags=0};};
52 } }
53 #endif
54 #endif
55 
56 namespace gtsam {
57 
61 template <class T>
64 void serializeToStream(const T& input, std::ostream& out_archive_stream) {
65  boost::archive::text_oarchive out_archive(out_archive_stream);
66  out_archive << input;
67 }
68 
70 template <class T>
71 void deserializeFromStream(std::istream& in_archive_stream, T& output) {
72  boost::archive::text_iarchive in_archive(in_archive_stream);
73  in_archive >> output;
74 }
75 
77 template <class T>
78 std::string serializeToString(const T& input) {
79  std::ostringstream out_archive_stream;
80  serializeToStream(input, out_archive_stream);
81  return out_archive_stream.str();
82 }
83 
85 template <class T>
86 void deserializeFromString(const std::string& serialized, T& output) {
87  std::istringstream in_archive_stream(serialized);
88  deserializeFromStream(in_archive_stream, output);
89 }
90 
92 template <class T>
93 bool serializeToFile(const T& input, const std::string& filename) {
94  std::ofstream out_archive_stream(filename.c_str());
95  if (!out_archive_stream.is_open()) return false;
96  serializeToStream(input, out_archive_stream);
97  out_archive_stream.close();
98  return true;
99 }
100 
102 template <class T>
103 bool deserializeFromFile(const std::string& filename, T& output) {
104  std::ifstream in_archive_stream(filename.c_str());
105  if (!in_archive_stream.is_open()) return false;
106  deserializeFromStream(in_archive_stream, output);
107  in_archive_stream.close();
108  return true;
109 }
110 
112 template <class T>
113 std::string serialize(const T& input) {
114  return serializeToString(input);
115 }
116 
118 template <class T>
119 void deserialize(const std::string& serialized, T& output) {
120  deserializeFromString(serialized, output);
121 }
123 
127 template <class T>
130 void serializeToXMLStream(const T& input, std::ostream& out_archive_stream,
131  const std::string& name = "data") {
132  boost::archive::xml_oarchive out_archive(out_archive_stream);
133  out_archive << boost::serialization::make_nvp(name.c_str(), input);
134 }
135 
137 template <class T>
138 void deserializeFromXMLStream(std::istream& in_archive_stream, T& output,
139  const std::string& name = "data") {
140  boost::archive::xml_iarchive in_archive(in_archive_stream);
141  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
142 }
143 
145 template <class T>
146 std::string serializeToXMLString(const T& input,
147  const std::string& name = "data") {
148  std::ostringstream out_archive_stream;
149  serializeToXMLStream(input, out_archive_stream, name);
150  return out_archive_stream.str();
151 }
152 
154 template <class T>
155 void deserializeFromXMLString(const std::string& serialized, T& output,
156  const std::string& name = "data") {
157  std::istringstream in_archive_stream(serialized);
158  deserializeFromXMLStream(in_archive_stream, output, name);
159 }
160 
162 template <class T>
163 bool serializeToXMLFile(const T& input, const std::string& filename,
164  const std::string& name = "data") {
165  std::ofstream out_archive_stream(filename.c_str());
166  if (!out_archive_stream.is_open()) return false;
167  serializeToXMLStream(input, out_archive_stream, name);
168  out_archive_stream.close();
169  return true;
170 }
171 
173 template <class T>
174 bool deserializeFromXMLFile(const std::string& filename, T& output,
175  const std::string& name = "data") {
176  std::ifstream in_archive_stream(filename.c_str());
177  if (!in_archive_stream.is_open()) return false;
178  deserializeFromXMLStream(in_archive_stream, output, name);
179  in_archive_stream.close();
180  return true;
181 }
182 
184 template <class T>
185 std::string serializeXML(const T& input,
186  const std::string& name = "data") {
187  return serializeToXMLString(input, name);
188 }
189 
191 template <class T>
192 void deserializeXML(const std::string& serialized, T& output,
193  const std::string& name = "data") {
194  deserializeFromXMLString(serialized, output, name);
195 }
197 
201 template <class T>
204 void serializeToBinaryStream(const T& input, std::ostream& out_archive_stream,
205  const std::string& name = "data") {
206  boost::archive::binary_oarchive out_archive(out_archive_stream);
207  out_archive << boost::serialization::make_nvp(name.c_str(), input);
208 }
209 
211 template <class T>
212 void deserializeFromBinaryStream(std::istream& in_archive_stream, T& output,
213  const std::string& name = "data") {
214  boost::archive::binary_iarchive in_archive(in_archive_stream);
215  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
216 }
217 
219 template <class T>
220 std::string serializeToBinaryString(const T& input,
221  const std::string& name = "data") {
222  std::ostringstream out_archive_stream;
223  serializeToBinaryStream(input, out_archive_stream, name);
224  return out_archive_stream.str();
225 }
226 
228 template <class T>
229 void deserializeFromBinaryString(const std::string& serialized, T& output,
230  const std::string& name = "data") {
231  std::istringstream in_archive_stream(serialized);
232  deserializeFromBinaryStream(in_archive_stream, output, name);
233 }
234 
236 template <class T>
237 bool serializeToBinaryFile(const T& input, const std::string& filename,
238  const std::string& name = "data") {
239  std::ofstream out_archive_stream(filename.c_str());
240  if (!out_archive_stream.is_open()) return false;
241  serializeToBinaryStream(input, out_archive_stream, name);
242  out_archive_stream.close();
243  return true;
244 }
245 
247 template <class T>
248 bool deserializeFromBinaryFile(const std::string& filename, T& output,
249  const std::string& name = "data") {
250  std::ifstream in_archive_stream(filename.c_str());
251  if (!in_archive_stream.is_open()) return false;
252  deserializeFromBinaryStream(in_archive_stream, output, name);
253  in_archive_stream.close();
254  return true;
255 }
256 
258 template <class T>
259 std::string serializeBinary(const T& input,
260  const std::string& name = "data") {
261  return serializeToBinaryString(input, name);
262 }
263 
265 template <class T>
266 void deserializeBinary(const std::string& serialized, T& output,
267  const std::string& name = "data") {
268  deserializeFromBinaryString(serialized, output, name);
269 }
271 
272 } // namespace gtsam
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
void deserializeFromXMLStream(std::istream &in_archive_stream, T &output, const std::string &name="data")
deserializes from a stream in XML
std::string serializeToXMLString(const T &input, const std::string &name="data")
serializes to a string in XML
void serializeToStream(const T &input, std::ostream &out_archive_stream)
std::string serialize(const T &input)
serializes to a string
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
std::string serializeToBinaryString(const T &input, const std::string &name="data")
serializes to a string in binary
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
void deserializeFromStream(std::istream &in_archive_stream, T &output)
deserializes from a stream
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
void serializeToBinaryStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
void deserializeFromString(const std::string &serialized, T &output)
deserializes from a string
void deserializeFromXMLString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
void deserializeFromBinaryString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
traits
Definition: chartTesting.h:28
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:162
void deserialize(const std::string &serialized, T &output)
deserializes from a string
std::string serializeXML(const T &input, const std::string &name="data")
serializes to a string in XML
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Annotation for function names.
Definition: attr.h:48
void deserializeFromBinaryStream(std::istream &in_archive_stream, T &output, const std::string &name="data")
deserializes from a stream in binary
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
std::string serializeBinary(const T &input, const std::string &name="data")
serializes to a string in binary
std::string serializeToString(const T &input)
serializes to a string
void serializeToXMLStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:42