Main MRPT website > C++ reference for MRPT 1.9.9
Classes | Modules
[mrpt-serialization]

Detailed Description

Serialization (marshalling) portable library for C++ objects persistence.

Back to list of all libraries | See all modules

Library <tt>mrpt-serialization</tt>

[New in MRPT 2.0.0]

This library is part of MRPT and can be installed in Debian-based systems with:

    sudo apt install libmrpt-serialization-dev

Main classes and concepts associated with this library:

Serialization happens via archive << object operators in all cases but, underneath, two mechanisms are provided:

Support for STL containers is provided via this "direct mechanism" for the container structure itself, but contained elements can use any of the serialization mechanisms.

Serializing shared_ptr<T> is supported for any arbitrary type T. It is legal to serialize an empty (nullptr) smart pointer; an empty pointer will be read back. Polymorphic classes can be also writen and read, although reading a smart pointer to a polymorphic base class is only supported for classes derived from MRPT's CSerializable, since that operation requires registering types in a class factory (see mrpt_rtti_grp and mrpt::serialization::CSerializable).

Example #1: serialize STL container via MRPT <tt>CStream</tt>s

See: serialization_stl/test.cpp

#include <iostream> // cout
{
// Declare data to be serialized:
std::map<std::string, uint32_t> m1{{"one", 1}, {"two", 2}};
// === Write ===
{
// CStream output:
mrpt::io::CFileOutputStream ofs("file.bin");
auto arch_out = mrpt::serialization::archiveFrom(ofs);
// Use << to serialize in binary form:
arch_out << m1;
}
// === Read ===
std::map<std::string, uint32_t> m2;
{
// CStream output:
mrpt::io::CFileInputStream ifs("file.bin");
auto arch_in = mrpt::serialization::archiveFrom(ifs);
// Use >> to deserialize:
arch_in >> m2;
}
std::cout << "Wrote: ";
printMap(m1);
std::cout << "Read : ";
printMap(m2);
}

Output:

Wrote: one=1, two=2,
Read: one=1, two=2,

Example #2: serialize STL container via <tt>std::ostream</tt> and <tt>std::istream</tt>

See: serialization_stl/test.cpp

#include <fstream> // io std streams
#include <iostream> // cout
{
// Declare data to be serialized:
std::map<std::string, uint32_t> m1{{"one", 1}, {"two", 2}};
// === Write ===
{
// CStream output:
std::ofstream ofs("file.bin");
auto arch_out = mrpt::serialization::archiveFrom<std::ostream>(ofs);
// Use << to serialize in binary form:
arch_out << m1;
}
// === Read ===
std::map<std::string, uint32_t> m2;
{
// CStream output:
std::ifstream ifs("file.bin");
auto arch_in = mrpt::serialization::archiveFrom<std::istream>(ifs);
// Use >> to deserialize:
arch_in >> m2;
}
std::cout << "Wrote: ";
printMap(m1);
std::cout << "Read : ";
printMap(m2);
}

Output:

Wrote: one=1, two=2,
Read: one=1, two=2,

Example #3: Serialization of existing MRPT classes

Write me!

Example #4: Polymorphic serialization of user classes

Write me!

Collaboration diagram for [mrpt-serialization]:

Classes

class  mrpt::serialization::CArchive
 Virtual base class for "archives": classes abstracting I/O streams. More...
 
class  mrpt::serialization::CMessage
 A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object. More...
 
class  mrpt::serialization::CSerializable
 The virtual base class which provides a unified interface for all persistent objects in MRPT. More...
 

Modules

 Non-CStream serialization functions (in
 
CFileOutputStream.h
stl_serialization.h
CFileInputStream.h
WriteAndReadExample
void WriteAndReadExample()
[example]
Definition: vision_stereo_rectify/test.cpp:28
mrpt::io::CFileInputStream
This CStream derived class allow using a file as a read-only, binary stream.
Definition: io/CFileInputStream.h:23
WriteAndReadExampleStdIO
void WriteAndReadExampleStdIO()
[example]
Definition: vision_stereo_rectify/test.cpp:67
mrpt::io::CFileOutputStream
This CStream derived class allow using a file as a write-only, binary stream.
Definition: io/CFileOutputStream.h:24
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:561
printMap
void printMap(const CONTAINER &m)
Definition: vision_stereo_rectify/test.cpp:14
CArchive.h
archiveFrom_std_streams.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST