serialization docstrings
parent
de7332fcea
commit
6d75e992e8
|
|
@ -42,19 +42,25 @@
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
// Serialization directly to strings in compressed format
|
/** @name Standard serialization
|
||||||
|
* Serialization in default compressed format
|
||||||
|
*/
|
||||||
|
///@{
|
||||||
|
/// serializes to a stream
|
||||||
template <class T>
|
template <class T>
|
||||||
void serialize(const T& input, std::ostream& out_archive_stream) {
|
void serialize(const T& input, std::ostream& out_archive_stream) {
|
||||||
boost::archive::text_oarchive out_archive(out_archive_stream);
|
boost::archive::text_oarchive out_archive(out_archive_stream);
|
||||||
out_archive << input;
|
out_archive << input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a stream
|
||||||
template <class T>
|
template <class T>
|
||||||
void deserialize(std::istream& in_archive_stream, T& output) {
|
void deserialize(std::istream& in_archive_stream, T& output) {
|
||||||
boost::archive::text_iarchive in_archive(in_archive_stream);
|
boost::archive::text_iarchive in_archive(in_archive_stream);
|
||||||
in_archive >> output;
|
in_archive >> output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// serializes to a string
|
||||||
template <class T>
|
template <class T>
|
||||||
std::string serialize(const T& input) {
|
std::string serialize(const T& input) {
|
||||||
std::ostringstream out_archive_stream;
|
std::ostringstream out_archive_stream;
|
||||||
|
|
@ -62,12 +68,14 @@ std::string serialize(const T& input) {
|
||||||
return out_archive_stream.str();
|
return out_archive_stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a string
|
||||||
template <class T>
|
template <class T>
|
||||||
void deserialize(const std::string& serialized, T& output) {
|
void deserialize(const std::string& serialized, T& output) {
|
||||||
std::istringstream in_archive_stream(serialized);
|
std::istringstream in_archive_stream(serialized);
|
||||||
deserialize(in_archive_stream, output);
|
deserialize(in_archive_stream, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// serializes to a file
|
||||||
template <class T>
|
template <class T>
|
||||||
bool serializeToFile(const T& input, const std::string& filename) {
|
bool serializeToFile(const T& input, const std::string& filename) {
|
||||||
std::ofstream out_archive_stream(filename.c_str());
|
std::ofstream out_archive_stream(filename.c_str());
|
||||||
|
|
@ -77,6 +85,7 @@ bool serializeToFile(const T& input, const std::string& filename) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a file
|
||||||
template <class T>
|
template <class T>
|
||||||
bool deserializeFromFile(const std::string& filename, T& output) {
|
bool deserializeFromFile(const std::string& filename, T& output) {
|
||||||
std::ifstream in_archive_stream(filename.c_str());
|
std::ifstream in_archive_stream(filename.c_str());
|
||||||
|
|
@ -85,8 +94,13 @@ bool deserializeFromFile(const std::string& filename, T& output) {
|
||||||
in_archive_stream.close();
|
in_archive_stream.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
///@}
|
||||||
|
|
||||||
// Serialization to XML format with named structures
|
/** @name XML Serialization
|
||||||
|
* Serialization to XML format with named structures
|
||||||
|
*/
|
||||||
|
///@{
|
||||||
|
/// serializes to a stream in XML
|
||||||
template <class T>
|
template <class T>
|
||||||
void serializeXML(const T& input, std::ostream& out_archive_stream,
|
void serializeXML(const T& input, std::ostream& out_archive_stream,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -94,6 +108,7 @@ void serializeXML(const T& input, std::ostream& out_archive_stream,
|
||||||
out_archive << boost::serialization::make_nvp(name.c_str(), input);
|
out_archive << boost::serialization::make_nvp(name.c_str(), input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a stream in XML
|
||||||
template <class T>
|
template <class T>
|
||||||
void deserializeXML(std::istream& in_archive_stream, T& output,
|
void deserializeXML(std::istream& in_archive_stream, T& output,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -101,6 +116,7 @@ void deserializeXML(std::istream& in_archive_stream, T& output,
|
||||||
in_archive >> boost::serialization::make_nvp(name.c_str(), output);
|
in_archive >> boost::serialization::make_nvp(name.c_str(), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// serializes to a string in XML
|
||||||
template <class T>
|
template <class T>
|
||||||
std::string serializeXML(const T& input,
|
std::string serializeXML(const T& input,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -109,6 +125,7 @@ std::string serializeXML(const T& input,
|
||||||
return out_archive_stream.str();
|
return out_archive_stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a string in XML
|
||||||
template <class T>
|
template <class T>
|
||||||
void deserializeXML(const std::string& serialized, T& output,
|
void deserializeXML(const std::string& serialized, T& output,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -116,6 +133,7 @@ void deserializeXML(const std::string& serialized, T& output,
|
||||||
deserializeXML(in_archive_stream, output, name);
|
deserializeXML(in_archive_stream, output, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// serializes to an XML file
|
||||||
template <class T>
|
template <class T>
|
||||||
bool serializeToXMLFile(const T& input, const std::string& filename,
|
bool serializeToXMLFile(const T& input, const std::string& filename,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -126,6 +144,7 @@ bool serializeToXMLFile(const T& input, const std::string& filename,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from an XML file
|
||||||
template <class T>
|
template <class T>
|
||||||
bool deserializeFromXMLFile(const std::string& filename, T& output,
|
bool deserializeFromXMLFile(const std::string& filename, T& output,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -135,8 +154,13 @@ bool deserializeFromXMLFile(const std::string& filename, T& output,
|
||||||
in_archive_stream.close();
|
in_archive_stream.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
///@}
|
||||||
|
|
||||||
// Serialization to binary format with named structures
|
/** @name Binary Serialization
|
||||||
|
* Serialization to binary format with named structures
|
||||||
|
*/
|
||||||
|
///@{
|
||||||
|
/// serializes to a stream in binary
|
||||||
template <class T>
|
template <class T>
|
||||||
void serializeBinary(const T& input, std::ostream& out_archive_stream,
|
void serializeBinary(const T& input, std::ostream& out_archive_stream,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -144,6 +168,7 @@ void serializeBinary(const T& input, std::ostream& out_archive_stream,
|
||||||
out_archive << boost::serialization::make_nvp(name.c_str(), input);
|
out_archive << boost::serialization::make_nvp(name.c_str(), input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a stream in binary
|
||||||
template <class T>
|
template <class T>
|
||||||
void deserializeBinary(std::istream& in_archive_stream, T& output,
|
void deserializeBinary(std::istream& in_archive_stream, T& output,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -151,7 +176,7 @@ void deserializeBinary(std::istream& in_archive_stream, T& output,
|
||||||
in_archive >> boost::serialization::make_nvp(name.c_str(), output);
|
in_archive >> boost::serialization::make_nvp(name.c_str(), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialization to binary format with named structures
|
/// serializes to a string in binary
|
||||||
template <class T>
|
template <class T>
|
||||||
std::string serializeBinary(const T& input,
|
std::string serializeBinary(const T& input,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -160,6 +185,7 @@ std::string serializeBinary(const T& input,
|
||||||
return out_archive_stream.str();
|
return out_archive_stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a string in binary
|
||||||
template <class T>
|
template <class T>
|
||||||
void deserializeBinary(const std::string& serialized, T& output,
|
void deserializeBinary(const std::string& serialized, T& output,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -167,6 +193,7 @@ void deserializeBinary(const std::string& serialized, T& output,
|
||||||
deserializeBinary(in_archive_stream, output, name);
|
deserializeBinary(in_archive_stream, output, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// serializes to a binary file
|
||||||
template <class T>
|
template <class T>
|
||||||
bool serializeToBinaryFile(const T& input, const std::string& filename,
|
bool serializeToBinaryFile(const T& input, const std::string& filename,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -177,6 +204,7 @@ bool serializeToBinaryFile(const T& input, const std::string& filename,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deserializes from a binary file
|
||||||
template <class T>
|
template <class T>
|
||||||
bool deserializeFromBinaryFile(const std::string& filename, T& output,
|
bool deserializeFromBinaryFile(const std::string& filename, T& output,
|
||||||
const std::string& name = "data") {
|
const std::string& name = "data") {
|
||||||
|
|
@ -186,5 +214,6 @@ bool deserializeFromBinaryFile(const std::string& filename, T& output,
|
||||||
in_archive_stream.close();
|
in_archive_stream.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
///@}
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,8 @@ boost::filesystem::path resetFilesystem(
|
||||||
// Templated round-trip serialization
|
// Templated round-trip serialization
|
||||||
template<class T>
|
template<class T>
|
||||||
void roundtrip(const T& input, T& output) {
|
void roundtrip(const T& input, T& output) {
|
||||||
// Serialize
|
|
||||||
std::string serialized = serialize(input);
|
std::string serialized = serialize(input);
|
||||||
if (verbose) std::cout << serialized << std::endl << std::endl;
|
if (verbose) std::cout << serialized << std::endl << std::endl;
|
||||||
|
|
||||||
deserialize(serialized, output);
|
deserialize(serialized, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,12 +61,11 @@ void roundtrip(const T& input, T& output) {
|
||||||
template<class T>
|
template<class T>
|
||||||
void roundtripFile(const T& input, T& output) {
|
void roundtripFile(const T& input, T& output) {
|
||||||
boost::filesystem::path path = resetFilesystem()/"graph.dat";
|
boost::filesystem::path path = resetFilesystem()/"graph.dat";
|
||||||
|
|
||||||
serializeToFile(input, path.string());
|
serializeToFile(input, path.string());
|
||||||
deserializeFromFile(path.string(), output);
|
deserializeFromFile(path.string(), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This version requires equality operator
|
// This version requires equality operator and uses string and file round-trips
|
||||||
template<class T>
|
template<class T>
|
||||||
bool equality(const T& input = T()) {
|
bool equality(const T& input = T()) {
|
||||||
T output = create<T>(), outputf = create<T>();
|
T output = create<T>(), outputf = create<T>();
|
||||||
|
|
@ -96,11 +93,8 @@ bool equalsDereferenced(const T& input) {
|
||||||
// Templated round-trip serialization using XML
|
// Templated round-trip serialization using XML
|
||||||
template<class T>
|
template<class T>
|
||||||
void roundtripXML(const T& input, T& output) {
|
void roundtripXML(const T& input, T& output) {
|
||||||
// Serialize
|
|
||||||
std::string serialized = serializeXML<T>(input);
|
std::string serialized = serializeXML<T>(input);
|
||||||
if (verbose) std::cout << serialized << std::endl << std::endl;
|
if (verbose) std::cout << serialized << std::endl << std::endl;
|
||||||
|
|
||||||
// De-serialize
|
|
||||||
deserializeXML(serialized, output);
|
deserializeXML(serialized, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,11 +102,7 @@ void roundtripXML(const T& input, T& output) {
|
||||||
template<class T>
|
template<class T>
|
||||||
void roundtripXMLFile(const T& input, T& output) {
|
void roundtripXMLFile(const T& input, T& output) {
|
||||||
boost::filesystem::path path = resetFilesystem()/"graph.xml";
|
boost::filesystem::path path = resetFilesystem()/"graph.xml";
|
||||||
|
|
||||||
// Serialize
|
|
||||||
serializeToXMLFile(input, path.string());
|
serializeToXMLFile(input, path.string());
|
||||||
|
|
||||||
// De-serialize
|
|
||||||
deserializeFromXMLFile(path.string(), output);
|
deserializeFromXMLFile(path.string(), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,11 +134,8 @@ bool equalsDereferencedXML(const T& input = T()) {
|
||||||
// Templated round-trip serialization using XML
|
// Templated round-trip serialization using XML
|
||||||
template<class T>
|
template<class T>
|
||||||
void roundtripBinary(const T& input, T& output) {
|
void roundtripBinary(const T& input, T& output) {
|
||||||
// Serialize
|
|
||||||
std::string serialized = serializeBinary<T>(input);
|
std::string serialized = serializeBinary<T>(input);
|
||||||
if (verbose) std::cout << serialized << std::endl << std::endl;
|
if (verbose) std::cout << serialized << std::endl << std::endl;
|
||||||
|
|
||||||
// De-serialize
|
|
||||||
deserializeBinary(serialized, output);
|
deserializeBinary(serialized, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -156,9 +143,7 @@ void roundtripBinary(const T& input, T& output) {
|
||||||
template<class T>
|
template<class T>
|
||||||
void roundtripBinaryFile(const T& input, T& output) {
|
void roundtripBinaryFile(const T& input, T& output) {
|
||||||
boost::filesystem::path path = resetFilesystem()/"graph.bin";
|
boost::filesystem::path path = resetFilesystem()/"graph.bin";
|
||||||
// Serialize
|
|
||||||
serializeToBinaryFile(input, path.string());
|
serializeToBinaryFile(input, path.string());
|
||||||
// De-serialize
|
|
||||||
deserializeFromBinaryFile(path.string(), output);
|
deserializeFromBinaryFile(path.string(), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue