Main MRPT website > C++ reference for MRPT 1.9.9
CSerializable.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 
14 #include <mrpt/system/os.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace mrpt::system;
19 
20 #include <cstdio>
21 
23 
24 /* -----------------------------------------------------------------------
25  Used to pass MRPT objects into a CORBA-like object,
26  a string. See doc about "Integration with BABEL".
27  ----------------------------------------------------------------------- */
28 std::string utils::ObjectToString(const CSerializable* o)
29 {
30  CMemoryStream tmp, tmpCoded;
31  std::string str;
32 
33  try
34  {
35  tmp.WriteObject(o);
36  size_t n = tmp.getTotalBytesCount();
37 
38  // Scan the string to code it:
39  // ----------------------------------
40  size_t lastIdx = 0;
41  unsigned char* data = (unsigned char*)tmp.getRawBufferData();
42  for (size_t i = 0; i < n; i++)
43  {
44  unsigned char c = data[i];
45  // Search for first "0x00" byte:
46  if (c == 0x01 || !c)
47  {
48  // Copy all till now:
49  tmpCoded.WriteBuffer(&data[lastIdx], i - lastIdx);
50  lastIdx = i + 1;
51 
52  // And code:
53  // 0x01 --> 0x01 0x01
54  // 0x00 --> 0x01 0x02
55  unsigned char dumm[2];
56  dumm[0] = 0x01;
57  if (c)
58  dumm[1] = 0x01;
59  else
60  dumm[1] = 0x02;
61 
62  // Append to coded stream:
63  tmpCoded.WriteBuffer(dumm, 2);
64  }
65  } // end for i
66 
67  // Copy the rest:
68  if (n != lastIdx) tmpCoded.WriteBuffer(&data[lastIdx], n - lastIdx);
69 
70  // Copy to string object:
71  n = tmpCoded.getTotalBytesCount();
72  str.resize(n);
73  memcpy(&str[0], tmpCoded.getRawBufferData(), n);
74  return str;
75  }
76  catch (std::bad_alloc&)
77  {
78  throw;
79  }
80  catch (std::exception& e)
81  {
82  fprintf(stderr, "[ObjectToString] Exception: %s\n", e.what());
83  return "";
84  }
85  catch (...)
86  {
87  fprintf(stderr, "[ObjectToString] Unknown exception\n");
88  return "";
89  }
90 }
91 
92 /* -----------------------------------------------------------------------
93  Used to pass CORBA-like object into a MRPT object.
94  See doc about "Integration with BABEL".
95  ----------------------------------------------------------------------- */
97 {
99 
100  if (str.empty()) return;
101 
102  CMemoryStream tmp;
103  size_t n;
104  size_t i, lastIdx;
105 
106  obj.reset();
107 
108  n = str.size();
109 
110  // Scan the string to decode it:
111  // ----------------------------------
112  lastIdx = 0;
113  const char* data = str.c_str();
114  unsigned char c;
115  for (i = 0; i < n && (c = data[i]) != 0; i++)
116  {
117  // Search for first "0x01" byte:
118  if (c == 0x01)
119  {
120  // Copy all till now:
121  tmp.WriteBuffer(&data[lastIdx], i - lastIdx + 1);
122  i += 1; // +1 from "for" loop
123  lastIdx = i + 1;
124 
125  // And decode:
126  // 0x01 0x01 --> 0x01
127  // 0x01 0x02 --> 0x00
128  if (data[i] == 0x01)
129  ((unsigned char*)
130  tmp.getRawBufferData())[tmp.getTotalBytesCount() - 1] =
131  (unsigned char)0x01;
132  else
133  ((unsigned char*)
134  tmp.getRawBufferData())[tmp.getTotalBytesCount() - 1] =
135  (unsigned char)0x00;
136  }
137  } // end for i
138 
139  // Copy the rest:
140  if (n != lastIdx) tmp.WriteBuffer(&data[lastIdx], n - lastIdx);
141 
142  // And the '\0' char:
143  char dummy = '\0';
144  tmp.WriteBuffer(&dummy, sizeof(char));
145 
147  obj = tmp.ReadObject();
148 
149  MRPT_END
150 }
151 
152 /* -----------------------------------------------------------------------
153  ObjectToOctetVector
154  ----------------------------------------------------------------------- */
156 {
157  try
158  {
159  CMemoryStream tmp;
160  tmp.WriteObject(o);
161 
162  size_t N = tmp.getTotalBytesCount();
163  out_vector.resize(N);
164  if (N)
165  {
166  os::memcpy(&out_vector[0], N, tmp.getRawBufferData(), N);
167  }
168  }
169  catch (std::bad_alloc&)
170  {
171  throw;
172  }
173  catch (std::exception& e)
174  {
175  fprintf(stderr, "[ObjectToOctetVector] Exception: %s\n", e.what());
176  }
177  catch (...)
178  {
179  fprintf(stderr, "[ObjectToOctetVector] Unknown exception\n");
180  }
181 }
182 
183 /* -----------------------------------------------------------------------
184  OctetVectorToObject
185  ----------------------------------------------------------------------- */
187  const vector_byte& in_data, CSerializable::Ptr& obj)
188 {
189  try
190  {
191  obj.reset();
192 
193  if (in_data.empty()) return;
194 
195  CMemoryStream tmp;
196  tmp.assignMemoryNotOwn(&in_data[0], in_data.size());
197  obj = tmp.ReadObject();
198  }
199  catch (std::bad_alloc&)
200  {
201  throw;
202  }
203  catch (std::exception& e)
204  {
205  fprintf(stderr, "[OctetVectorToObject] Exception: %s\n", e.what());
206  }
207  catch (...)
208  {
209  fprintf(stderr, "[OctetVectorToObject] Unknown exception\n");
210  }
211 }
212 
213 /* -----------------------------------------------------------------------
214  ObjectToRawString
215  ----------------------------------------------------------------------- */
217 {
218  try
219  {
220  CMemoryStream tmp;
221  tmp.WriteObject(o);
222 
223  size_t N = tmp.getTotalBytesCount();
224  out_vector.resize(N);
225  if (N)
226  {
227  os::memcpy(&out_vector[0], N, tmp.getRawBufferData(), N);
228  }
229  }
230  catch (std::bad_alloc&)
231  {
232  throw;
233  }
234  catch (std::exception& e)
235  {
236  fprintf(stderr, "[ObjectToRawString] Exception: %s\n", e.what());
237  }
238  catch (...)
239  {
240  fprintf(stderr, "[ObjectToRawString] Unknown exception\n");
241  }
242 }
243 
244 /* -----------------------------------------------------------------------
245  RawStringToObject
246  ----------------------------------------------------------------------- */
248  const std::string& in_data, CSerializable::Ptr& obj)
249 {
250  try
251  {
252  obj.reset();
253 
254  if (in_data.empty()) return;
255 
256  CMemoryStream tmp;
257  tmp.assignMemoryNotOwn(&in_data[0], in_data.size());
258  obj = tmp.ReadObject();
259  }
260  catch (std::bad_alloc&)
261  {
262  throw;
263  }
264  catch (std::exception& e)
265  {
266  fprintf(stderr, "[RawStringToObject] Exception: %s\n", e.what());
267  }
268  catch (...)
269  {
270  fprintf(stderr, "[RawStringToObject] Unknown exception\n");
271  }
272 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::vector< uint8_t > vector_byte
Definition: types_simple.h:27
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
void StringToObject(const std::string &str, CSerializable::Ptr &obj)
Used to pass CORBA-like objects (strings) into a MRPT object.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
GLenum GLsizei n
Definition: glext.h:5074
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:64
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void OctetVectorToObject(const vector_byte &in_data, CSerializable::Ptr &obj)
Converts back (de-serializes) a sequence of binary data into a MRPT object, without prior information...
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
Definition: CStream.h:207
IMPLEMENTS_VIRTUAL_MRPT_OBJECT(CMultiObjectiveMotionOptimizerBase, CObject, mrpt::nav) CMultiObjectiveMotionOptimizerBase
void ObjectToOctetVector(const CSerializable *o, vector_byte &out_vector)
Converts (serializes) an MRPT object into an array of bytes.
#define MRPT_END
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:27
const GLubyte * c
Definition: glext.h:6313
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
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void assignMemoryNotOwn(const void *data, const uint64_t nBytesInData)
Initilize the data in the stream from a block of memory which is NEITHER OWNED NOR COPIED by the obje...
void RawStringToObject(const std::string &in_str, CSerializable::Ptr &obj)
Converts back (de-serializes) a sequence of binary data within a std::string into a MRPT object...
void ObjectToRawString(const CSerializable *o, std::string &out_str)
Converts (serializes) an MRPT object into an array of bytes within a std::string, without codifying t...
std::string ObjectToString(const CSerializable *o)
Used to pass MRPT objects into a CORBA-like object (strings).
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:147
uint64_t getTotalBytesCount() override
Returns the total size of the internal buffer.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
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