MRPT  1.9.9
CSerializable.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 #pragma once
10 
11 #include <mrpt/rtti/CObject.h>
13 #include <cstdint>
14 
15 // Make this frwd decl independent of MRPT_HAS_MATLAB in config.h:
16 /** Forward declaration for mxArray (avoid #including as much as possible to
17  * speed up compiling) */
18 using mxArray = struct mxArray_tag;
19 
20 namespace mrpt::serialization
21 {
22 /** The virtual base class which provides a unified interface for all persistent
23  *objects in MRPT.
24  * Many important properties of this class are inherited from
25  *mrpt::rtti::CObject.
26  * Refer to the library tutorial: \ref mrpt_serialization_grp
27  * \sa CArchive
28  * \ingroup mrpt_serialization_grp
29  */
31 {
32  friend class CArchive;
33 
34  // This must be added to any CObject derived class:
36 
37  virtual ~CSerializable() {}
38  protected:
39  /** @name CSerializable virtual methods
40  * @{ */
41  /** Must return the current versioning number of the object. Start in zero
42  * for new classes, and increments each time there is a change in the stored
43  * format.
44  */
45  virtual uint8_t serializeGetVersion() const = 0;
46  /** Pure virtual method for writing (serializing) to an abstract archive.
47  * Users don't call this method directly. Instead, use `stream << object;`.
48  * \exception std::exception On any I/O error
49  */
50  virtual void serializeTo(CArchive& out) const = 0;
51  /** Pure virtual method for reading (deserializing) from an abstract
52  *archive. Users don't call this method directly. Instead, use `stream >>
53  *object;`. \param in The input binary stream where the object data must
54  *read from. \param version The version of the object stored in the stream:
55  *use this version number in your code to know how to read the incoming
56  *data. \exception std::exception On any I/O error
57  */
58  virtual void serializeFrom(CArchive& in, uint8_t serial_version) = 0;
59  /** @} */
60 
61  public:
62  /** Introduces a pure virtual method responsible for writing to a `mxArray`
63  * Matlab object, typically a MATLAB `struct` whose contents are documented
64  * in each derived class. \return A new `mxArray` (caller is responsible of
65  * memory freeing) or nullptr is class does not support conversion to
66  * MATLAB.
67  */
68  virtual mxArray* writeToMatlab() const { return nullptr; }
69 }; // End of class def.
70 
71 /** \addtogroup noncstream_serialization Non-CStream serialization functions (in
72  * #include <mrpt/serializatin/CSerializable.h>)
73  * \ingroup mrpt_serialization_grp
74  * @{ */
75 
76 /** Converts (serializes) an MRPT object into an array of bytes.
77  * \param o The object to be serialized.
78  * \param out_vector The vector which at return will contain the data. Size will
79  * be set automatically.
80  * \sa OctetVectorToObject, ObjectToString
81  */
83  const CSerializable* o, std::vector<uint8_t>& out_vector);
84 
85 /** Converts back (de-serializes) a sequence of binary data into a MRPT object,
86  * without prior information about the object's class.
87  * \param in_data The serialized input data representing the object.
88  * \param obj The newly created object will be stored in this smart pointer.
89  * \exception None On any internal exception, this function returns a nullptr
90  * pointer.
91  * \sa ObjectToOctetVector, StringToObject
92  */
94  const std::vector<uint8_t>& in_data, CSerializable::Ptr& obj);
95 
96 /** @} */
97 
98 /** This declaration must be inserted in all CSerializable classes definition,
99  * within the class declaration. */
100 #define DEFINE_SERIALIZABLE(class_name) \
101  DEFINE_MRPT_OBJECT(class_name) \
102  protected: \
103  /*! @name CSerializable virtual methods */ \
104  /*! @{ */ \
105  uint8_t serializeGetVersion() const override; \
106  void serializeTo(mrpt::serialization::CArchive& out) const override; \
107  void serializeFrom( \
108  mrpt::serialization::CArchive& in, uint8_t serial_version) override; \
109 /*! @} */
110 
111 /** This must be inserted in all CSerializable classes implementation files */
112 #define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace) \
113  IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace)
114 
115 /** This declaration must be inserted in virtual CSerializable classes
116  * definition: */
117 #define DEFINE_VIRTUAL_SERIALIZABLE(class_name) \
118  DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
119 
120 /** This must be inserted as implementation of some required members for
121  * virtual CSerializable classes:
122  */
123 #define IMPLEMENTS_VIRTUAL_SERIALIZABLE( \
124  class_name, base_class_name, NameSpace) \
125  IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace)
126 
127 /** This must be inserted if a custom conversion method for MEX API is
128  * implemented in the class */
129 #define DECLARE_MEX_CONVERSION \
130  /*! @name Virtual methods for MRPT-MEX conversion */ \
131  /*! @{ */ \
132  public: \
133  mxArray* writeToMatlab() const override; \
134 /*! @} */
135 
136 /** This must be inserted if a custom conversion method for MEX API is
137  * implemented in the class */
138 #define DECLARE_MEXPLUS_FROM(complete_type) \
139  namespace mexplus \
140  { \
141  template <typename T> \
142  mxArray* from(const T& value); \
143  template <> \
144  mxArray* from(const complete_type& value); \
145  }
146 
147 #define IMPLEMENTS_MEXPLUS_FROM(complete_type) \
148  namespace mexplus \
149  { \
150  template <> \
151  mxArray* from(const complete_type& var) \
152  { \
153  return var.writeToMatlab(); \
154  } \
155  }
156 } // namespace mrpt
157 
158 
virtual void serializeTo(CArchive &out) const =0
Pure virtual method for writing (serializing) to an abstract archive.
virtual void serializeFrom(CArchive &in, uint8_t serial_version)=0
Pure virtual method for reading (deserializing) from an abstract archive.
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:35
void ObjectToOctetVector(const CSerializable *o, std::vector< uint8_t > &out_vector)
Converts (serializes) an MRPT object into an array of bytes.
#define DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
This declaration must be inserted in virtual CObject classes definition:
Definition: CObject.h:249
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:141
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
unsigned char uint8_t
Definition: rptypes.h:41
void OctetVectorToObject(const std::vector< uint8_t > &in_data, CSerializable::Ptr &obj)
Converts back (de-serializes) a sequence of binary data into a MRPT object, without prior information...
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling) ...
Definition: CSerializable.h:18
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
virtual mxArray * writeToMatlab() const
Introduces a pure virtual method responsible for writing to a mxArray Matlab object, typically a MATLAB struct whose contents are documented in each derived class.
Definition: CSerializable.h:68
GLuint in
Definition: glext.h:7274
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
virtual uint8_t serializeGetVersion() const =0
Must return the current versioning number of the object.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020