Loading...
Searching...
No Matches
serialization.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#pragma once
#include <Eigen/Core>
#include <fstream>
#include <sstream>
#include <string>
// includes for standard serialization types
#include <boost/serialization/version.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/deque.hpp>
#include <boost/serialization/weak_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/export.hpp>
// Workaround a bug in GCC >= 7 and C++17
// ref. https://gitlab.com/libeigen/eigen/-/issues/1676
#ifdef __GNUC__
#if __GNUC__ >= 7 && __cplusplus >= 201703L
namespace boost { namespace serialization { struct U; } }
namespace Eigen { namespace internal {
template<> struct traits<boost::serialization::U> {enum {Flags=0};};
} }
#endif
#endif
namespace gtsam {
template <class T>
void serializeToStream(const T& input, std::ostream& out_archive_stream) {
boost::archive::text_oarchive out_archive(out_archive_stream);
out_archive << input;
}
template <class T>
void deserializeFromStream(std::istream& in_archive_stream, T& output) {
boost::archive::text_iarchive in_archive(in_archive_stream);
in_archive >> output;
}
template <class T>
std::string serializeToString(const T& input) {
std::ostringstream out_archive_stream;
serializeToStream(input, out_archive_stream);
return out_archive_stream.str();
}
template <class T>
void deserializeFromString(const std::string& serialized, T& output) {
std::istringstream in_archive_stream(serialized);
deserializeFromStream(in_archive_stream, output);
}
template <class T>
bool serializeToFile(const T& input, const std::string& filename) {
std::ofstream out_archive_stream(filename.c_str());
if (!out_archive_stream.is_open()) return false;
serializeToStream(input, out_archive_stream);
out_archive_stream.close();
return true;
}
template <class T>
bool deserializeFromFile(const std::string& filename, T& output) {
std::ifstream in_archive_stream(filename.c_str());
if (!in_archive_stream.is_open()) return false;
deserializeFromStream(in_archive_stream, output);
in_archive_stream.close();
return true;
}
template <class T>
std::string serialize(const T& input) {
return serializeToString(input);
}
template <class T>
void deserialize(const std::string& serialized, T& output) {
deserializeFromString(serialized, output);
}
template <class T>
void serializeToXMLStream(const T& input, std::ostream& out_archive_stream,
const std::string& name = "data") {
boost::archive::xml_oarchive out_archive(out_archive_stream);
out_archive << boost::serialization::make_nvp(name.c_str(), input);
}
template <class T>
void deserializeFromXMLStream(std::istream& in_archive_stream, T& output,
const std::string& name = "data") {
boost::archive::xml_iarchive in_archive(in_archive_stream);
in_archive >> boost::serialization::make_nvp(name.c_str(), output);
}
template <class T>
std::string serializeToXMLString(const T& input,
const std::string& name = "data") {
std::ostringstream out_archive_stream;
serializeToXMLStream(input, out_archive_stream, name);
return out_archive_stream.str();
}
template <class T>
void deserializeFromXMLString(const std::string& serialized, T& output,
const std::string& name = "data") {
std::istringstream in_archive_stream(serialized);
deserializeFromXMLStream(in_archive_stream, output, name);
}
template <class T>
bool serializeToXMLFile(const T& input, const std::string& filename,
const std::string& name = "data") {
std::ofstream out_archive_stream(filename.c_str());
if (!out_archive_stream.is_open()) return false;
serializeToXMLStream(input, out_archive_stream, name);
out_archive_stream.close();
return true;
}
template <class T>
bool deserializeFromXMLFile(const std::string& filename, T& output,
const std::string& name = "data") {
std::ifstream in_archive_stream(filename.c_str());
if (!in_archive_stream.is_open()) return false;
deserializeFromXMLStream(in_archive_stream, output, name);
in_archive_stream.close();
return true;
}
template <class T>
std::string serializeXML(const T& input,
const std::string& name = "data") {
return serializeToXMLString(input, name);
}
template <class T>
void deserializeXML(const std::string& serialized, T& output,
const std::string& name = "data") {
deserializeFromXMLString(serialized, output, name);
}
template <class T>
void serializeToBinaryStream(const T& input, std::ostream& out_archive_stream,
const std::string& name = "data") {
boost::archive::binary_oarchive out_archive(out_archive_stream);
out_archive << boost::serialization::make_nvp(name.c_str(), input);
}
template <class T>
void deserializeFromBinaryStream(std::istream& in_archive_stream, T& output,
const std::string& name = "data") {
boost::archive::binary_iarchive in_archive(in_archive_stream);
in_archive >> boost::serialization::make_nvp(name.c_str(), output);
}
template <class T>
std::string serializeToBinaryString(const T& input,
const std::string& name = "data") {
std::ostringstream out_archive_stream;
serializeToBinaryStream(input, out_archive_stream, name);
return out_archive_stream.str();
}
template <class T>
void deserializeFromBinaryString(const std::string& serialized, T& output,
const std::string& name = "data") {
std::istringstream in_archive_stream(serialized);
deserializeFromBinaryStream(in_archive_stream, output, name);
}
template <class T>
bool serializeToBinaryFile(const T& input, const std::string& filename,
const std::string& name = "data") {
std::ofstream out_archive_stream(filename.c_str());
if (!out_archive_stream.is_open()) return false;
serializeToBinaryStream(input, out_archive_stream, name);
out_archive_stream.close();
return true;
}
template <class T>
bool deserializeFromBinaryFile(const std::string& filename, T& output,
const std::string& name = "data") {
std::ifstream in_archive_stream(filename.c_str());
if (!in_archive_stream.is_open()) return false;
deserializeFromBinaryStream(in_archive_stream, output, name);
in_archive_stream.close();
return true;
}
template <class T>
std::string serializeBinary(const T& input,
const std::string& name = "data") {
return serializeToBinaryString(input, name);
}
template <class T>
void deserializeBinary(const std::string& serialized, T& output,
const std::string& name = "data") {
deserializeFromBinaryString(serialized, output, name);
}
} // namespace gtsam
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::serializeToStream
void serializeToStream(const T &input, std::ostream &out_archive_stream)
Definition serialization.h:64
gtsam::deserializeFromXMLFile
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
Definition serialization.h:174
gtsam::serializeToXMLString
std::string serializeToXMLString(const T &input, const std::string &name="data")
serializes to a string in XML
Definition serialization.h:146
gtsam::deserializeFromString
void deserializeFromString(const std::string &serialized, T &output)
deserializes from a string
Definition serialization.h:86
gtsam::serialize
std::string serialize(const T &input)
serializes to a string
Definition serialization.h:113
gtsam::deserializeFromBinaryString
void deserializeFromBinaryString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition serialization.h:229
gtsam::deserializeBinary
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition serialization.h:266
gtsam::deserializeFromXMLString
void deserializeFromXMLString(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition serialization.h:155
gtsam::serializeToBinaryString
std::string serializeToBinaryString(const T &input, const std::string &name="data")
serializes to a string in binary
Definition serialization.h:220
gtsam::deserializeXML
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition serialization.h:192
gtsam::serializeToXMLFile
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Definition serialization.h:163
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:138
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:212
gtsam::serializeXML
std::string serializeXML(const T &input, const std::string &name="data")
serializes to a string in XML
Definition serialization.h:185
gtsam::serializeToString
std::string serializeToString(const T &input)
serializes to a string
Definition serialization.h:78
gtsam::serializeToFile
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
Definition serialization.h:93
gtsam::deserialize
void deserialize(const std::string &serialized, T &output)
deserializes from a string
Definition serialization.h:119
gtsam::serializeToBinaryStream
void serializeToBinaryStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
Definition serialization.h:204
gtsam::deserializeFromStream
void deserializeFromStream(std::istream &in_archive_stream, T &output)
deserializes from a stream
Definition serialization.h:71
gtsam::serializeBinary
std::string serializeBinary(const T &input, const std::string &name="data")
serializes to a string in binary
Definition serialization.h:259
gtsam::serializeToBinaryFile
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
Definition serialization.h:237
gtsam::deserializeFromFile
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
Definition serialization.h:103
gtsam::serializeToXMLStream
void serializeToXMLStream(const T &input, std::ostream &out_archive_stream, const std::string &name="data")
Definition serialization.h:130
gtsam::deserializeFromBinaryFile
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
Definition serialization.h:248