diff options
Diffstat (limited to 'ble/ByteBuffer.cpp')
-rw-r--r-- | ble/ByteBuffer.cpp | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp index 820c638..377334b 100644 --- a/ble/ByteBuffer.cpp +++ b/ble/ByteBuffer.cpp @@ -2,19 +2,34 @@ #include <string.h> #include <sstream> #include <iomanip> +#include <cassert> +#include "log.h" using namespace std; +ByteBuffer ByteBuffer::alloc(std::size_t capacity) { + auto bytes = shared_ptr<uint8_t>(new uint8_t[capacity], [](uint8_t* p) { + delete[] p; + }); + return ByteBuffer(bytes, capacity, (size_t) 0, (size_t) 0); +} + ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity) : - bytes(bytes), capacity(capacity), zero(bytes.get()), end(&bytes.get()[capacity]) { - ptr = (uint8_t *) zero; + bytes(bytes), capacity(capacity), zero(bytes.get()), end(bytes.get()) { + ptr = const_cast<uint8_t *>(zero); } -ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t zero, size_t size) : +ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t size) : + bytes(bytes), capacity(capacity), zero(bytes.get()), end(&bytes.get()[size]) { + assert(size <= capacity); + ptr = const_cast<uint8_t *>(this->zero); +} + +ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t size, size_t zero) : bytes(bytes), capacity(capacity), zero(&bytes.get()[zero]), end(&bytes.get()[size]) { assert(zero <= size); assert(size <= capacity); - ptr = (uint8_t *) zero; + ptr = const_cast<uint8_t *>(this->zero); } ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, const uint8_t *zero, const uint8_t *end) : @@ -34,6 +49,20 @@ ByteBuffer &ByteBuffer::write16le(uint16_t value) { return *this; } +ByteBuffer &ByteBuffer::write(const ByteBuffer &value) { + return write(value.zero, value.getSize()); +} + +ByteBuffer &ByteBuffer::write(const uint8_t *bytes, size_t len) { + checkAndUpdateEnd(len); + + memcpy(ptr, bytes, len); + + ptr += len; + + return *this; +} + uint8_t ByteBuffer::get8(size_t index) const { assertCanAccessRelative(index); return ptr[index]; |