MRPT  1.9.9
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-2019, 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  ---------------------------------------------------------------*/
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  ---------------------------------------------------------------*/
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
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
GLsizei const GLchar ** string
Definition: glext.h:4116
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
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
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".
Definition: os.cpp:358



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 31e853f50 Thu Nov 21 23:57:32 2019 +0100 at vie nov 22 00:00:11 CET 2019