81 std::shared_ptr<unsigned char> bufferRef;
82 unsigned char* buffer =
nullptr;
87 inline void check(
size_t count)
const 89 if ((mposition + count) > mlimit)
90 throw BufferException(
"Buffer buffer overflow: Cannot add more data to the Buffer's buffer.");
111 inline Buffer(
void* buffer_,
size_t bufferSize) : buffer((unsigned char*)buffer_), mcapacity(bufferSize)
122 inline explicit Buffer(
size_t bufferSize) : buffer(bufferSize ? new unsigned char[bufferSize] : NULL), mcapacity(bufferSize)
125 bufferRef.reset(buffer, std::default_delete<
unsigned char[]>());
147 bufferRef = buf.bufferRef;
148 mcapacity = buf.mcapacity;
153 inline Buffer& allocate(
size_t bufferSize)
155 buffer = bufferSize ?
new unsigned char[bufferSize] : NULL;
156 bufferRef.reset(buffer, std::default_delete<
unsigned char[]>());
157 mcapacity = bufferSize;
178 inline Buffer&
flip() { mlimit = mposition; mposition = 0;
return *
this; }
194 inline Buffer&
clear() { mlimit = mcapacity; mposition = 0;
return *
this; }
207 inline size_t remaining()
const {
return mlimit - mposition; }
209 inline Buffer& put(
const void* src,
size_t bytes)
210 {
check(bytes); memcpy( buffer + mposition, src, bytes); mposition += bytes;
return *
this; }
211 inline Buffer&
get(
void* dest,
size_t bytes)
212 {
check(bytes); memcpy( dest, buffer + mposition, bytes); mposition += bytes;
return *
this; }
214 inline unsigned char* data()
const {
return buffer; }
215 inline unsigned char* curPosition()
const {
return buffer + mposition; }
216 inline Buffer& setPosition(
size_t position) { mposition = position;
return *
this; }
217 inline Buffer& forward(
size_t positionIncrement)
218 {
check(positionIncrement); mposition += positionIncrement;
return *
this; }
220 inline size_t limit()
const {
return mlimit; }
221 inline size_t capacity()
const {
return mcapacity; }
222 inline size_t position()
const {
return mposition; }
224 #define DEFAULTBUFFERRELATIVERW(name,type) \ 225 inline Buffer& put##name(const type & val) { return put(&val, sizeof(type)); } \ 226 inline type get##name() { type ret; get(&ret, sizeof(type)); return ret; } 228 DEFAULTBUFFERRELATIVERW(Bool,
bool);
229 DEFAULTBUFFERRELATIVERW(Int,
int);
230 DEFAULTBUFFERRELATIVERW(Char,
char);
231 DEFAULTBUFFERRELATIVERW(Long,
long);
232 DEFAULTBUFFERRELATIVERW(Float,
float);
233 DEFAULTBUFFERRELATIVERW(Double,
double);
234 DEFAULTBUFFERRELATIVERW(
Pointer,
void*);
235 DEFAULTBUFFERRELATIVERW(LongLong,
long long);
236 #undef DEFAULTBUFFERRELATIVERW 238 inline Buffer& putString(
const char* str) {
size_t len = strlen(str) + 1;
check(len); put(str, len);
return (*
this); }
239 inline Buffer& putString(
const std::string& str) {
size_t len = str.length() + 1;
check(len); put(str.c_str(), len);
return (*
this); }
241 inline std::string getString() { std::string ret((
const char*)(buffer + mposition));
size_t len = ret.length() + 1;
check(len); mposition += len;
return ret; }
242 inline std::string getString(
size_t length)
245 std::string ret((
const char*)(buffer + mposition),length);
249 inline char* getCharPointerDirect() {
char* ret = (
char*)(buffer + mposition);
size_t len = strlen(ret) + 1;
check(len); mposition += len;
return ret; }
Buffer & operator=(const Buffer &buf)
Copy another buffer.
Definition: Buffer.h:144
size_t remaining() const
This method provides for the remaining number of bytes that can be read out of the buffer or written ...
Definition: Buffer.h:207
Buffer & flip()
Flips this buffer.
Definition: Buffer.h:178
Buffer & rewind()
This method resets the position to the beginning of the buffer so that it can be either reread or wri...
Definition: Buffer.h:200
Buffer(void *buffer_, size_t bufferSize)
Construct a buffer given an externally managed memory buffer.
Definition: Buffer.h:111
Buffer(size_t bufferSize)
Construct a buffer buffer using the size buffer provided.
Definition: Buffer.h:122
This class is based on the java java.nio.Buffer class however, it does not implement the 'mark' funct...
Definition: Buffer.h:79
A class representing a touch pointer interaction consisting of an down touch, the last touch and the ...
Definition: TouchTypes.h:49
#define check(A)
disappears in normal production mode
Definition: bigint.c:90
Buffer()
Construct an uninitialized buffer instance, perhaps as an lvalue.
Definition: Buffer.h:97
Buffer & clear()
Clears this buffer.
Definition: Buffer.h:194