Main MRPT website > C++ reference for MRPT 1.9.9
CMRPTMessage.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/utils/CMessage.h>
14 #include <mrpt/system/os.h>
16 #include <cstring>
17 
18 using namespace mrpt::utils;
19 
20 /*---------------------------------------------------------------
21  serializeObject
22  ---------------------------------------------------------------*/
24 {
26  CMemoryStream auxStream;
27 
28  // Dump the object in the memory stream:
29  auxStream.WriteObject(obj);
30 
31  // Copy data to message:
32  content.resize(auxStream.getTotalBytesCount());
33  memcpy(
34  &content[0], // Dest
35  auxStream.getRawBufferData(), // Src
36  content.size());
37 
38  MRPT_END
39 }
40 
41 /*---------------------------------------------------------------
42  deserializeIntoExistingObject
43  ---------------------------------------------------------------*/
45 {
47  CMemoryStream auxStream;
48 
49  // Copy data into the stream:
50  auxStream.WriteBuffer(&content[0], content.size());
51  auxStream.Seek(0);
52 
53  // Try to parse data into existing object:
54  auxStream.ReadObject(obj);
55 
56  MRPT_END
57 }
58 
59 /*---------------------------------------------------------------
60  deserializeIntoNewObject
61  ---------------------------------------------------------------*/
63 {
65  CMemoryStream auxStream;
66 
67  // Copy data into the stream:
68  if (!content.empty())
69  {
70  auxStream.WriteBuffer(&content[0], content.size());
71  auxStream.Seek(0);
72 
73  // Try to parse data into a new object:
74  obj = auxStream.ReadObject();
75  }
76  else
77  obj.reset();
78 
79  MRPT_END
80 }
81 
82 /*---------------------------------------------------------------
83  setContentFromString
84  ---------------------------------------------------------------*/
86 {
87  content.resize(str.size());
88  if (content.size() > 0) memcpy(&content[0], str.c_str(), str.size());
89 }
90 
91 /*---------------------------------------------------------------
92  getContentAsString
93  ---------------------------------------------------------------*/
95 {
96  str.resize(content.size());
97  if (content.size() > 0) memcpy(&str[0], &content[0], str.size());
98 }
99 
100 /*---------------------------------------------------------------
101  setContentFromPointer
102  ---------------------------------------------------------------*/
104 {
105  content.resize(sizeof(void*));
106  void** ptrPtr = reinterpret_cast<void**>(&content[0]);
107  *ptrPtr = ptr;
108 }
109 
110 /*---------------------------------------------------------------
111  getContentAsPointer
112  ---------------------------------------------------------------*/
114 {
115  MRPT_START
116  ASSERT_(content.size() == sizeof(void*));
117 
118  return *reinterpret_cast<void**>(const_cast<unsigned char*>(&content[0]));
119 
120  MRPT_END
121 }
void setContentFromString(const std::string &str)
Sets the contents of the message from a string.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:64
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
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:27
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...
void WriteObject(const CSerializable *o)
Writes an object to the stream.
Definition: CStream.cpp:158
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.
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
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:355



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