From c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 19 Nov 2018 22:09:59 +0100 Subject: ByteBuffer: o Reducing silliness, no allocations by ByteBuffer. o Create StaticByteBuffer as a nice one-liner to create a buffer. LinuxBluetooth: methods that want a buffer needs to pass it in, ByteBuffer is not allocating anymore. --- include/ble/Bluetooth.h | 2 +- include/ble/ByteBuffer.h | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h index 477bd29..d95d843 100644 --- a/include/ble/Bluetooth.h +++ b/include/ble/Bluetooth.h @@ -129,7 +129,7 @@ public: virtual void writeValue(const BluetoothGattCharacteristicPtr &c, const ByteBuffer &bytes) = 0; - virtual ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c) = 0; + virtual ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer& response) = 0; virtual void discoverServices() = 0; diff --git a/include/ble/ByteBuffer.h b/include/ble/ByteBuffer.h index 5e904c2..09deca5 100644 --- a/include/ble/ByteBuffer.h +++ b/include/ble/ByteBuffer.h @@ -1,13 +1,9 @@ #ifndef BYTE_STREAM_WRAPPER_H #define BYTE_STREAM_WRAPPER_H -#include #include -#include -#include #include #include -#include namespace FLOAT { static const uint32_t positive_infinity = 0x007FFFFE; @@ -38,8 +34,6 @@ public: class ByteBuffer { public: - static ByteBuffer alloc(std::size_t capacity); - template explicit ByteBuffer(uint8_t (&bytes)[N]) : zero(bytes), end_(&bytes[N]), cursor(bytes) {} @@ -58,7 +52,9 @@ public: } inline ByteBuffer &setCursor(size_t newCursor) { - cursor = (uint8_t *) &zero[newCursor]; + auto tmp = (uint8_t *) &zero[newCursor]; + assertCanAccessIndex(tmp); + cursor = tmp; return *this; } @@ -66,19 +62,19 @@ public: cursor += length; } - inline const uint8_t *cbegin() { + inline const uint8_t *cbegin() const { return zero; } - inline const uint8_t *cend() { + inline const uint8_t *cend() const { return end_; } - inline uint8_t *begin() { + inline uint8_t *begin() const { return const_cast(zero); } - inline uint8_t *end() { + inline uint8_t *end() const { return const_cast(end_); } @@ -120,6 +116,8 @@ public: void copy(uint8_t *bytes, size_t length) const; + void copy(ByteBuffer& other) const; + void reset(); /** @@ -147,4 +145,13 @@ private: uint8_t *cursor; }; +template +class StaticByteBuffer : public ByteBuffer { +public: + StaticByteBuffer() : ByteBuffer(raw) {} + +private: + uint8_t raw[N] = {}; +}; + #endif -- cgit v1.2.3