22 #include <gtsam/config.h>
24 #if GTSAM_ENABLE_BOOST_SERIALIZATION
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>
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>
52 #if __GNUC__ >= 7 && __cplusplus >= 201703L
53 namespace boost {
namespace serialization {
struct U; } }
68 void serializeToStream(
const T& input, std::ostream& out_archive_stream) {
69 boost::archive::text_oarchive out_archive(out_archive_stream);
75 void deserializeFromStream(std::istream& in_archive_stream,
T& output) {
76 boost::archive::text_iarchive in_archive(in_archive_stream);
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();
90 void deserializeFromString(
const std::string& serialized,
T& output) {
91 std::istringstream in_archive_stream(serialized);
92 deserializeFromStream(in_archive_stream, output);
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();
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();
117 std::string serialize(
const T& input) {
118 return serializeToString(input);
123 void deserialize(
const std::string& serialized,
T& output) {
124 deserializeFromString(serialized, output);
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);
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);
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();
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);
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();
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();
189 std::string serializeXML(
const T& input,
190 const std::string&
name =
"data") {
191 return serializeToXMLString(input,
name);
196 void deserializeXML(
const std::string& serialized,
T& output,
197 const std::string&
name =
"data") {
198 deserializeFromXMLString(serialized, output,
name);
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);
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);
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();
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);
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();
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();
263 std::string serializeBinary(
const T& input,
264 const std::string&
name =
"data") {
265 return serializeToBinaryString(input,
name);
270 void deserializeBinary(
const std::string& serialized,
T& output,
271 const std::string&
name =
"data") {
272 deserializeFromBinaryString(serialized, output,
name);