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



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