Main MRPT website > C++ reference for MRPT 1.9.9
CMessage.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-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 
10 #include "serialization-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
16 #include <sstream>
17 #include <cstring> // memcpy()
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 }
101 
102 /*---------------------------------------------------------------
103  setContentFromPointer
104  ---------------------------------------------------------------*/
106 {
107  content.resize(sizeof(void*));
108  void** ptrPtr = reinterpret_cast<void**>(&content[0]);
109  *ptrPtr = ptr;
110 }
111 
112 /*---------------------------------------------------------------
113  getContentAsPointer
114  ---------------------------------------------------------------*/
116 {
117  MRPT_START
118  ASSERT_(content.size() == sizeof(void*));
119 
120  return *reinterpret_cast<void**>(const_cast<unsigned char*>(&content[0]));
121 
122  MRPT_END
123 }
exceptions.h
mrpt::serialization::CSerializable::Ptr
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:37
mrpt::serialization::CMessage::getContentAsPointer
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
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::serialization::CMessage::deserializeIntoNewObject
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
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::serialization::CMessage::content
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object)
Definition: CMessage.h:37
mrpt::serialization::CSerializable
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:32
serialization-precomp.h
mrpt::serialization
Definition: aligned_serialization.h:14
mrpt::serialization::CMessage::getContentAsString
void getContentAsString(std::string &str)
Gets the contents of the message as a string.
Definition: CMessage.cpp:96
mrpt::serialization::CMessage::setContentFromPointer
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
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::serialization::CMessage::setContentFromString
void setContentFromString(const std::string &str)
Sets the contents of the message from a string.
Definition: CMessage.cpp:87
string
GLsizei const GLchar ** string
Definition: glext.h:4101
CSerializable.h
archiveFrom_std_streams.h
mrpt::serialization::CMessage::deserializeIntoExistingObject
void deserializeIntoExistingObject(CSerializable *obj)
A method that parse the data in the message into an existing object.
Definition: CMessage.cpp:44
CMessage.h
mrpt::serialization::CMessage::serializeObject
void serializeObject(const CSerializable *obj)
A method for serializing a MRPT's object into the content.
Definition: CMessage.cpp:21
mrpt::system::os::memcpy
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:356



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST