1
0
mirror of https://gitlab.com/obbart/universal_robots_ros_driver.git synced 2026-04-10 18:10:47 +02:00

BinParser improvements

This commit is contained in:
Simon Rasmussen
2017-02-09 12:15:54 +01:00
parent e523a5d466
commit e94bbb5536

View File

@@ -4,8 +4,10 @@
#include <endian.h> #include <endian.h>
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <assert.h>
#include "ur_modern_driver/types.h" #include "ur_modern_driver/types.h"
#include "ur_modern_driver/log.h"
class BinParser { class BinParser {
private: private:
@@ -16,14 +18,16 @@ public:
BinParser(uint8_t *buffer, size_t buf_len) : BinParser(uint8_t *buffer, size_t buf_len) :
_buf_pos(buffer), _buf_pos(buffer),
_buf_end(buffer+buf_len), _buf_end(buffer+buf_len),
_parent(*this) _parent(*this) {
{ } assert(_buf_pos <= _buf_end);
}
BinParser(BinParser &parent, size_t sub_len) : BinParser(BinParser &parent, size_t sub_len) :
_buf_pos(parent._buf_pos), _buf_pos(parent._buf_pos),
_buf_end(parent._buf_pos+sub_len), _buf_end(parent._buf_pos+sub_len),
_parent(parent) _parent(parent) {
{ } assert(_buf_pos <= _buf_end);
}
~BinParser() { ~BinParser() {
_parent._buf_pos = _buf_pos; _parent._buf_pos = _buf_pos;
@@ -63,6 +67,7 @@ public:
template<typename T> template<typename T>
T peek() { T peek() {
assert(_buf_pos <= _buf_end);
return decode(*(reinterpret_cast<T*>(_buf_pos))); return decode(*(reinterpret_cast<T*>(_buf_pos)));
} }
@@ -93,8 +98,12 @@ public:
parse(val.rotation); parse(val.rotation);
} }
void parse_remainder(std::string &val) {
parse(val, size_t(_buf_end - _buf_pos));
}
void parse(std::string &val, size_t len) { void parse(std::string &val, size_t len) {
val = val.assign(reinterpret_cast<char*>(_buf_pos), len); val.assign(reinterpret_cast<char*>(_buf_pos), len);
_buf_pos += len; _buf_pos += len;
} }
@@ -112,7 +121,10 @@ public:
} }
} }
void skip(size_t bytes) { void consume() {
_buf_pos = _buf_end;
}
void consume(size_t bytes) {
_buf_pos += bytes; _buf_pos += bytes;
} }
@@ -123,4 +135,12 @@ public:
bool check_size(void) { bool check_size(void) {
return check_size(T::SIZE); return check_size(T::SIZE);
} }
bool empty() {
return _buf_pos == _buf_end;
}
void debug() {
LOG_DEBUG("BinParser: %zx - %zx (%zu bytes)", _buf_pos, _buf_end, _buf_end - _buf_pos);
}
}; };