19 #ifndef _ByteBuffer_H_ 20 #define _ByteBuffer_H_ 23 #define DEFAULT_SIZE 4096 50 std::vector<uint8_t> buf;
56 template <
typename T> T read() {
57 T data = read<T>(rpos);
62 template <
typename T> T read(uint32_t index)
const {
63 if(index +
sizeof(T) <= buf.size())
64 return *((T*)&buf[index]);
68 template <
typename T>
void append(T data) {
69 uint32_t s =
sizeof(data);
71 if (
size() < (wpos + s))
73 memcpy(&buf[wpos], (uint8_t*)&data, s);
79 template <
typename T>
void insert(T data, uint32_t index) {
80 if((index +
sizeof(data)) >
size())
83 memcpy(&buf[index], (uint8_t*)&data,
sizeof(data));
84 wpos = index+
sizeof(data);
97 void resize(uint32_t newSize);
101 void reserve(uint32_t newCapacity);
104 template <
typename T> int32_t find(T key, uint32_t start=0) {
106 uint32_t len = buf.size();
107 for(uint32_t i = start; i < len; i++) {
110 if((key != 0) && (data == 0))
123 void replace(uint8_t key, uint8_t rep, uint32_t start = 0,
bool firstOccuranceOnly=
false);
129 uint8_t
get(uint32_t index);
130 void getBytes(uint8_t* buf, uint32_t len);
132 char getChar(uint32_t index);
134 double getDouble(uint32_t index);
136 float getFloat(uint32_t index);
138 uint32_t getInt(uint32_t index);
140 uint64_t getLong(uint32_t index);
142 uint16_t getShort(uint32_t index);
148 void put(uint8_t b, uint32_t index);
149 void putBytes(uint8_t* b, uint32_t len);
150 void putBytes(uint8_t* b, uint32_t len, uint32_t index);
151 void putChar(
char value);
152 void putChar(
char value, uint32_t index);
153 void putDouble(
double value);
154 void putDouble(
double value, uint32_t index);
155 void putFloat(
float value);
156 void putFloat(
float value, uint32_t index);
157 void putInt(uint32_t value);
158 void putInt(uint32_t value, uint32_t index);
159 void putLong(uint64_t value);
160 void putLong(uint64_t value, uint32_t index);
161 void putShort(uint16_t value);
162 void putShort(uint16_t value, uint32_t index);
166 void setReadPos(uint32_t r) {
170 uint32_t getReadPos() {
174 void setWritePos(uint32_t w) {
178 uint32_t getWritePos() {
184 void setName(std::string n);
185 std::string getName();
190 void printPosition();
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
ByteBuffer * clone()
Clone Allocate an exact copy of the ByteBuffer on the heap and return a pointer.
Definition: ByteBuffer.cpp:91
void resize(uint32_t newSize)
Resize Reallocates memory for the internal buffer of size newSize.
Definition: ByteBuffer.cpp:134
void clear()
Clear Clears out all data from the internal vector (original preallocated size remains), resets the positions to 0.
Definition: ByteBuffer.cpp:79
ByteBuffer(uint32_t size=DEFAULT_SIZE)
ByteBuffer constructor Reserves specified size in internal vector.
Definition: ByteBuffer.cpp:27
Definition: ByteBuffer.h:47
bool equals(ByteBuffer *other)
Equals, test for data equivilancy Compare this ByteBuffer to another by looking at each byte in the i...
Definition: ByteBuffer.cpp:113
uint32_t bytesRemaining()
Bytes Remaining Returns the number of bytes from the current read position till the end of the buffer...
Definition: ByteBuffer.cpp:71
Definition: enum_maker.hpp:46
uint32_t size()
Size Returns the size of the internal buffer...not necessarily the length of bytes used as data! ...
Definition: ByteBuffer.cpp:146
void replace(uint8_t key, uint8_t rep, uint32_t start=0, bool firstOccuranceOnly=false)
Replace Replace occurance of a particular uint8_t, key, with the uint8_t rep.
Definition: ByteBuffer.cpp:170
virtual ~ByteBuffer()
ByteBuffer Deconstructor.
Definition: ByteBuffer.cpp:62