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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020