gtsam  4.1.0
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 <sstream>
23 #include <fstream>
24 #include <string>
25 
26 // includes for standard serialization types
27 #include <boost/serialization/optional.hpp>
28 #include <boost/serialization/shared_ptr.hpp>
29 #include <boost/serialization/vector.hpp>
30 #include <boost/serialization/map.hpp>
31 #include <boost/serialization/list.hpp>
32 #include <boost/serialization/deque.hpp>
33 #include <boost/serialization/weak_ptr.hpp>
34 
35 #include <boost/archive/text_oarchive.hpp>
36 #include <boost/archive/text_iarchive.hpp>
37 #include <boost/archive/xml_iarchive.hpp>
38 #include <boost/archive/xml_oarchive.hpp>
39 #include <boost/archive/binary_iarchive.hpp>
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/serialization/export.hpp>
42 
43 namespace gtsam {
44 
48 template <class T>
51 void serializeToStream(const T& input, std::ostream& out_archive_stream) {
52  boost::archive::text_oarchive out_archive(out_archive_stream);
53  out_archive << input;
54 }
55 
57 template <class T>
58 void deserializeFromStream(std::istream& in_archive_stream, T& output) {
59  boost::archive::text_iarchive in_archive(in_archive_stream);
60  in_archive >> output;
61 }
62 
64 template <class T>
65 std::string serializeToString(const T& input) {
66  std::ostringstream out_archive_stream;
67  serializeToStream(input, out_archive_stream);
68  return out_archive_stream.str();
69 }
70 
72 template <class T>
73 void deserializeFromString(const std::string& serialized, T& output) {
74  std::istringstream in_archive_stream(serialized);
75  deserializeFromStream(in_archive_stream, output);
76 }
77 
79 template <class T>
80 bool serializeToFile(const T& input, const std::string& filename) {
81  std::ofstream out_archive_stream(filename.c_str());
82  if (!out_archive_stream.is_open()) return false;
83  serializeToStream(input, out_archive_stream);
84  out_archive_stream.close();
85  return true;
86 }
87 
89 template <class T>
90 bool deserializeFromFile(const std::string& filename, T& output) {
91  std::ifstream in_archive_stream(filename.c_str());
92  if (!in_archive_stream.is_open()) return false;
93  deserializeFromStream(in_archive_stream, output);
94  in_archive_stream.close();
95  return true;
96 }
97 
99 template <class T>
100 std::string serialize(const T& input) {
101  return serializeToString(input);
102 }
103 
105 template <class T>
106 void deserialize(const std::string& serialized, T& output) {
107  deserializeFromString(serialized, output);
108 }
110 
114 template <class T>
117 void serializeToXMLStream(const T& input, std::ostream& out_archive_stream,
118  const std::string& name = "data") {
119  boost::archive::xml_oarchive out_archive(out_archive_stream);
120  out_archive << boost::serialization::make_nvp(name.c_str(), input);
121 }
122 
124 template <class T>
125 void deserializeFromXMLStream(std::istream& in_archive_stream, T& output,
126  const std::string& name = "data") {
127  boost::archive::xml_iarchive in_archive(in_archive_stream);
128  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
129 }
130 
132 template <class T>
133 std::string serializeToXMLString(const T& input,
134  const std::string& name = "data") {
135  std::ostringstream out_archive_stream;
136  serializeToXMLStream(input, out_archive_stream, name);
137  return out_archive_stream.str();
138 }
139 
141 template <class T>
142 void deserializeFromXMLString(const std::string& serialized, T& output,
143  const std::string& name = "data") {
144  std::istringstream in_archive_stream(serialized);
145  deserializeFromXMLStream(in_archive_stream, output, name);
146 }
147 
149 template <class T>
150 bool serializeToXMLFile(const T& input, const std::string& filename,
151  const std::string& name = "data") {
152  std::ofstream out_archive_stream(filename.c_str());
153  if (!out_archive_stream.is_open()) return false;
154  serializeToXMLStream(input, out_archive_stream, name);
155  out_archive_stream.close();
156  return true;
157 }
158 
160 template <class T>
161 bool deserializeFromXMLFile(const std::string& filename, T& output,
162  const std::string& name = "data") {
163  std::ifstream in_archive_stream(filename.c_str());
164  if (!in_archive_stream.is_open()) return false;
165  deserializeFromXMLStream(in_archive_stream, output, name);
166  in_archive_stream.close();
167  return true;
168 }
169 
171 template <class T>
172 std::string serializeXML(const T& input,
173  const std::string& name = "data") {
174  return serializeToXMLString(input, name);
175 }
176 
178 template <class T>
179 void deserializeXML(const std::string& serialized, T& output,
180  const std::string& name = "data") {
181  deserializeFromXMLString(serialized, output, name);
182 }
184 
188 template <class T>
191 void serializeToBinaryStream(const T& input, std::ostream& out_archive_stream,
192  const std::string& name = "data") {
193  boost::archive::binary_oarchive out_archive(out_archive_stream);
194  out_archive << boost::serialization::make_nvp(name.c_str(), input);
195 }
196 
198 template <class T>
199 void deserializeFromBinaryStream(std::istream& in_archive_stream, T& output,
200  const std::string& name = "data") {
201  boost::archive::binary_iarchive in_archive(in_archive_stream);
202  in_archive >> boost::serialization::make_nvp(name.c_str(), output);
203 }
204 
206 template <class T>
207 std::string serializeToBinaryString(const T& input,
208  const std::string& name = "data") {
209  std::ostringstream out_archive_stream;
210  serializeToBinaryStream(input, out_archive_stream, name);
211  return out_archive_stream.str();
212 }
213 
215 template <class T>
216 void deserializeFromBinaryString(const std::string& serialized, T& output,
217  const std::string& name = "data") {
218  std::istringstream in_archive_stream(serialized);
219  deserializeFromBinaryStream(in_archive_stream, output, name);
220 }
221 
223 template <class T>
224 bool serializeToBinaryFile(const T& input, const std::string& filename,
225  const std::string& name = "data") {
226  std::ofstream out_archive_stream(filename.c_str());
227  if (!out_archive_stream.is_open()) return false;
228  serializeToBinaryStream(input, out_archive_stream, name);
229  out_archive_stream.close();
230  return true;
231 }
232 
234 template <class T>
235 bool deserializeFromBinaryFile(const std::string& filename, T& output,
236  const std::string& name = "data") {
237  std::ifstream in_archive_stream(filename.c_str());
238  if (!in_archive_stream.is_open()) return false;
239  deserializeFromBinaryStream(in_archive_stream, output, name);
240  in_archive_stream.close();
241  return true;
242 }
243 
245 template <class T>
246 std::string serializeBinary(const T& input,
247  const std::string& name = "data") {
248  return serializeToBinaryString(input, name);
249 }
250 
252 template <class T>
253 void deserializeBinary(const std::string& serialized, T& output,
254  const std::string& name = "data") {
255  deserializeFromBinaryString(serialized, output, name);
256 }
258 
259 } // namespace gtsam
gtsam::serializeToBinaryStream
void serializeToBinaryStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
Definition: serialization.h:191
gtsam::deserializeFromString
void deserializeFromString(const std::string &serialized, T &output)
deserializes from a string
Definition: serialization.h:73
gtsam::deserializeXML
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition: serialization.h:179
gtsam::serializeToBinaryFile
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
Definition: serialization.h:224
gtsam::serializeXML
std::string serializeXML(const T &input, const std::string &name="data")
serializes to a string in XML
Definition: serialization.h:172
gtsam::serializeToBinaryString
std::string serializeToBinaryString(const T &input, const std::string &name="data")
serializes to a string in binary
Definition: serialization.h:207
gtsam::serialize
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:100
gtsam::deserializeFromFile
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
Definition: serialization.h:90
gtsam::deserializeFromBinaryFile
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
Definition: serialization.h:235
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::deserializeFromXMLString
void deserializeFromXMLString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition: serialization.h:142
gtsam::serializeToXMLFile
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Definition: serialization.h:150
gtsam::serializeToXMLString
std::string serializeToXMLString(const T &input, const std::string &name="data")
serializes to a string in XML
Definition: serialization.h:133
gtsam::deserializeBinary
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition: serialization.h:253
gtsam::deserializeFromXMLStream
void deserializeFromXMLStream(std::istream &in_archive_stream, T &output, const std::string &name="data")
deserializes from a stream in XML
Definition: serialization.h:125
gtsam::deserializeFromXMLFile
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
Definition: serialization.h:161
gtsam::serializeBinary
std::string serializeBinary(const T &input, const std::string &name="data")
serializes to a string in binary
Definition: serialization.h:246
gtsam::deserialize
void deserialize(const std::string &serialized, T &output)
deserializes from a string
Definition: serialization.h:106
gtsam::deserializeFromBinaryStream
void deserializeFromBinaryStream(std::istream &in_archive_stream, T &output, const std::string &name="data")
deserializes from a stream in binary
Definition: serialization.h:199
gtsam::deserializeFromStream
void deserializeFromStream(std::istream &in_archive_stream, T &output)
deserializes from a stream
Definition: serialization.h:58
gtsam::serializeToString
std::string serializeToString(const T &input)
serializes to a string
Definition: serialization.h:65
gtsam::serializeToStream
void serializeToStream(const T &input, std::ostream &out_archive_stream)
Definition: serialization.h:51
gtsam::deserializeFromBinaryString
void deserializeFromBinaryString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition: serialization.h:216
gtsam::serializeToFile
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
Definition: serialization.h:80
gtsam::serializeToXMLStream
void serializeToXMLStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
Definition: serialization.h:117