diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-20 22:56:22 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-20 22:56:22 +0100 |
commit | e44813dddbf5ba063d29ae1e40862e7a7cbb6f43 (patch) | |
tree | 67009d481b8b106af6937a5f386fe4e2e15b1fcc /ByteBuffer.cpp | |
parent | b6f080193d71334e8afea95ae26afbc03c27fac3 (diff) | |
download | ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.tar.gz ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.tar.bz2 ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.tar.xz ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.zip |
Reorganizing the source code:
o Moving main to apps/
o Moving the library sources to ble/
o Creating cmake files for each piece.
Diffstat (limited to 'ByteBuffer.cpp')
-rw-r--r-- | ByteBuffer.cpp | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp deleted file mode 100644 index 820c638..0000000 --- a/ByteBuffer.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "ByteBuffer.h" -#include <string.h> -#include <sstream> -#include <iomanip> - -using namespace std; - -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; -} - -ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t zero, size_t size) : - bytes(bytes), capacity(capacity), zero(&bytes.get()[zero]), end(&bytes.get()[size]) { - assert(zero <= size); - assert(size <= capacity); - ptr = (uint8_t *) zero; -} - -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::write8(uint8_t value) { - checkAndUpdateEnd(1); - (*ptr++) = value; - return *this; -} - -ByteBuffer &ByteBuffer::write16le(uint16_t value) { - checkAndUpdateEnd(2); - (*ptr++) = (uint8_t) (value & 0xff); - (*ptr++) = (uint8_t) ((value >> 8) & 0xff); - return *this; -} - -uint8_t ByteBuffer::get8(size_t index) const { - assertCanAccessRelative(index); - return ptr[index]; -} - -uint8_t ByteBuffer::read8() { - assertCanAccessRelative(0); - return *ptr++; -} - -uint16_t ByteBuffer::read16le() { - assertCanAccessRelative(0); - uint16_t value; - value = *ptr++; - value |= ((uint16_t) *ptr++) << 8; - return value; -} - -void ByteBuffer::copy(uint8_t *bytes, size_t length) const { - assertCanAccessRelative(length - 1); - - memcpy(bytes, ptr, 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); -} - -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); -} - -void ByteBuffer::checkAndUpdateEnd(size_t newBytes) { - uint8_t *newEnd = ptr + newBytes; - if (newEnd >= end) { - if (newEnd >= &zero[capacity]) { - throw ByteBufferException(string("New size is too large! cursor=") + to_string(getCursor()) + ", size=" + to_string(getSize()) + ", capacity=" + to_string(capacity) + ", new bytes=" + to_string(newBytes)); - } - end = newEnd; - } -} - -void ByteBuffer::assertCanAccessRelative(size_t diff) const { - assertCanAccessIndex(ptr + diff); -} - -void ByteBuffer::assertCanAccessIndex(uint8_t *p) const { - if (p >= end || p < zero) { - throw ByteBufferException(string("Out of bounds! size=") + to_string(getSize()) + ", index=" + to_string(p - zero)); - } -} - -std::string ByteBuffer::toString() const { - stringstream s; - - for (uint8_t *i = (uint8_t *) zero; i < end; i++) { - s << hex << setfill('0') << setw(2) << (int) *i << " "; - } - - return string(s.str()); -} |