Main MRPT website > C++ reference
MRPT logo
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-2015, 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 namespace mrpt
17 {
18  namespace utils {
19  class BASE_IMPEXP CStream;
20  }
21 
22  /** Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
23  * \ingroup mrpt_base_grp
24  */
25  namespace utils
26  {
28 
29  /** The virtual base class which provides a unified interface for all persistent objects in MRPT.
30  * Many important properties of this class are inherited from mrpt::utils::CObject. See that class for more details.
31  * Refer to the tutorial about <a href="http://www.mrpt.org/Serialization" >serialization</a> online.
32  * \sa CStream
33  * \ingroup mrpt_base_grp
34  */
35  class BASE_IMPEXP CSerializable : public mrpt::utils::CObject
36  {
37  // This must be added to any CObject derived class:
39 
40  virtual ~CSerializable() { }
41 
42  protected:
43  /** Introduces a pure virtual method responsible for writing to a CStream.
44  * This can not be used directly be users, instead use "stream << object;"
45  * for writing it to a stream.
46  * \param out The output binary stream where object must be dumped.
47  * \param getVersion If NULL, the object must be dumped. If not, only the
48  * version of the object dump must be returned in this pointer. This enables
49  * the versioning of objects dumping and backward compatibility with previously
50  * stored data.
51  * \exception std::exception On any error, see CStream::WriteBuffer
52  * \sa CStream
53  */
54  virtual void writeToStream(mrpt::utils::CStream &out, int *getVersion) const = 0;
55 
56  /** Introduces a pure virtual method responsible for loading from a CStream
57  * This can not be used directly be users, instead use "stream >> object;"
58  * for reading it from a stream or "stream >> object_ptr;" if the class is
59  * unknown apriori.
60  * \param in The input binary stream where the object data must read from.
61  * \param version The version of the object stored in the stream: use this version
62  * number in your code to know how to read the incoming data.
63  * \exception std::exception On any error, see CStream::ReadBuffer
64  * \sa CStream
65  */
66  virtual void readFromStream(mrpt::utils::CStream &in, int version) = 0;
67  }; // End of class def.
68 
70 
71  /** @name Non-streaming serialization functions
72  @{ */
73 
74  /** Used to pass MRPT objects into a CORBA-like object (strings). See doc about "Integration with BABEL".
75  * \param o The object to be serialized.
76  * \return The string containing the binay version of object.
77  * \sa StringToObject, <a href="http://www.mrpt.org/Integration_with_BABEL" >Integration with BABEL</a>
78  */
79  std::string BASE_IMPEXP ObjectToString(const CSerializable *o);
80 
81  /** Used to pass CORBA-like objects (strings) into a MRPT object.
82  * \param str An string generated with ObjectToString
83  * \param obj A currently empty pointer, where a pointer to the newly created object will be stored.
84  * \exception None On any internal exception, this function returns NULL.
85  * \sa ObjectToString, <a href="http://www.mrpt.org/Integration_with_BABEL" >Integration with BABEL</a>
86  */
87  void BASE_IMPEXP StringToObject(const std::string &str, CSerializablePtr &obj);
88 
89  /** Converts (serializes) an MRPT object into an array of bytes.
90  * \param o The object to be serialized.
91  * \param out_vector The vector which at return will contain the data. Size will be set automatically.
92  * \sa OctetVectorToObject, ObjectToString
93  */
94  void BASE_IMPEXP ObjectToOctetVector(const CSerializable *o, vector_byte & out_vector);
95 
96  /** Converts back (de-serializes) a sequence of binary data into a MRPT object, without prior information about the object's class.
97  * \param in_data The serialized input data representing the object.
98  * \param obj The newly created object will be stored in this smart pointer.
99  * \exception None On any internal exception, this function returns a NULL pointer.
100  * \sa ObjectToOctetVector, StringToObject
101  */
102  void BASE_IMPEXP OctetVectorToObject(const vector_byte & in_data, CSerializablePtr &obj);
103 
104  /** Converts (serializes) an MRPT object into an array of bytes within a std::string, without codifying to avoid NULL characters.
105  * This is therefore more efficient than ObjectToString
106  * \param o The object to be serialized.
107  * \param out_vector The string which at return will contain the data. Size will be set automatically.
108  * \sa RawStringToObject, ObjectToOctetVector
109  */
110  void BASE_IMPEXP ObjectToRawString(const CSerializable *o, std::string & out_str);
111 
112  /** Converts back (de-serializes) a sequence of binary data within a std::string into a MRPT object, without prior information about the object's class.
113  * \param in_data The serialized input data representing the object.
114  * \param obj The newly created object will be stored in this smart pointer.
115  * \exception None On any internal exception, this function returns a NULL pointer.
116  * \sa ObjectToRawString
117  */
118  void BASE_IMPEXP RawStringToObject(const std::string & in_str, CSerializablePtr &obj);
119 
120  /** @} */
121 
122  /** Like DEFINE_SERIALIZABLE, but for template classes that need the DLL imp/exp keyword in Windows. */
123  #define DEFINE_SERIALIZABLE_CUSTOM_LINKAGE(class_name, _VOID_LINKAGE_, _STATIC_LINKAGE_, _VIRTUAL_LINKAGE_ ) \
124  DEFINE_MRPT_OBJECT_CUSTOM_LINKAGE(class_name, _STATIC_LINKAGE_, _VIRTUAL_LINKAGE_ ) \
125  protected: \
126  /*! @name CSerializable virtual methods */ \
127  /*! @{ */ \
128  _VOID_LINKAGE_ writeToStream(mrpt::utils::CStream &out, int *getVersion) const;\
129  _VOID_LINKAGE_ readFromStream(mrpt::utils::CStream &in, int version); \
130  /*! @} */
131 
132  /** This declaration must be inserted in all CSerializable classes definition, within the class declaration. */
133  #define DEFINE_SERIALIZABLE(class_name) \
134  DEFINE_SERIALIZABLE_CUSTOM_LINKAGE(class_name, void /*no extra linkage keyword*/, static /*none*/,virtual /*none*/ )
135 
136  /** This declaration must be inserted in all CSerializable classes definition, before the class declaration.
137  */
138  #define DEFINE_SERIALIZABLE_PRE_CUSTOM_LINKAGE(class_name,_LINKAGE_) \
139  DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, mrpt::utils::CSerializable, _LINKAGE_ class_name) \
140  _LINKAGE_ ::mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in, class_name##Ptr &pObj);
141 
142  #define DEFINE_SERIALIZABLE_POST_CUSTOM_LINKAGE(class_name,_LINKAGE_) \
143  DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, mrpt::utils::CSerializable, _LINKAGE_ class_name)
144 
145  /** This declaration must be inserted in all CSerializable classes definition, before the class declaration.
146  */
147  #define DEFINE_SERIALIZABLE_PRE(class_name) \
148  DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, mrpt::utils::CSerializable, BASE_IMPEXP class_name) \
149  BASE_IMPEXP ::mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in, class_name##Ptr &pObj);
150 
151  #define DEFINE_SERIALIZABLE_POST(class_name) \
152  DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, mrpt::utils::CSerializable, BASE_IMPEXP class_name)
153 
154  /** This declaration must be inserted in all CSerializable classes definition, before the class declaration.
155  */
156  #define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_ ) \
157  DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE2(class_name, base_name, _LINKAGE_ class_name) \
158  _LINKAGE_ ::mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in, class_name##Ptr &pObj);
159 
160  #define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_ ) \
161  DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE2(class_name, base_name, _LINKAGE_ class_name)
162 
163  /** This declaration must be inserted in all CSerializable classes definition, before the class declaration. */
164  #define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name) \
165  DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, BASE_IMPEXP ) \
166  BASE_IMPEXP ::mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in, class_name##Ptr &pObj);
167 
168  #define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name) \
169  DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, BASE_IMPEXP ) \
170 
171  /** This must be inserted in all CSerializable classes implementation files */
172  #define IMPLEMENTS_SERIALIZABLE(class_name, base,NameSpace) \
173  IMPLEMENTS_MRPT_OBJECT(class_name, base,NameSpace) \
174  mrpt::utils::CStream& NameSpace::operator>>(mrpt::utils::CStream& in, NameSpace::class_name##Ptr &pObj) \
175  { pObj = NameSpace::class_name##Ptr( in.ReadObject() ); return in; }
176 
177  /** This declaration must be inserted in virtual CSerializable classes definition: */
178  #define DEFINE_VIRTUAL_SERIALIZABLE(class_name) \
179  DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
180 
181  /** This must be inserted as implementation of some required members for
182  * virtual CSerializable classes:
183  */
184  #define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class_name,NameSpace) \
185  IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name,NameSpace) \
186  mrpt::utils::CStream& NameSpace::operator>>(mrpt::utils::CStream& in, class_name##Ptr &pObj) \
187  { pObj = class_name##Ptr( in.ReadObject() ); return in; }
188 
189  } // End of namespace
190 } // End of namespace
191 
192 #endif
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:35
class BASE_IMPEXP CStream
Definition: math_frwds.h:26
#define DEFINE_VIRTUAL_MRPT_OBJECT(class_name)
This declaration must be inserted in virtual CSerializable classes definition:
Definition: CObject.h:268
STL namespace.
#define DEFINE_MRPT_OBJECT_POST(class_name)
Definition: CObject.h:245
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
class BASE_IMPEXP CSerializable
Definition: CStream.h:23
void BASE_IMPEXP ObjectToOctetVector(const CSerializable *o, vector_byte &out_vector)
Converts (serializes) an MRPT object into an array of bytes.
std::vector< uint8_t > vector_byte
Definition: types_simple.h:23
struct BASE_IMPEXP CSerializablePtr
Definition: CStream.h:24
#define DEFINE_MRPT_OBJECT_PRE(class_name)
This declaration must be inserted in all CObject classes definition, before the class declaration...
Definition: CObject.h:244
std::string BASE_IMPEXP ObjectToString(const CSerializable *o)
Used to pass MRPT objects into a CORBA-like object (strings).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void BASE_IMPEXP OctetVectorToObject(const vector_byte &in_data, CSerializablePtr &obj)
Converts back (de-serializes) a sequence of binary data into a MRPT object, without prior information...
void BASE_IMPEXP StringToObject(const std::string &str, CSerializablePtr &obj)
Used to pass CORBA-like objects (strings) into a MRPT object.
void BASE_IMPEXP 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...
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:119
void BASE_IMPEXP RawStringToObject(const std::string &in_str, CSerializablePtr &obj)
Converts back (de-serializes) a sequence of binary data within a std::string into a MRPT object...



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015