diff options
Diffstat (limited to 'include/ble/ByteBuffer.h')
-rw-r--r-- | include/ble/ByteBuffer.h | 69 |
1 files changed, 43 insertions, 26 deletions
diff --git a/include/ble/ByteBuffer.h b/include/ble/ByteBuffer.h index 1032187..5e904c2 100644 --- a/include/ble/ByteBuffer.h +++ b/include/ble/ByteBuffer.h @@ -33,43 +33,53 @@ static const double precision = 10000000; class ByteBufferException : public std::runtime_error { public: - ByteBufferException(std::string const &what) : std::runtime_error(what) { - } + explicit ByteBufferException(std::string const &what) : std::runtime_error(what) {} }; class ByteBuffer { public: static ByteBuffer alloc(std::size_t capacity); - ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity); - - ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t size); + template<size_t N> + explicit ByteBuffer(uint8_t (&bytes)[N]) : zero(bytes), end_(&bytes[N]), cursor(bytes) {} - ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t size, size_t zero); + ByteBuffer(uint8_t* bytes, size_t capacity); inline size_t getSize() const { - return end - zero; - } - - inline size_t getCapacity() const { - return capacity; + return end_ - zero; } inline size_t getCursor() const { - return ptr - zero; + return cursor - zero; } inline size_t getBytesLeft() const { - return end - ptr; + return end_ - cursor; } inline ByteBuffer &setCursor(size_t newCursor) { - ptr = (uint8_t *) &zero[newCursor]; + cursor = (uint8_t *) &zero[newCursor]; return *this; } inline void skip(size_t length) { - ptr += length; + cursor += length; + } + + inline const uint8_t *cbegin() { + return zero; + } + + inline const uint8_t *cend() { + return end_; + } + + inline uint8_t *begin() { + return const_cast<uint8_t *>(zero); + } + + inline uint8_t *end() { + return const_cast<uint8_t *>(end_); } ByteBuffer &write8(uint8_t value); @@ -87,8 +97,16 @@ public: ByteBuffer &writeFLOAT(double d); + /** + * Reads a uint8_t from the start of the buffer. + */ uint8_t get8(size_t index) const; + /** + * Reads a uint8_t relative to the pointer. + */ + uint8_t peek8(size_t relative_index) const; + uint8_t read8(); uint16_t read16le(); @@ -102,32 +120,31 @@ public: void copy(uint8_t *bytes, size_t length) const; + void reset(); + /** * Creates a view from cursor to size. */ ByteBuffer view() const; - // TODO: should return const ByteBuffer view(size_t length) const; + ByteBuffer viewAndSkip(size_t length) { + auto v = view(length); + skip(length); + return v; + } + std::string toString() const; private: - ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, const uint8_t *zero, const uint8_t *end); - - ByteBuffer view(uint8_t *ptr, const uint8_t *end) const; - - void checkAndUpdateEnd(size_t count); - void assertCanAccessRelative(size_t diff) const; void assertCanAccessIndex(uint8_t *p) const; - const std::shared_ptr<uint8_t> bytes; - const size_t capacity; const uint8_t *zero; - const uint8_t *end; - uint8_t *ptr; + const uint8_t *end_; + uint8_t *cursor; }; #endif |