MRPT  1.9.9
CMessage.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <cstdint>
13 #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) */
32  uint32_t type;
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  */
72  void getContentAsString(std::string& str);
73 
74  /** Sets the contents of the message from an arbitary structure - This is
75  * intended for inter-thread comms only, the message will be not
76  * cross-platform.
77  * \sa getContentAsStruct
78  */
79  template <class T>
80  void setContentFromStruct(const T& data)
81  {
82  content.resize(sizeof(data));
83  T* ptr = reinterpret_cast<T*>(&content[0]);
84  *ptr = data;
85  }
86 
87  /** Gets the contents of the message as an arbitary structure - This is
88  * intended for inter-thread comms only, the message will be not
89  * cross-platform.
90  * \sa setContentFromStruct
91  */
92  template <class T>
93  void getContentAsStruct(T& data) const
94  {
96  ASSERT_(content.size() == sizeof(data));
97  data = *reinterpret_cast<T*>(&content[0]);
98  MRPT_END
99  }
100 
101 }; // End of class
102 
103 } // namespace mrpt::serialization
#define MRPT_START
Definition: exceptions.h:241
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
Definition: CMessage.h:32
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
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:245
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
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:80
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:93
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020