diff options
Diffstat (limited to 'ByteBuffer.cpp')
-rw-r--r-- | ByteBuffer.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp new file mode 100644 index 0000000..9714c8c --- /dev/null +++ b/ByteBuffer.cpp @@ -0,0 +1,38 @@ +#include "ByteBuffer.h" +#include <stdexcept> + +using namespace std; + +ByteBuffer::ByteBuffer(uint8_t *bytes, ssize_t size, ssize_t zero) : + bytes(bytes), size(size), ptr(zero), zero(zero) { +} + +ByteBuffer &ByteBuffer::add8(uint8_t value) { + canAccessNextBytes(1); + bytes[zero + ptr++] = value; + return *this; +} + +ByteBuffer &ByteBuffer::add16le(uint16_t value) { + canAccessNextBytes(2); + bytes[zero + ptr++] = (uint8_t) (value & 0xff); + bytes[zero + ptr++] = (uint8_t) ((value >> 8) & 0xff); + return *this; +} + +uint8_t ByteBuffer::get8(ssize_t index) { + canAccessIndex(index); + return bytes[zero + ptr]; +} + +void ByteBuffer::canAccessNextBytes(ssize_t newBytes) { + if (zero + ptr + newBytes >= size) { + throw exception(); + } +} + +void ByteBuffer::canAccessIndex(ssize_t index) { + if (zero + index >= size) { + throw exception(); + } +} |