diff options
Diffstat (limited to 'include/ble/ByteBuffer.h')
-rw-r--r-- | include/ble/ByteBuffer.h | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/include/ble/ByteBuffer.h b/include/ble/ByteBuffer.h index 09deca5..6b09049 100644 --- a/include/ble/ByteBuffer.h +++ b/include/ble/ByteBuffer.h @@ -1,5 +1,5 @@ -#ifndef BYTE_STREAM_WRAPPER_H -#define BYTE_STREAM_WRAPPER_H +#ifndef BYTE_BUFFER_H +#define BYTE_BUFFER_H #include <cstdint> #include <memory> @@ -34,16 +34,29 @@ public: class ByteBuffer { public: + ByteBuffer() noexcept : zero(nullptr), end_(nullptr), cursor(nullptr) {}; + template<size_t N> - explicit ByteBuffer(uint8_t (&bytes)[N]) : zero(bytes), end_(&bytes[N]), cursor(bytes) {} + constexpr explicit ByteBuffer(uint8_t (&bytes)[N]) noexcept : zero(bytes), end_(&bytes[N]), cursor(zero) {}; + + ByteBuffer(uint8_t *bytes, size_t size) noexcept : zero(bytes), end_(&bytes[size]), cursor(zero) {}; - ByteBuffer(uint8_t* bytes, size_t capacity); + ByteBuffer(uint8_t *bytes, size_t size, size_t position) : zero(bytes), end_(&bytes[size]), cursor(zero) { + setPosition(position); + }; + + template<size_t N> + static const ByteBuffer wrapInitialized(const uint8_t (&bytes)[N], size_t cursor = 0) noexcept { + return wrap(bytes, N, N); + }; + + static const ByteBuffer wrap(const uint8_t *bytes, size_t size, size_t cursor = 0) noexcept; inline size_t getSize() const { return end_ - zero; } - inline size_t getCursor() const { + inline size_t getPosition() const { return cursor - zero; } @@ -51,10 +64,8 @@ public: return end_ - cursor; } - inline ByteBuffer &setCursor(size_t newCursor) { - auto tmp = (uint8_t *) &zero[newCursor]; - assertCanAccessIndex(tmp); - cursor = tmp; + inline ByteBuffer &setPosition(size_t newCursor) { + cursor = &zero[newCursor]; return *this; } @@ -70,12 +81,12 @@ public: return end_; } - inline uint8_t *begin() const { + inline uint8_t *underlying() { return const_cast<uint8_t *>(zero); } inline uint8_t *end() const { - return const_cast<uint8_t *>(end_); + return const_cast<uint8_t *>(cend()); } ByteBuffer &write8(uint8_t value); @@ -99,7 +110,7 @@ public: uint8_t get8(size_t index) const; /** - * Reads a uint8_t relative to the pointer. + * Reads a uint8_t relative to the cursor. */ uint8_t peek8(size_t relative_index) const; @@ -114,35 +125,31 @@ public: */ double readFLOAT(); - void copy(uint8_t *bytes, size_t length) const; + void copyFromEntire(const ByteBuffer &other) const; - void copy(ByteBuffer& other) const; + void copyTo(uint8_t *bytes, size_t length) const; - void reset(); + ByteBuffer viewCursorToEnd() const; - /** - * Creates a view from cursor to size. - */ - ByteBuffer view() const; + ByteBuffer viewBeginningToCursor() const; - ByteBuffer view(size_t length) const; + /** + * Creates a view from cursor to cursor + length. + */ + ByteBuffer viewForward(size_t length) const; - ByteBuffer viewAndSkip(size_t length) { - auto v = view(length); - skip(length); - return v; - } + /** + * Creates a view from cursor to cursor + length and then moves the cursor length further. + */ + ByteBuffer viewForwardAndSkip(size_t length); std::string toString() const; -private: - void assertCanAccessRelative(size_t diff) const; - - void assertCanAccessIndex(uint8_t *p) const; + void assertCanAccessPosition(size_t position) const; + void assertCanAccessPtr(uint8_t* ptr) const; - const uint8_t *zero; - const uint8_t *end_; - uint8_t *cursor; +protected: + uint8_t *zero, *end_, *cursor; }; template<size_t N> |