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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019