Main MRPT website > C++ reference for MRPT 1.5.7
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  ---------------------------------------------------------------*/
62 void CMessage::deserializeIntoNewObject( CSerializablePtr &obj )
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 obj.clear_unique();
77 
78 
79  MRPT_END
80 }
81 
82 /*---------------------------------------------------------------
83  setContentFromString
84  ---------------------------------------------------------------*/
86 {
87  content.resize( str.size() );
88  if (content.size()>0)
89  memcpy( &content[0], str.c_str(), str.size() );
90 }
91 
92 /*---------------------------------------------------------------
93  getContentAsString
94  ---------------------------------------------------------------*/
96 {
97  str.resize( content.size() );
98  if (content.size()>0)
99  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 }
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
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.
Definition: zip.h:16
void deserializeIntoNewObject(CSerializablePtr &obj)
A method that parse the data in the message into a new object of (a priori) unknown class...
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:67
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:3902
#define MRPT_END
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:26
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:166
GLsizei const GLchar ** string
Definition: glext.h:3919
#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.
void serializeObject(CSerializable *obj)
A method for serializing a MRPT&#39;s object into the content.
#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:33



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019