diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-17 20:14:34 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-17 23:30:04 +0100 |
commit | 254be95ff2f37df8adec7ce068448ba8abc8d734 (patch) | |
tree | f1483c2ff4c2e194cf90c59ce741d96917f7aaf5 /ByteBuffer.h | |
parent | 0d0543e4daeb1c6b01d4799736026b6f3aef9779 (diff) | |
parent | 0d0749ff0f842f10fea6929dc466e4e1be458234 (diff) | |
download | ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.gz ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.bz2 ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.xz ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.zip |
Merge remote-tracking branch 'origin/master'
Conflicts:
Bluetooth.cpp
Bluetooth.h
ByteBuffer.cpp
CMakeLists.txt
LinuxBluetooth.cpp
main.cpp
test/ByteBufferTest.cpp
Diffstat (limited to 'ByteBuffer.h')
-rw-r--r-- | ByteBuffer.h | 62 |
1 files changed, 46 insertions, 16 deletions
diff --git a/ByteBuffer.h b/ByteBuffer.h index 828ee4b..d1d02bb 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -6,6 +6,9 @@ #include <string> #include <stdexcept> +// For now +#include "log.h" + class ByteBufferException : public std::runtime_error { public: ByteBufferException(std::string const &what) : std::runtime_error(what) { @@ -14,22 +17,37 @@ public: class ByteBuffer { public: - ByteBuffer(uint8_t *bytes, size_t capacity, size_t size, size_t zero = 0); - - inline size_t getSize() { - return size; + /** + * Wrapping constructor, the size will be equal to the capacity. + */ + ByteBuffer(const uint8_t *bytes, size_t capacity); + + /** + * Wrapping constructor. + */ + ByteBuffer(const uint8_t *bytes, size_t capacity, size_t zero, size_t size); + + inline size_t getSize() const { + DF << "end=" << (uint64_t)end << ", zero=" << (uint64_t)zero << ", size=" << (end - zero); + return end - zero; } - inline size_t getCapacity() { + inline size_t getCapacity() const { return capacity; } - inline size_t getCursor() { - return cursor; + inline size_t getCursor() const { + return ptr - zero; } inline void setCursor(size_t newCursor) { - cursor = newCursor; +// assertCanAccessRelative(newCursor); + ptr = (uint8_t *) &zero[newCursor]; + } + + inline void skip(size_t length) { + checkAndUpdateEnd(length); + ptr += length; } ByteBuffer &add8(uint8_t value); @@ -44,20 +62,32 @@ public: void copy(uint8_t *bytes, size_t length); - ByteBuffer view(size_t length); + /** + * Creates a view from cursor to size. + */ + ByteBuffer view() const; + + // TODO: should return const + ByteBuffer view(size_t length) const; std::string toString() const; private: - void checkAndUpdateSize(size_t count); + ByteBuffer(const 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 canAccessIndex(size_t count); + void assertCanAccessIndex(uint8_t *p) const; - uint8_t *bytes; - size_t zero; - size_t size; - size_t capacity; - size_t cursor; + const uint8_t *bytes; + const size_t capacity; + const uint8_t *zero; + const uint8_t *end; + uint8_t *ptr; }; #endif |