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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019