C12Adapter Opensource C++ Interface
|
Byte buffer with variable capacity that allows efficient buffering by having one reader/getter and one writer/putter. More...
Public Types | |
enum | { DEFAULT_INITIAL_CAPACITY = 1024 } |
Public Member Functions | |
MBufferCircular (unsigned initialCapacity=DEFAULT_INITIAL_CAPACITY) | |
Create buffer of a given initial capacity. More... | |
~MBufferCircular () | |
Circular buffer destructor. | |
unsigned | GetSize () const |
Number of bytes buffered, available for getting. | |
unsigned | CanPutWithoutResize () const |
How many bytes can be put into circular buffer without necessity to reallocate buffer. More... | |
void | Clear () |
Clear the contents of the buffer so the size becomes zero. | |
void | Resize (unsigned newCapacity) |
Resize the buffer to given capacity. More... | |
void | Put (const char *buff, unsigned size) |
Put the whole given buffer, grow object capacity if necessary. More... | |
unsigned | Get (char *buff, unsigned size) |
Get the data given chunk from the circular buffer. More... | |
Byte buffer with variable capacity that allows efficient buffering by having one reader/getter and one writer/putter.
This is a slightly more efficient std::deque, which lacks methods to pop chunks of bytes. Grows as necessary to accommodate putting of any number of bytes. However, of course, one cannot get more bytes than available in the buffer. The class is low level and it does not throw exceptions as any would be a program error. Synchronization has to be provided outside.
MBufferCircular::MBufferCircular | ( | unsigned | initialCapacity = DEFAULT_INITIAL_CAPACITY | ) |
Create buffer of a given initial capacity.
initialCapacity | Byte size of the initial buffer, will grow if necessary. Should be more than 2, typical is a power of 2 like 512. The default initial capacity is 1024. |
|
inline |
How many bytes can be put into circular buffer without necessity to reallocate buffer.
This method is rarely needed as the buffer is reallocated at necessity.
unsigned MBufferCircular::Get | ( | char * | buff, |
unsigned | size | ||
) |
Get the data given chunk from the circular buffer.
buff | Buffer where to get the data. |
size | Size of the chunk. Size and offset should fit within the buffer length. |
void MBufferCircular::Put | ( | const char * | buff, |
unsigned | size | ||
) |
Put the whole given buffer, grow object capacity if necessary.
buff | Buffer where the chunk is located. |
size | Size of the chunk. |
void MBufferCircular::Resize | ( | unsigned | newCapacity | ) |
Resize the buffer to given capacity.
Note that the real amount of bytes that can be put into the buffer is one less than its capacity. In most cases there is no need for this method to be called as the growth happens transparently when necessary.
newCapacity | New capacity of the buffer. |