MRPT  2.0.4
CMessage.cpp
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 
10 #include "serialization-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
16 #include <cstring> // memcpy()
17 #include <sstream>
18 
19 using namespace mrpt::serialization;
20 
22 {
24  std::stringstream auxStream;
25  auto arch = mrpt::serialization::archiveFrom<std::iostream>(auxStream);
26 
27  // Dump the object in the memory stream:
28  arch.WriteObject(obj);
29 
30  // Copy data to message:
31  const auto& data = auxStream.str();
32  content.resize(data.size());
33  memcpy(
34  &content[0], // Dest
35  &data[0], // Src
36  content.size());
37 
38  MRPT_END
39 }
40 
41 /*---------------------------------------------------------------
42  deserializeIntoExistingObject
43  ---------------------------------------------------------------*/
45 {
47  std::stringstream auxStream;
48  auto arch = mrpt::serialization::archiveFrom<std::iostream>(auxStream);
49 
50  // Copy data into the stream:
51  arch.WriteBuffer(&content[0], content.size());
52  auxStream.seekg(0);
53 
54  // Try to parse data into existing object:
55  arch.ReadObject(obj);
56 
57  MRPT_END
58 }
59 
60 /*---------------------------------------------------------------
61  deserializeIntoNewObject
62  ---------------------------------------------------------------*/
64 {
66  std::stringstream auxStream;
67  auto arch = mrpt::serialization::archiveFrom<std::iostream>(auxStream);
68 
69  // Copy data into the stream:
70  if (!content.empty())
71  {
72  arch.WriteBuffer(&content[0], content.size());
73  auxStream.seekg(0);
74 
75  // Try to parse data into a new object:
76  obj = arch.ReadObject();
77  }
78  else
79  obj.reset();
80 
81  MRPT_END
82 }
83 
84 /*---------------------------------------------------------------
85  setContentFromString
86  ---------------------------------------------------------------*/
87 void CMessage::setContentFromString(const std::string& str)
88 {
89  content.resize(str.size());
90  if (content.size() > 0) memcpy(&content[0], str.c_str(), str.size());
91 }
92 
93 /*---------------------------------------------------------------
94  getContentAsString
95  ---------------------------------------------------------------*/
96 void CMessage::getContentAsString(std::string& str)
97 {
98  str.resize(content.size());
99  if (content.size() > 0) memcpy(&str[0], &content[0], str.size());
100 }
#define MRPT_START
Definition: exceptions.h:241
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
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 serializeObject(const CSerializable *obj)
A method for serializing a MRPT&#39;s object into the content.
Definition: CMessage.cpp:21
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020