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
14#include <vector>
15
16/*---------------------------------------------------------------
17 Class
18 ---------------------------------------------------------------*/
19namespace 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 */
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 {
101 ASSERT_(content.size() == sizeof(data) );
102 data = * reinterpret_cast< T* >( &content[0] );
104 }
105
106
107 }; // End of class
108
109 } // End of namespace
110} // End of namespace
111
112#endif
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition CMessage.h:33
void getContentAsString(std::string &str)
Gets the contents of the message as a string.
void serializeObject(CSerializable *obj)
A method for serializing a MRPT's object into the content.
void deserializeIntoExistingObject(CSerializable *obj)
A method that parse the data in the message into an existing object.
void setContentFromPointer(void *ptr)
Sets the contents of the message from a "void*" (the pointer itself becomes the message) - This is in...
void * getContentAsPointer() const
Gets the contents of the message as a "void*" (the pointer itself is the message) - This is intended ...
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
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 setContentFromString(const std::string &str)
Sets the contents of the message from a string.
void deserializeIntoNewObject(CSerializablePtr &obj)
A method that parse the data in the message into a new object of (a priori) unknown class.
uint32_t type
An identifier of the message type.
Definition CMessage.h:37
#define MRPT_START
#define ASSERT_(f)
#define MRPT_END
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned long uint32_t
Definition pstdint.h:216



Page generated by Doxygen 1.9.8 for MRPT 1.4.0 SVN: at Wed Dec 6 15:06:50 UTC 2023