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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020