20 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
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>
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>
49 #if __GNUC__ >= 7 && __cplusplus >= 201703L
50 namespace boost {
namespace serialization {
struct U; } }
65 void serializeToStream(
const T& input, std::ostream& out_archive_stream) {
66 boost::archive::text_oarchive out_archive(out_archive_stream);
72 void deserializeFromStream(std::istream& in_archive_stream,
T& output) {
73 boost::archive::text_iarchive in_archive(in_archive_stream);
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();
87 void deserializeFromString(
const std::string& serialized,
T& output) {
88 std::istringstream in_archive_stream(serialized);
89 deserializeFromStream(in_archive_stream, output);
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();
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();
114 std::string serialize(
const T& input) {
115 return serializeToString(input);
120 void deserialize(
const std::string& serialized,
T& output) {
121 deserializeFromString(serialized, output);
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);
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);
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();
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);
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();
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();
186 std::string serializeXML(
const T& input,
187 const std::string&
name =
"data") {
188 return serializeToXMLString(input,
name);
193 void deserializeXML(
const std::string& serialized,
T& output,
194 const std::string&
name =
"data") {
195 deserializeFromXMLString(serialized, output,
name);
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);
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);
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();
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);
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();
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();
260 std::string serializeBinary(
const T& input,
261 const std::string&
name =
"data") {
262 return serializeToBinaryString(input,
name);
267 void deserializeBinary(
const std::string& serialized,
T& output,
268 const std::string&
name =
"data") {
269 deserializeFromBinaryString(serialized, output,
name);