Main MRPT website > C++ reference for MRPT 1.9.9
lib_mrpt_serialization.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 /** \defgroup mrpt_serialization_grp [mrpt-serialization]
11 
12 Serialization (marshalling) portable library for C++ objects persistence.
13 
14 <small> <a href="index.html#libs">Back to list of all libraries</a> | <a href="modules.html" >See all modules</a> </small>
15 <br>
16 
17 # Library `mrpt-serialization`
18 <small> [New in MRPT 2.0.0] </small>
19 
20 This library is part of MRPT and can be installed in Debian-based systems with:
21 
22  sudo apt install libmrpt-serialization-dev
23 
24 Main classes and concepts associated with this library:
25 - mrpt::serialization::CArchive provides:
26  - An abstraction of I/O streams (e.g. std::stream's, MRPT's mrpt::io::CStream's, sockets)
27  - A portable API (endianness aware) for serialization of data structures on those streams.
28 - mrpt::serialization::CSerializable provides:
29  - A generic way to define persistent C++ classes.
30  - Versioning: if class fields are added or removed, you'll still be able to read old data files.
31 
32 Serialization happens via `archive << object` operators in all cases but, underneath, two mechanisms are provided:
33 - **Direct overloading of the `<<`/`>>` operators** for mrpt::serialization::CArchive objects.
34  - Pros: concise declaration (just the two operator functions).
35  - Cons: Cannot handle versioning. Cannot deserialize unknown types (i.e. no support for class factory).
36 - **Via mrpt::serialization::CSerializable** and associated macros:
37  - Pros: Allows polymorphic classes to be (de)serialized. Allows versioning.
38  - Cons: Requires adding macros to class definitions. Requires **registering** the class with \ref mrpt_rtti_grp.
39 
40 Support for STL containers is provided via this "direct mechanism" for the container structure itself, but contained
41 elements can use any of the serialization mechanisms.
42 
43 Serializing `shared_ptr<T>` is supported for any arbitrary type `T`. It is legal to serialize an empty (`nullptr`)
44 smart pointer; an empty pointer will be read back. Polymorphic classes can be also writen and read, although reading
45 a smart pointer to a polymorphic base class is only supported for classes derived from MRPT's CSerializable, since
46 that operation requires registering types in a class factory (see \a mrpt_rtti_grp and mrpt::serialization::CSerializable).
47 
48 ## Example #1: serialize STL container via MRPT `CStream`s
49 
50 See: \ref serialization_stl/test.cpp
51 \snippet serialization_stl/test.cpp example
52 Output:
53 \include serialization_stl/console.out
54 
55 ## Example #2: serialize STL container via `std::ostream` and `std::istream`
56 
57 See: \ref serialization_stl/test.cpp
58 \snippet serialization_stl/test.cpp example_stdio
59 Output:
60 \include serialization_stl/console.out
61 
62 ## Example #3: Serialization of existing MRPT classes
63 
64 Write me!
65 
66 ## Example #4: Polymorphic serialization of user classes
67 
68 Write me!
69 
70 */



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