Main MRPT website > C++ reference for 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-2017, 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 #ifndef CSERIALIZABLE_H
10 #define CSERIALIZABLE_H
11 
12 #include <mrpt/utils/CObject.h>
13 #include <mrpt/utils/TTypeName.h>
15 
16 #if MRPT_HAS_MATLAB
17 /** Forward declaration for mxArray (avoid #including as much as possible to
18  * speed up compiling) */
19 typedef struct mxArray_tag mxArray;
20 #endif
21 
22 namespace mrpt
23 {
24 namespace utils
25 {
26 class CStream;
27 }
28 
29 /** Classes for serialization, sockets, ini-file manipulation, streams, list of
30  * properties-values, timewatch, extensions to STL.
31  * \ingroup mrpt_base_grp
32  */
33 namespace utils
34 {
35 /** The virtual base class which provides a unified interface for all persistent
36  *objects in MRPT.
37  * Many important properties of this class are inherited from
38  *mrpt::utils::CObject. See that class for more details.
39  * Refer to the tutorial about <a href="http://www.mrpt.org/Serialization"
40  *>serialization</a> online.
41  * \sa CStream
42  * \ingroup mrpt_base_grp
43  */
45 {
46  // This must be added to any CObject derived class:
48 
49  virtual ~CSerializable() {}
50  protected:
51  /** Introduces a pure virtual method responsible for writing to a CStream.
52  * This can not be used directly be users, instead use "stream << object;"
53  * for writing it to a stream.
54  * \param out The output binary stream where object must be dumped.
55  * \param getVersion If nullptr, the object must be dumped. If not, only the
56  * version of the object dump must be returned in this pointer. This
57  *enables
58  * the versioning of objects dumping and backward compatibility with
59  *previously
60  * stored data.
61  * \exception std::exception On any error, see CStream::WriteBuffer
62  * \sa CStream
63  */
64  virtual void writeToStream(
65  mrpt::utils::CStream& out, int* getVersion) const = 0;
66 
67  /** Introduces a pure virtual method responsible for loading from a CStream
68  * This can not be used directly be users, instead use "stream >> object;"
69  * for reading it from a stream or "stream >> object_ptr;" if the class is
70  * unknown apriori.
71  * \param in The input binary stream where the object data must read from.
72  * \param version The version of the object stored in the stream: use this
73  *version
74  * number in your code to know how to read the incoming data.
75  * \exception std::exception On any error, see CStream::ReadBuffer
76  * \sa CStream
77  */
78  virtual void readFromStream(mrpt::utils::CStream& in, int version) = 0;
79 
80  public:
81 /** Introduces a pure virtual method responsible for writing to a `mxArray`
82  * Matlab object,
83  * typically a MATLAB `struct` whose contents are documented in each derived
84  * class.
85  * \return A new `mxArray` (caller is responsible of memory freeing) or nullptr
86  * is class does not support conversion to MATLAB.
87  */
88 #if MRPT_HAS_MATLAB
89  virtual mxArray* writeToMatlab() const { return nullptr; }
90 #endif
91 }; // End of class def.
92 
93 /** \addtogroup noncstream_serialization Non-CStream serialization functions (in
94  * #include <mrpt/utils/CSerializable.h>)
95  * \ingroup mrpt_base_grp
96  * @{ */
97 
98 /** Used to pass MRPT objects into a CORBA-like object (strings). See doc about
99  * "Integration with BABEL".
100  * \param o The object to be serialized.
101  * \return The string containing the binay version of object.
102  * \sa StringToObject, <a href="http://www.mrpt.org/Integration_with_BABEL"
103  * >Integration with BABEL</a>
104  */
105 std::string ObjectToString(const CSerializable* o);
106 
107 /** Used to pass CORBA-like objects (strings) into a MRPT object.
108  * \param str An string generated with ObjectToString
109  * \param obj A currently empty pointer, where a pointer to the newly created
110  * object will be stored.
111  * \exception None On any internal exception, this function returns NULL.
112  * \sa ObjectToString, <a href="http://www.mrpt.org/Integration_with_BABEL"
113  * >Integration with BABEL</a>
114  */
116 
117 /** Converts (serializes) an MRPT object into an array of bytes.
118  * \param o The object to be serialized.
119  * \param out_vector The vector which at return will contain the data. Size will
120  * be set automatically.
121  * \sa OctetVectorToObject, ObjectToString
122  */
123 void ObjectToOctetVector(const CSerializable* o, vector_byte& out_vector);
124 
125 /** Converts back (de-serializes) a sequence of binary data into a MRPT object,
126  * without prior information about the object's class.
127  * \param in_data The serialized input data representing the object.
128  * \param obj The newly created object will be stored in this smart pointer.
129  * \exception None On any internal exception, this function returns a nullptr
130  * pointer.
131  * \sa ObjectToOctetVector, StringToObject
132  */
134 
135 /** Converts (serializes) an MRPT object into an array of bytes within a
136  * std::string, without codifying to avoid nullptr characters.
137  * This is therefore more efficient than ObjectToString
138  * \param o The object to be serialized.
139  * \param out_vector The string which at return will contain the data. Size will
140  * be set automatically.
141  * \sa RawStringToObject, ObjectToOctetVector
142  */
143 void ObjectToRawString(const CSerializable* o, std::string& out_str);
144 
145 /** Converts back (de-serializes) a sequence of binary data within a std::string
146  * into a MRPT object, without prior information about the object's class.
147  * \param in_data The serialized input data representing the object.
148  * \param obj The newly created object will be stored in this smart pointer.
149  * \exception None On any internal exception, this function returns a nullptr
150  * pointer.
151  * \sa ObjectToRawString
152  */
154 
155 /** @} */
156 
157 /** This declaration must be inserted in all CSerializable classes definition,
158 * within the class declaration. */
159 #define DEFINE_SERIALIZABLE(class_name) \
160  DEFINE_MRPT_OBJECT(class_name) \
161  protected: \
162  /*! @name CSerializable virtual methods */ \
163  /*! @{ */ \
164  void writeToStream(mrpt::utils::CStream& out, int* getVersion) const override;\
165  void readFromStream(mrpt::utils::CStream& in, int version) override; \
166  /*! @} */
167 
168 /** This must be inserted in all CSerializable classes implementation files */
169 #define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace) \
170  IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace)
171 
172 /** This declaration must be inserted in virtual CSerializable classes
173  * definition: */
174 #define DEFINE_VIRTUAL_SERIALIZABLE(class_name) \
175  DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
176 
177 /** This must be inserted as implementation of some required members for
178  * virtual CSerializable classes:
179  */
180 #define IMPLEMENTS_VIRTUAL_SERIALIZABLE( \
181  class_name, base_class_name, NameSpace) \
182  IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace)
183 
184 /** This must be inserted if a custom conversion method for MEX API is
185  * implemented in the class */
186 #if MRPT_HAS_MATLAB
187 #define DECLARE_MEX_CONVERSION \
188  /*! @name Virtual methods for MRPT-MEX conversion */ \
189  /*! @{ */ \
190  public: \
191  virtual mxArray* writeToMatlab() const; \
192 /*! @} */
193 #else
194 #define DECLARE_MEX_CONVERSION // Empty
195 #endif
196 
197 /** This must be inserted if a custom conversion method for MEX API is
198  * implemented in the class */
199 #if MRPT_HAS_MATLAB
200 #define DECLARE_MEXPLUS_FROM(complete_type) \
201  namespace mexplus \
202  { \
203  template <typename T> \
204  mxArray* from(const T& value); \
205  template <> \
206  mxArray* from(const complete_type& value); \
207  }
208 
209 #define IMPLEMENTS_MEXPLUS_FROM(complete_type) \
210  namespace mexplus \
211  { \
212  template <> \
213  mxArray* from(const complete_type& var) \
214  { \
215  return var.writeToMatlab(); \
216  } \
217  }
218 #else
219 #define DECLARE_MEXPLUS_FROM(complete_type) // Empty
220 #define IMPLEMENTS_MEXPLUS_FROM(complete_type) // Empty
221 #endif
222 } // End of namespace
223 } // End of namespace
224 
225 #endif
std::vector< uint8_t > vector_byte
Definition: types_simple.h:27
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
void StringToObject(const std::string &str, CSerializable::Ptr &obj)
Used to pass CORBA-like objects (strings) into a MRPT object.
virtual void writeToStream(mrpt::utils::CStream &out, int *getVersion) const =0
Introduces a pure virtual method responsible for writing to a CStream.
#define DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
This declaration must be inserted in virtual CSerializable classes definition:
Definition: CObject.h:249
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void OctetVectorToObject(const vector_byte &in_data, CSerializable::Ptr &obj)
Converts back (de-serializes) a sequence of binary data into a MRPT object, without prior information...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
void ObjectToOctetVector(const CSerializable *o, vector_byte &out_vector)
Converts (serializes) an MRPT object into an array of bytes.
GLsizei const GLchar ** string
Definition: glext.h:4101
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:47
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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:89
void RawStringToObject(const std::string &in_str, CSerializable::Ptr &obj)
Converts back (de-serializes) a sequence of binary data within a std::string into a MRPT object...
void ObjectToRawString(const CSerializable *o, std::string &out_str)
Converts (serializes) an MRPT object into an array of bytes within a std::string, without codifying t...
std::string ObjectToString(const CSerializable *o)
Used to pass MRPT objects into a CORBA-like object (strings).
GLuint in
Definition: glext.h:7274
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:147
virtual void readFromStream(mrpt::utils::CStream &in, int version)=0
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling) ...
Definition: CSerializable.h:19



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019