C12Adapter Opensource C++ Interface
MBufferCircular Class Reference

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...
 

Detailed Description

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.

Member Enumeration Documentation

anonymous enum
Enumerator
DEFAULT_INITIAL_CAPACITY 

Default initial capacity of the buffer.

Constructor & Destructor Documentation

MBufferCircular::MBufferCircular ( unsigned  initialCapacity = DEFAULT_INITIAL_CAPACITY)

Create buffer of a given initial capacity.

Parameters
initialCapacityByte 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.

Member Function Documentation

unsigned MBufferCircular::CanPutWithoutResize ( ) const
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.

Parameters
buffBuffer where to get the data.
sizeSize of the chunk. Size and offset should fit within the buffer length.
Returns
Actual number of bytes got, could be zero if the buffer is empty.
void MBufferCircular::Put ( const char *  buff,
unsigned  size 
)

Put the whole given buffer, grow object capacity if necessary.

Parameters
buffBuffer where the chunk is located.
sizeSize 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.

Parameters
newCapacityNew capacity of the buffer.