deserializer.cpp
Go to the documentation of this file.
2 #include <fastcdr/Cdr.h>
3 
4 /* make sure to remain compatible with previous version of fastCdr */
5 #if ((FASTCDR_VERSION_MAJOR < 2))
6 #define get_buffer_pointer() getBufferPointer()
7 #define get_current_position() getCurrentPosition()
8 #define CdrVersion Cdr
9 #endif
10 namespace RosMsgParser
11 {
12 
14 {
15  switch (type)
16  {
17  case BOOL:
18  return deserialize<bool>();
19  case CHAR:
20  return deserialize<char>();
21  case BYTE:
22  case UINT8:
23  return deserialize<uint8_t>();
24  case UINT16:
25  return deserialize<uint16_t>();
26  case UINT32:
27  return deserialize<uint32_t>();
28  case UINT64:
29  return deserialize<uint64_t>();
30 
31  case INT8:
32  return deserialize<int8_t>();
33  case INT16:
34  return deserialize<int16_t>();
35  case INT32:
36  return deserialize<int32_t>();
37  case INT64:
38  return deserialize<int64_t>();
39 
40  case FLOAT32:
41  return deserialize<float>();
42  case FLOAT64:
43  return deserialize<double>();
44 
45  case DURATION:
46  case TIME: {
48  tmp.sec = deserialize<uint32_t>();
49  tmp.nsec = deserialize<uint32_t>();
50  return tmp;
51  }
52 
53  default:
54  std::runtime_error("ROS_Deserializer: type not recognized");
55  }
56 
57  return {};
58 }
59 
61 {
62  uint32_t string_size = deserialize<uint32_t>();
63 
64  if (string_size > _bytes_left)
65  {
66  throw std::runtime_error("Buffer overrun in ROS_Deserializer::deserializeString");
67  }
68 
69  if (string_size == 0)
70  {
71  dst = {};
72  return;
73  }
74 
75  const char* buffer_ptr = reinterpret_cast<const char*>(_ptr);
76  dst.assign(buffer_ptr, string_size);
77 
78  _ptr += string_size;
79  _bytes_left -= string_size;
80 }
81 
83 {
84  return deserialize<uint32_t>();
85 }
86 
88 {
89  uint32_t vect_size = deserialize<uint32_t>();
90  if (vect_size > _bytes_left)
91  {
92  throw std::runtime_error("Buffer overrun in "
93  "ROS_Deserializer::deserializeByteSequence");
94  }
95  if (vect_size == 0)
96  {
97  return {};
98  }
99  Span<const uint8_t> out(_ptr, vect_size);
100  jump(vect_size);
101  return out;
102 }
103 
104 const uint8_t* ROS_Deserializer::getCurrentPtr() const
105 {
106  return _ptr;
107 }
108 
110 {
111  if (bytes > _bytes_left)
112  {
113  throw std::runtime_error("Buffer overrun");
114  }
115  _ptr += bytes;
116  _bytes_left -= bytes;
117 }
118 
120 {
121  _ptr = _buffer.data();
122  _bytes_left = _buffer.size();
123 }
124 
125 // ----------------------------------------------
126 
127 template <typename T>
129 {
130  T tmp;
131  cdr.deserialize(tmp);
132  return tmp;
133 }
134 
136 {
137  switch (type)
138  {
139  case BOOL:
140  return Deserialize<bool>(*_cdr);
141  case CHAR:
142  return Deserialize<char>(*_cdr);
143  case BYTE:
144  case UINT8:
145  return Deserialize<uint8_t>(*_cdr);
146  case UINT16:
147  return Deserialize<uint16_t>(*_cdr);
148  case UINT32:
149  return Deserialize<uint32_t>(*_cdr);
150  case UINT64:
151  return Deserialize<uint64_t>(*_cdr);
152 
153  case INT8:
154  return Deserialize<int8_t>(*_cdr);
155  case INT16:
156  return Deserialize<int16_t>(*_cdr);
157  case INT32:
158  return Deserialize<int32_t>(*_cdr);
159  case INT64:
160  return Deserialize<int64_t>(*_cdr);
161 
162  case FLOAT32:
163  return Deserialize<float>(*_cdr);
164  case FLOAT64:
165  return Deserialize<double>(*_cdr);
166 
167  case DURATION:
168  case TIME: {
169  RosMsgParser::Time tmp;
170  tmp.sec = Deserialize<uint32_t>(*_cdr);
171  tmp.nsec = Deserialize<uint32_t>(*_cdr);
172  return tmp;
173  }
174 
175  default:
176  throw std::runtime_error("FastCDR_Deserializer: type not recognized");
177  }
178 
179  return {};
180 }
181 
183 {
184  _cdr->deserialize(dst);
185 }
186 
188 {
189  return Deserialize<uint32_t>(*_cdr);
190 }
191 
193 {
194  // thread_local std::vector<uint8_t> tmp;
195  // _cdr->deserialize(tmp);
196  // return {tmp.data(), tmp.size()};
197 
198  uint32_t seqLength = 0;
199  _cdr->deserialize(seqLength);
200 
201  // dirty trick to change the internal state of cdr
202  auto* ptr = _cdr->get_current_position();
203 
204  uint8_t dummy;
205  _cdr->deserialize(dummy);
206 
207  _cdr->jump(seqLength - 1);
208  return { reinterpret_cast<const uint8_t*>(ptr), seqLength };
209 }
210 
212 {
213  return reinterpret_cast<const uint8_t*>(_cdr->get_buffer_pointer());
214 }
215 
217 {
218  _cdr->jump(bytes);
219 }
220 
222 {
223  using namespace eprosima::fastcdr;
224 
225  char* buffer_ptr = reinterpret_cast<char*>(const_cast<uint8_t*>(_buffer.data()));
226 
227  _cdr_buffer = std::make_shared<FastBuffer>(buffer_ptr, _buffer.size());
228  _cdr = std::make_shared<Cdr>(*_cdr_buffer, Cdr::DEFAULT_ENDIAN, CdrVersion::DDS_CDR);
229  _cdr->read_encapsulation();
230 }
231 
232 } // namespace RosMsgParser
RosMsgParser::FLOAT32
@ FLOAT32
Definition: builtin_types.hpp:56
eprosima::fastcdr
Definition: fixed_size_string.hpp:33
RosMsgParser::Time::nsec
uint32_t nsec
Definition: builtin_types.hpp:153
eprosima::fastcdr::DDS_CDR
@ DDS_CDR
DDS CDR serialization.
Definition: CdrEncoding.hpp:29
bytes
Definition: format.h:4101
RosMsgParser::FastCDR_Deserializer::_cdr_buffer
std::shared_ptr< eprosima::fastcdr::FastBuffer > _cdr_buffer
Definition: deserializer.hpp:131
RosMsgParser::INT8
@ INT8
Definition: builtin_types.hpp:52
RosMsgParser::Time::sec
uint32_t sec
Definition: builtin_types.hpp:152
RosMsgParser::BYTE
@ BYTE
Definition: builtin_types.hpp:46
RosMsgParser::ROS_Deserializer::_ptr
const uint8_t * _ptr
Definition: deserializer.hpp:86
RosMsgParser::TIME
@ TIME
Definition: builtin_types.hpp:58
RosMsgParser::ROS_Deserializer::jump
void jump(size_t bytes) override
Definition: deserializer.cpp:109
RosMsgParser::FastCDR_Deserializer::reset
virtual void reset() override
Definition: deserializer.cpp:221
RosMsgParser::FastCDR_Deserializer::deserializeUInt32
uint32_t deserializeUInt32() override
Definition: deserializer.cpp:187
RosMsgParser::FastCDR_Deserializer::getCurrentPtr
const uint8_t * getCurrentPtr() const override
Definition: deserializer.cpp:211
RosMsgParser::Deserialize
static T Deserialize(eprosima::fastcdr::Cdr &cdr)
Definition: deserializer.cpp:128
RosMsgParser::FastCDR_Deserializer::deserializeString
void deserializeString(std::string &dst) override
Definition: deserializer.cpp:182
eprosima::fastcdr::Cdr
This class offers an interface to serialize/deserialize some basic types using CDR protocol inside an...
Definition: Cdr.h:67
dummy
int dummy
Definition: lstrlib.c:1350
RosMsgParser::ROS_Deserializer::deserializeByteSequence
Span< const uint8_t > deserializeByteSequence() override
Definition: deserializer.cpp:87
RosMsgParser::ROS_Deserializer::getCurrentPtr
const uint8_t * getCurrentPtr() const override
Definition: deserializer.cpp:104
RosMsgParser::UINT8
@ UINT8
Definition: builtin_types.hpp:48
RosMsgParser::FLOAT64
@ FLOAT64
Definition: builtin_types.hpp:57
RosMsgParser
Definition: builtin_types.hpp:34
RosMsgParser::ROS_Deserializer::_bytes_left
size_t _bytes_left
Definition: deserializer.hpp:87
RosMsgParser::FastCDR_Deserializer::jump
void jump(size_t bytes) override
Definition: deserializer.cpp:216
RosMsgParser::UINT64
@ UINT64
Definition: builtin_types.hpp:51
RosMsgParser::Deserializer::_buffer
Span< const uint8_t > _buffer
Definition: deserializer.hpp:57
RosMsgParser::FastCDR_Deserializer::_cdr
std::shared_ptr< eprosima::fastcdr::Cdr > _cdr
Definition: deserializer.hpp:132
RosMsgParser::DURATION
@ DURATION
Definition: builtin_types.hpp:59
RosMsgParser::UINT32
@ UINT32
Definition: builtin_types.hpp:50
RosMsgParser::UINT16
@ UINT16
Definition: builtin_types.hpp:49
RosMsgParser::Variant
Definition: variant.hpp:81
nonstd::span_lite::span
Definition: span.hpp:581
RosMsgParser::ROS_Deserializer::deserializeUInt32
uint32_t deserializeUInt32() override
Definition: deserializer.cpp:82
Cdr.h
RosMsgParser::FastCDR_Deserializer::deserialize
Variant deserialize(BuiltinType type) override
Definition: deserializer.cpp:135
RosMsgParser::FastCDR_Deserializer::deserializeByteSequence
Span< const uint8_t > deserializeByteSequence() override
Definition: deserializer.cpp:192
RosMsgParser::CHAR
@ CHAR
Definition: builtin_types.hpp:47
deserializer.hpp
RosMsgParser::BOOL
@ BOOL
Definition: builtin_types.hpp:45
eprosima::fastcdr::Cdr::deserialize
Cdr & deserialize(_T &value)
Decodes the value of a type from the buffer.
Definition: Cdr.h:1428
RosMsgParser::ROS_Deserializer::deserializeString
void deserializeString(std::string &dst) override
Definition: deserializer.cpp:60
dst
char * dst
Definition: lz4.h:792
RosMsgParser::Time
Definition: builtin_types.hpp:150
RosMsgParser::ROS_Deserializer::reset
void reset() override
Definition: deserializer.cpp:119
RosMsgParser::ROS_Deserializer::deserialize
T deserialize()
Definition: deserializer.hpp:90
RosMsgParser::INT16
@ INT16
Definition: builtin_types.hpp:53
RosMsgParser::BuiltinType
BuiltinType
Definition: builtin_types.hpp:43
RosMsgParser::INT32
@ INT32
Definition: builtin_types.hpp:54
sol::detail::ptr
T * ptr(T &val)
Definition: sol.hpp:2106
RosMsgParser::INT64
@ INT64
Definition: builtin_types.hpp:55


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44