88 BinParser(uint8_t* buffer,
size_t buf_len) : buf_pos_(buffer), buf_end_(buffer + buf_len), parent_(*this)
90 assert(buf_pos_ <= buf_end_);
100 : buf_pos_(parent.buf_pos_), buf_end_(parent.buf_pos_ + sub_len), parent_(parent)
102 assert(buf_pos_ <= buf_end_);
120 template <
typename T>
123 if (buf_pos_ +
sizeof(T) > buf_end_)
124 throw UrException(
"Could not parse received package. This can occur if the driver is started while the robot is " 125 "booting - please restart the driver once the robot has finished booting. " 126 "If the problem persists after the robot has booted, please contact the package maintainer.");
128 std::memcpy(&val, buf_pos_,
sizeof(T));
138 template <
typename T>
142 buf_pos_ +=
sizeof(T);
153 parse<uint64_t>(inner);
154 std::memcpy(&val, &inner,
sizeof(
double));
164 parse<uint32_t>(inner);
165 std::memcpy(&val, &inner,
sizeof(
float));
180 parse<uint8_t>(inner);
191 for (
size_t i = 0; i < val.size(); ++i)
204 for (
size_t i = 0; i < val.size(); ++i)
217 for (
size_t i = 0; i < val.size(); ++i)
230 for (
size_t i = 0; i < val.size(); ++i)
242 void rawData(std::unique_ptr<uint8_t>& buffer,
size_t& buffer_length)
244 buffer_length = buf_end_ -
buf_pos_;
245 buffer.reset(
new uint8_t[buffer_length]);
246 memcpy(buffer.get(),
buf_pos_, buffer_length);
257 parse(val,
size_t(buf_end_ - buf_pos_));
266 void parse(std::string& val,
size_t len)
268 val.assign(reinterpret_cast<char*>(buf_pos_), len);
281 parse(val,
size_t(len));
291 template <
typename T,
size_t N>
294 for (
size_t i = 0; i < N; i++)
307 template <
typename T,
size_t N>
312 set = std::bitset<N>(val);
341 return bytes <= size_t(buf_end_ - buf_pos_);
350 template <
typename T>
371 URCL_LOG_DEBUG(
"BinParser: %p - %p (%zu bytes)", buf_pos_, buf_end_, buf_end_ - buf_pos_);
void parse(float &val)
Parses the next bytes as a float.
void consume(size_t bytes)
Moves the current buffer position ahead by a given amount.
void debug()
Logs debugging information about the BinParser object.
void parse(vector6d_t &val)
Parses the next bytes as a vector of 6 doubles.
void parse(T &val)
Parses the next bytes as given type.
void parseRemainder(std::string &val)
Parses the remaining bytes as a string.
bool empty()
Checks if no unparsed bytes remain in the buffer.
BinParser(BinParser &parent, size_t sub_len)
Creates a new BinParser object for part of a buffer from a parent BinParser.
The BinParser class handles a byte buffer and functionality to iteratively parse the content...
BinParser(uint8_t *buffer, size_t buf_len)
Creates a new BinParser object from a given buffer.
void parse(bool &val)
Parses the next byte as a bool.
void parse(double &val)
Parses the next bytes as a double.
uint16_t decode(uint16_t val)
bool checkSize(void)
Checks if enough bytes for a given type remain unparsed in the buffer.
uint64_t decode(uint64_t val)
void parse(vector3d_t &val)
Parses the next bytes as a vector of 3 doubles.
int32_t decode(int32_t val)
void rawData(std::unique_ptr< uint8_t > &buffer, size_t &buffer_length)
Writes the remaining bytes into a given buffer without parsing them.
std::array< double, 3 > vector3d_t
void parse(vector6uint32_t &val)
Parses the next bytes as a vector of 6 unsigned 32 bit integers.
void parse(std::string &val)
Special string parse function that assumes uint8_t len followed by chars.
void parse(std::bitset< N > &set)
Parses the next bytes as a value of a given type, but also copies it to a bitset. ...
void consume()
Sets the current buffer position to the end of the buffer, finishing parsing.
bool checkSize(size_t bytes)
Checks if at least a given number of bytes is still remaining unparsed in the buffer.
#define URCL_LOG_DEBUG(...)
~BinParser()
Deconstructor for the BinParser.
int64_t decode(int64_t val)
std::array< int32_t, 6 > vector6int32_t
void parse(std::array< T, N > &array)
Parses the next bytes as an array of a given type and size.
uint32_t decode(uint32_t val)
std::array< uint32_t, 6 > vector6uint32_t
std::array< double, 6 > vector6d_t
int16_t decode(int16_t val)
T peek()
Parses the next bytes as given type without moving the buffer pointer.
void parse(vector6int32_t &val)
Parses the next bytes as a vector of 6 32 bit integers.
Our base class for exceptions. Specialized exceptions should inherit from those.
void parse(std::string &val, size_t len)
Parses a given number of bytes as a string.