deserializer.hpp
Go to the documentation of this file.
1 #ifndef DESERIALIZER_HPP
2 #define DESERIALIZER_HPP
3 
4 // API adapted to FastCDR
5 
6 #include <exception>
7 
10 
11 namespace eprosima::fastcdr
12 {
13 class FastBuffer;
14 class Cdr;
15 } // namespace eprosima::fastcdr
16 
17 namespace RosMsgParser
18 {
19 
21 {
22 public:
23  virtual void init(Span<const uint8_t> buffer)
24  {
25  _buffer = buffer;
26  reset();
27  }
28 
29  virtual bool isROS2() const = 0;
30 
31  virtual ~Deserializer() = default;
32 
33  // move the memory pointer
34  virtual void jump(size_t bytes) = 0;
35 
36  // deserialize the current pointer into a variant (not a string)
37  [[nodiscard]] virtual Variant deserialize(BuiltinType type) = 0;
38 
39  [[nodiscard]] virtual Span<const uint8_t> deserializeByteSequence() = 0;
40 
41  // deserialize the current pointer into a string
42  virtual void deserializeString(std::string& out) = 0;
43 
44  [[nodiscard]] virtual uint32_t deserializeUInt32() = 0;
45 
46  [[nodiscard]] virtual const uint8_t* getCurrentPtr() const = 0;
47 
48  [[nodiscard]] virtual size_t bytesLeft() const
49  {
50  return _buffer.size() - (getCurrentPtr() - _buffer.data());
51  }
52 
53  // reset the pointer to beginning of buffer
54  virtual void reset() = 0;
55 
56 protected:
58 };
59 
60 //-----------------------------------------------------------------
61 
62 // Specialization od deserializer that works with ROS1
64 {
65 public:
66  Variant deserialize(BuiltinType type) override;
67 
68  bool isROS2() const override
69  {
70  return false;
71  }
72 
73  void deserializeString(std::string& dst) override;
74 
75  uint32_t deserializeUInt32() override;
76 
78 
79  const uint8_t* getCurrentPtr() const override;
80 
81  void jump(size_t bytes) override;
82 
83  void reset() override;
84 
85 protected:
86  const uint8_t* _ptr;
87  size_t _bytes_left;
88 
89  template <typename T>
91  {
92  T out;
93  if (sizeof(T) > _bytes_left)
94  {
95  throw std::runtime_error("Buffer overrun in Deserializer");
96  }
97  out = (*(reinterpret_cast<const T*>(_ptr)));
98  _bytes_left -= sizeof(T);
99  _ptr += sizeof(T);
100  return out;
101  }
102 };
103 
104 //-----------------------------------------------------------------
105 
106 // Specialization od deserializer that works with ROS2
107 // wrapping FastCDR
109 {
110 public:
111  Variant deserialize(BuiltinType type) override;
112 
113  void deserializeString(std::string& dst) override;
114 
115  uint32_t deserializeUInt32() override;
116 
118 
119  const uint8_t* getCurrentPtr() const override;
120 
121  void jump(size_t bytes) override;
122 
123  virtual void reset() override;
124 
125  bool isROS2() const override
126  {
127  return true;
128  }
129 
130 protected:
131  std::shared_ptr<eprosima::fastcdr::FastBuffer> _cdr_buffer;
132  std::shared_ptr<eprosima::fastcdr::Cdr> _cdr;
133 };
134 
136 
137 } // namespace RosMsgParser
138 
139 #endif // DESERIALIZER_HPP
eprosima::fastcdr
Definition: fixed_size_string.hpp:33
RosMsgParser::Deserializer::deserialize
virtual Variant deserialize(BuiltinType type)=0
RosMsgParser::ROS_Deserializer::isROS2
bool isROS2() const override
Definition: deserializer.hpp:68
bytes
Definition: format.h:4101
RosMsgParser::FastCDR_Deserializer::isROS2
bool isROS2() const override
Definition: deserializer.hpp:125
RosMsgParser::FastCDR_Deserializer::_cdr_buffer
std::shared_ptr< eprosima::fastcdr::FastBuffer > _cdr_buffer
Definition: deserializer.hpp:131
RosMsgParser::Deserializer::~Deserializer
virtual ~Deserializer()=default
RosMsgParser::Deserializer::init
virtual void init(Span< const uint8_t > buffer)
Definition: deserializer.hpp:23
RosMsgParser::Deserializer::jump
virtual void jump(size_t bytes)=0
RosMsgParser::ROS_Deserializer::_ptr
const uint8_t * _ptr
Definition: deserializer.hpp:86
RosMsgParser::Deserializer::deserializeByteSequence
virtual Span< const uint8_t > deserializeByteSequence()=0
RosMsgParser::Deserializer::deserializeString
virtual void deserializeString(std::string &out)=0
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::FastCDR_Deserializer::deserializeString
void deserializeString(std::string &dst) override
Definition: deserializer.cpp:182
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::Deserializer::reset
virtual void reset()=0
RosMsgParser
Definition: builtin_types.hpp:34
builtin_types.hpp
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::Deserializer::_buffer
Span< const uint8_t > _buffer
Definition: deserializer.hpp:57
RosMsgParser::Deserializer::deserializeUInt32
virtual uint32_t deserializeUInt32()=0
RosMsgParser::Deserializer::getCurrentPtr
virtual const uint8_t * getCurrentPtr() const =0
RosMsgParser::FastCDR_Deserializer::_cdr
std::shared_ptr< eprosima::fastcdr::Cdr > _cdr
Definition: deserializer.hpp:132
RosMsgParser::Deserializer::bytesLeft
virtual size_t bytesLeft() const
Definition: deserializer.hpp:48
RosMsgParser::Deserializer
Definition: deserializer.hpp:20
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
RosMsgParser::ROS_Deserializer
Definition: deserializer.hpp:63
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
variant.hpp
RosMsgParser::ROS_Deserializer::deserializeString
void deserializeString(std::string &dst) override
Definition: deserializer.cpp:60
RosMsgParser::FastCDR_Deserializer
Definition: deserializer.hpp:108
dst
char * dst
Definition: lz4.h:792
RosMsgParser::ROS_Deserializer::reset
void reset() override
Definition: deserializer.cpp:119
RosMsgParser::ROS_Deserializer::deserialize
T deserialize()
Definition: deserializer.hpp:90
RosMsgParser::BuiltinType
BuiltinType
Definition: builtin_types.hpp:43
RosMsgParser::Deserializer::isROS2
virtual bool isROS2() const =0


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