Generic buffer of bytes which resizes itself when needed. More...
#include <UTBuffer.h>
Public Member Functions | |
Buffer_t () | |
Buffer_t (byte *stack_buffer, int stack_buffer_size) | |
~Buffer_t () | |
byte * | Buffer (int size, bool expect_more_growth=false) |
void | InstallStackBuffer (byte *stack_buffer, int stack_buffer_size) |
const byte * | ReadOnlyBuffer () const |
byte & | operator[] (int index) |
byte | ByteAt (int index) const |
int | ValidSize () const |
void | SetValidSize (int size) |
int | AllocatedSize () const |
void | GrowIfNeeded (int min_allocated_bytes, bool expect_more_growth=false) |
Generic buffer which manages optionally utilizing a stack buffer, but extending to dynamic memory allocation when necessary. The object also keeps track of what portion of the allocated buffer is considered valid, by having been explitly allocated by a call to the Buffer function, or by having been accessed by operator []. In general, operator [] can simply be used, and the object will be reasonably efficient in the frequency and size of its allocations. The Buffer function can be used to preallocate a size if the size which will be required for a pending operation or a reasonable approximation is known in advance.
Definition at line 36 of file UTBuffer.h.
Buffer_t::Buffer_t | ( | ) | [inline] |
Constructor for an empty buffer.
Definition at line 231 of file UTBuffer.h.
Buffer_t::Buffer_t | ( | byte * | stack_buffer, | |
int | stack_buffer_size | |||
) | [inline] |
Constructor for a buffer which initially uses a stack-allocated buffer of the specified size. If the required size grows beyond that size, the buffer will be reallocated from the heap.
Definition at line 241 of file UTBuffer.h.
Buffer_t::~Buffer_t | ( | ) | [inline] |
Destructor. The internal buffer will be deleted unless a stack buffer was provided in the constructor and the buffer never grew to the point of having been reallocated from the heap.
Definition at line 251 of file UTBuffer.h.
byte * Buffer_t::Buffer | ( | int | size, | |
bool | expect_more_growth = false | |||
) | [inline] |
Returns a pointer to the internal buffer if the currently allocated size is sufficient to satisfy the request, or reallocates the internal buffer to provide a pointer to a buffer of the requested size. If the new buffer is reallocated, the old contents are copied into the new internal buffer. Subsequent calls to ValidSize will return size unless the valid size was already larger than size.
Definition at line 259 of file UTBuffer.h.
void Buffer_t::InstallStackBuffer | ( | byte * | stack_buffer, | |
int | stack_buffer_size | |||
) | [inline] |
Installs a stack buffer into an empty but already constructed Buffer_t.
Definition at line 270 of file UTBuffer.h.
const byte * Buffer_t::ReadOnlyBuffer | ( | ) | const [inline] |
Returns a const pointer to the internal buffer. Only the portion up to the size indicated by the ValidSize function is actually valid. This function is provided for direct access to the internal buffer while dealing with const instances.
Definition at line 282 of file UTBuffer.h.
byte & Buffer_t::operator[] | ( | int | index | ) | [inline] |
Accessor to read or write the byte at the specified index. The internal buffer will grow if needed. If the internal buffer does need to grow, it will grow beyond index to make incremental access by operator [] more efficient. To grow the buffer to an exact size, use the Buffer function instead.
Reimplemented in SimpleTypedBuffer_t< T >.
Definition at line 289 of file UTBuffer.h.
byte Buffer_t::ByteAt | ( | int | index | ) | const [inline] |
Returns the value of the byte at the specified index. The index must be within the size considered valid by virtue of having been explicitly requested by the Buffer function, or having been accessed by operator [].
Definition at line 300 of file UTBuffer.h.
int Buffer_t::ValidSize | ( | ) | const [inline] |
Returns the size of the buffer considered to be valid by virtue of having been explicitly requested by the Buffer function, or having been accessed by operator [].
Definition at line 308 of file UTBuffer.h.
void Buffer_t::SetValidSize | ( | int | size | ) | [inline] |
Used for optimization of subsequent requests for a buffer of a larger size to ensure that if reallocation is needed, it will only copy what is needed. Note that this is typically unnecessary, since the Buffer function and operator [] keep track of what data has been touched. Optimization with SetValidSize would only be useful if the Buffer function had been used to preallocate a conservative size, not all of which proved to be necessary at the conclusion of the operation which necessitated operating on the buffer.
Definition at line 315 of file UTBuffer.h.
int Buffer_t::AllocatedSize | ( | ) | const [inline] |
Returns the size of the buffer which is actually allocated.
Definition at line 323 of file UTBuffer.h.
void Buffer_t::GrowIfNeeded | ( | int | min_allocated_bytes, | |
bool | expect_more_growth = false | |||
) | [inline] |
Increases the size of the buffer if needed, reallocating from the heap. This does not affect the number of bytes considered to be valid.
Reimplemented in SimpleTypedBuffer_t< T >.
Definition at line 330 of file UTBuffer.h.