Main MRPT website > C++ reference for MRPT 1.4.0
CMessage.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-2016, 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 CMessage_H
10 #define CMessage_H
11 
12 #include <mrpt/utils/core_defs.h>
13 #include <mrpt/utils/mrpt_stdint.h>
14 #include <vector>
15 
16 /*---------------------------------------------------------------
17  Class
18  ---------------------------------------------------------------*/
19 namespace mrpt
20 {
21  namespace utils
22  {
23  class CSerializable; struct CSerializablePtr;
24 
25  /** A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object.
26  * A message consists of a "header" (or type), and a "body" (or content).
27  * Apart from arbitrary data, specific methods are provided for easing the serialization of MRPT's "CSerializable" objects.
28  * This class is also used for passing data to hardware interfaces (see )
29  * \sa CClientTCPSocket
30  * \ingroup mrpt_base_grp
31  */
33  {
34  public:
35  /** An identifier of the message type.
36  */
37  uint32_t type;
38 
39  /** The contents of the message (memory is automatically handled by the std::vector object)
40  */
41  std::vector<unsigned char> content;
42 
43  /** A method for serializing a MRPT's object into the content.
44  * Any modification to data in "content" after this will corrupt the object serialization.
45  * Member "type" is unmodified in this method.
46  */
47  void serializeObject( CSerializable *obj );
48 
49  /** A method that parse the data in the message into an existing object.
50  * Note that the class of the object must be known and must match the one of the serialized object.
51  * \except std::exception On corrupt data, unknown serialized objects, unknown serialized object version, non-matching classes,...
52  */
53  void deserializeIntoExistingObject( CSerializable *obj );
54 
55  /** A method that parse the data in the message into a new object of (a priori) unknown class.
56  * The pointer will contain on return a copy of the reconstructed object. Deleting this object when
57  * no longer required is the responsability of the user. Note that previous contents of the pointer
58  * will be ignored (it should be NULL).
59  * \except std::exception On corrupt data, unknown serialized objects, unknown serialized object version,...
60  */
61  void deserializeIntoNewObject( CSerializablePtr &obj );
62 
63  /** Sets the contents of the message from a string
64  * \sa getContentAsString
65  */
66  void setContentFromString( const std::string &str );
67 
68  /** Gets the contents of the message as a string
69  * \sa setContentFromString
70  */
71  void getContentAsString( std::string &str );
72 
73  /** Sets the contents of the message from a "void*" (the pointer itself becomes the message) - This is intended for inter-thread comms only.
74  * \sa getContentAsPointer
75  */
76  void setContentFromPointer( void * ptr );
77 
78  /** Gets the contents of the message as a "void*" (the pointer itself is the message) - This is intended for inter-thread comms only.
79  * \sa setContentFromPointer
80  */
81  void * getContentAsPointer() const;
82 
83  /** Sets the contents of the message from an arbitary structure - This is intended for inter-thread comms only, the message will be not cross-platform.
84  * \sa getContentAsStruct
85  */
86  template <class T>
87  void setContentFromStruct( const T &data )
88  {
89  content.resize( sizeof(data) );
90  T * ptr = reinterpret_cast< T* >( &content[0] );
91  *ptr = data;
92  }
93 
94  /** Gets the contents of the message as an arbitary structure - This is intended for inter-thread comms only, the message will be not cross-platform.
95  * \sa setContentFromStruct
96  */
97  template <class T>
98  void getContentAsStruct( T &data ) const
99  {
100  MRPT_START
101  ASSERT_(content.size() == sizeof(data) );
102  data = * reinterpret_cast< T* >( &content[0] );
103  MRPT_END
104  }
105 
106 
107  }; // End of class
108 
109  } // End of namespace
110 } // End of namespace
111 
112 #endif
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
#define MRPT_END
void getContentAsStruct(T &data) const
Gets the contents of the message as an arbitary structure - This is intended for inter-thread comms o...
Definition: CMessage.h:98
std::vector< unsigned char > content
The contents of the message (memory is automatically handled by the std::vector object) ...
Definition: CMessage.h:41
void setContentFromStruct(const T &data)
Sets the contents of the message from an arbitary structure - This is intended for inter-thread comms...
Definition: CMessage.h:87
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:32
uint32_t type
An identifier of the message type.
Definition: CMessage.h:37



Page generated by Doxygen 1.8.14 for MRPT 1.4.0 SVN: at Mon Mar 5 22:51:34 UTC 2018