aboutsummaryrefslogtreecommitdiff
path: root/ble/ByteBuffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ble/ByteBuffer.cpp')
-rw-r--r--ble/ByteBuffer.cpp113
1 files changed, 45 insertions, 68 deletions
diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp
index da61427..1a294f1 100644
--- a/ble/ByteBuffer.cpp
+++ b/ble/ByteBuffer.cpp
@@ -5,57 +5,38 @@
#include <iomanip>
#include <cmath>
#include <iostream>
+#include <ble/ByteBuffer.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()) {
- ptr = const_cast<uint8_t *>(zero);
-}
-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);
-}
+using namespace std;
-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 = const_cast<uint8_t *>(this->zero);
+ByteBuffer ByteBuffer::alloc(std::size_t size) {
+ return {new uint8_t[size], size};
}
-ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, const uint8_t *zero, const uint8_t *end) :
- bytes(bytes), capacity(capacity), zero(zero), end(end), ptr((uint8_t *) zero) {
+ByteBuffer::ByteBuffer(uint8_t* bytes, size_t size) :
+ zero(bytes), end_(&bytes[size]), cursor(bytes) {
}
ByteBuffer &ByteBuffer::write8(uint8_t value) {
- checkAndUpdateEnd(1);
- (*ptr++) = value;
+ assertCanAccessRelative(1);
+ (*cursor++) = value;
return *this;
}
ByteBuffer &ByteBuffer::write16le(uint16_t value) {
- checkAndUpdateEnd(2);
- (*ptr++) = (uint8_t) (value & 0xff);
- (*ptr++) = (uint8_t) ((value >> 8) & 0xff);
+ assertCanAccessRelative(2);
+ (*cursor++) = (uint8_t) (value & 0xff);
+ (*cursor++) = (uint8_t) ((value >> 8) & 0xff);
return *this;
}
ByteBuffer &ByteBuffer::write32le(uint32_t value) {
- checkAndUpdateEnd(4);
- (*ptr++) = (uint8_t) (value & 0xff);
- (*ptr++) = (uint8_t) ((value >> 8) & 0xff);
- (*ptr++) = (uint8_t) ((value >> 16) & 0xff);
- (*ptr++) = (uint8_t) ((value >> 24) & 0xff);
+ assertCanAccessRelative(4);
+ (*cursor++) = (uint8_t) (value & 0xff);
+ (*cursor++) = (uint8_t) ((value >> 8) & 0xff);
+ (*cursor++) = (uint8_t) ((value >> 16) & 0xff);
+ (*cursor++) = (uint8_t) ((value >> 24) & 0xff);
return *this;
}
@@ -64,11 +45,11 @@ ByteBuffer &ByteBuffer::write(const ByteBuffer &value) {
}
ByteBuffer &ByteBuffer::write(const uint8_t *bytes, size_t len) {
- checkAndUpdateEnd(len);
+ assertCanAccessRelative(len);
- memcpy(ptr, bytes, len);
+ memcpy(cursor, bytes, len);
- ptr += len;
+ cursor += len;
return *this;
}
@@ -133,29 +114,34 @@ ByteBuffer &ByteBuffer::writeFLOAT(double d) {
uint8_t ByteBuffer::get8(size_t index) const {
assertCanAccessRelative(index);
- return ptr[index];
+ return zero[index];
+}
+
+uint8_t ByteBuffer::peek8(size_t relative_index) const {
+ assertCanAccessRelative(relative_index);
+ return cursor[relative_index];
}
uint8_t ByteBuffer::read8() {
assertCanAccessRelative(0);
- return *ptr++;
+ return *cursor++;
}
uint16_t ByteBuffer::read16le() {
assertCanAccessRelative(1);
uint16_t value;
- value = *ptr++;
- value |= ((uint16_t) *ptr++) << 8;
+ value = *cursor++;
+ value |= ((uint16_t) *cursor++) << 8;
return value;
}
uint32_t ByteBuffer::read32le() {
assertCanAccessRelative(3);
uint32_t value;
- value = *ptr++;
- value |= ((uint32_t) *ptr++) << 8;
- value |= ((uint32_t) *ptr++) << 16;
- value |= ((uint32_t) *ptr++) << 24;
+ value = *cursor++;
+ value |= ((uint32_t) *cursor++) << 8;
+ value |= ((uint32_t) *cursor++) << 16;
+ value |= ((uint32_t) *cursor++) << 24;
return value;
}
@@ -163,7 +149,7 @@ double ByteBuffer::readFLOAT() {
uint32_t data = read32le();
int32_t mantissa = data & 0xFFFFFF;
- int8_t exponent = (int8_t) (data >> 24);
+ auto exponent = static_cast<int8_t>(data >> 24);
double output = 0;
if (mantissa >= FLOAT::positive_infinity &&
@@ -188,38 +174,29 @@ double ByteBuffer::readFLOAT() {
void ByteBuffer::copy(uint8_t *bytes, size_t length) const {
assertCanAccessRelative(length - 1);
- memcpy(bytes, ptr, length);
+ memcpy(bytes, cursor, length);
}
-ByteBuffer ByteBuffer::view() const {
-// DF << "cursor=" << getCursor() << ", size=" << getSize() << ", new size=" << end - ptr << ", ptr=" << (uint64_t) ptr << ", zero=" << (uint64_t) zero;
- return view(ptr, end);
+void ByteBuffer::reset() {
+ cursor = const_cast<uint8_t *>(zero);
}
-ByteBuffer ByteBuffer::view(size_t length) const {
- return ByteBuffer(bytes, length, ptr, ptr + length);
-}
-
-ByteBuffer ByteBuffer::view(uint8_t *ptr, const uint8_t *end) const {
- return ByteBuffer(bytes, end - ptr, ptr, end);
+ByteBuffer ByteBuffer::view() const {
+// DF << "cursor=" << getCursor() << ", size=" << getSize() << ", new size=" << end_ - cursor << ", cursor=" << (uint64_t) cursor << ", zero=" << (uint64_t) zero;
+ return {cursor, getBytesLeft()};
}
-void ByteBuffer::checkAndUpdateEnd(size_t newBytes) {
- uint8_t *newEnd = ptr + newBytes;
- if (newEnd >= end) {
- if (newEnd >= &zero[capacity]) {
- throw ByteBufferException("New size is too large! cursor=" + to_string(getCursor()) + ", size=" + to_string(getSize()) + ", capacity=" + to_string(capacity) + ", new bytes=" + to_string(newBytes));
- }
- end = newEnd;
- }
+ByteBuffer ByteBuffer::view(size_t length) const {
+ assertCanAccessRelative(length - 1);
+ return {cursor, length};
}
void ByteBuffer::assertCanAccessRelative(size_t diff) const {
- assertCanAccessIndex(ptr + diff);
+ assertCanAccessIndex(cursor + diff);
}
void ByteBuffer::assertCanAccessIndex(uint8_t *p) const {
- if (p >= end || p < zero) {
+ if (p >= end_ || p < zero) {
throw ByteBufferException("Out of bounds! size=" + to_string(getSize()) + ", index=" + to_string(p - zero));
}
}
@@ -227,7 +204,7 @@ void ByteBuffer::assertCanAccessIndex(uint8_t *p) const {
std::string ByteBuffer::toString() const {
std::stringstream s;
- for (uint8_t *i = (uint8_t *) zero; i < end; i++) {
+ for (auto *i = zero; i < end_; i++) {
s << hex << setfill('0') << setw(2) << (int) *i << " ";
}