libcommonc++  0.7
CircularBuffer< T > Class Template Reference

A circular buffer that can be used to efficiently transfer data between buffers and/or streams. More...

#include <CircularBuffer.h++>

Inheritance diagram for CircularBuffer< T >:
Collaboration diagram for CircularBuffer< T >:

Public Member Functions

 CircularBuffer (uint_t size)
 Construct a new CircularBuffer with the given size. More...
 
 ~CircularBuffer ()
 Destructor. More...
 
void clear ()
 Clear the buffer. More...
 
void setSize (uint_t newSize)
 Resize the buffer. More...
 
uint_t write (const T *buf, uint_t count)
 Write data from an array into the buffer. More...
 
uint_t write (Buffer< T > &buffer, uint_t count=0)
 Write data from a Buffer into the buffer. More...
 
uint_t write (Stream &stream, uint_t count=0)
 Read data from a stream and write it into the buffer. More...
 
uint_t read (T *buf, uint_t count)
 Read data from the buffer into an array. More...
 
uint_t read (Buffer< T > &buffer, uint_t count=0)
 Read data from the buffer into a Buffer. More...
 
uint_t read (Stream &stream, uint_t count=0)
 Read data from the buffer and write it to a stream. More...
 
uint_t peek (const T &value, uint_t maxlen, bool &found, bool resetPeek=true)
 Scan forward from the current peek position for an element equal to the given value. More...
 
uint_t fill (const T &value, uint_t count)
 Fill the buffer with the given value. More...
 
uint_t getReadExtent () const
 Get the read extent. More...
 
uint_t getWriteExtent () const
 Get the write extent. More...
 
uint_t getRemaining () const
 Get the number of elements available to be read from the buffer. More...
 
uint_t getPeekRemaining () const
 Get the number of elements available to be peeked, that is, the number of elements between the peek position and the write position. More...
 
bool isEmpty () const
 Determine if the buffer is empty. More...
 
bool isFull () const
 Determine if the buffer is full. More...
 
uint_t getFree () const
 Get the number of elements available to be written to the buffer. More...
 
T * getReadPos ()
 Get the current read position. More...
 
T * getWritePos ()
 Get the current write position. More...
 
T * getPeekPos ()
 Get the current peek position. More...
 
T * advanceReadPos (uint_t count)
 Advance the read position by the given number of elements. More...
 
T * advanceWritePos (uint_t count)
 Advance the write position by the given number of elements. More...
 
T * advanceReadPos ()
 Advance the read position by the number of elements in the read extent, wrapping to the beginning of the buffer if necessary. More...
 
T * advanceWritePos ()
 Advance the write position by the number of elements in the write extent, wrapping to the beginning of the buffer if necessary. More...
 
T * advancePeekPos (uint_t count)
 Advance the peek position by the given number of elements, wrapping to the beginning of the buffer if necessary. More...
 
void resetPeekPos ()
 Reset the peek position to the read position. More...
 
bool isPartialWrite () const
 Determine if a partially-written element is in the buffer. More...
 
bool isPartialRead () const
 Determine if a partially-read element is in the buffer. More...
 
void markReadPos ()
 Marks the current read position. More...
 
void rewindReadPos ()
 Rewinds the read position to the read mark, and clears the read mark. More...
 
void markWritePos ()
 Marks the current write position. More...
 
void rewindWritePos ()
 Rewinds the write position to the write mark, and clears the write mark. More...
 
bool setReadPosFromMark (uint_t count)
 Moves the read position relative to the read mark. More...
 
bool setWritePosFromMark (uint_t count)
 Moves the write position relative to the write mark. More...
 
uint_t getSize () const
 Get the size of the buffer. More...
 
virtual bool hasRemaining () const
 Test if the buffer has elements available to be read or written. More...
 
T * getBase ()
 Get a pointer to the base of the buffer. More...
 
const T * getBase () const
 Get a pointer to the base of the buffer. More...
 

Protected Attributes

T * _data
 A pointer to the raw buffer. More...
 
uint_t _size
 The size of the buffer. More...
 
bool _owner
 Whether this object owns the raw buffer. More...
 

Detailed Description

template<typename T>
class ccxx::CircularBuffer< T >

A circular buffer that can be used to efficiently transfer data between buffers and/or streams.

The buffer has a read position and a write position, as well as a peek position. When either the read position or the write position reaches the end of the buffer, it wraps around to the beginning, but the read position can never overtake the write position. The peek position is always between the read position and the write position, and is used to "look ahead" for a specific value.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ CircularBuffer()

Construct a new CircularBuffer with the given size.

Parameters
sizeThe capacity of the buffer, in elements.

◆ ~CircularBuffer()

Destructor.

Member Function Documentation

◆ advancePeekPos()

T* advancePeekPos ( uint_t  count)

Advance the peek position by the given number of elements, wrapping to the beginning of the buffer if necessary.

If there are not enough elements between the current peek position and the write position, the peek position is not moved.

Parameters
countThe number of elements to advance.
Returns
The new peek position.

◆ advanceReadPos() [1/2]

T* advanceReadPos ( uint_t  count)

Advance the read position by the given number of elements.

Parameters
countThe number of elements to advance. If the value is greater than the number of bytes available to be read, the read position is left unchanged.
Returns
The new read position.

◆ advanceReadPos() [2/2]

T* advanceReadPos ( )
inline

Advance the read position by the number of elements in the read extent, wrapping to the beginning of the buffer if necessary.

Returns
The new read position.

◆ advanceWritePos() [1/2]

T* advanceWritePos ( uint_t  count)

Advance the write position by the given number of elements.

Parameters
countThe number of elements to advance. If the value is greater than the number of elements available to be written, the write position is left unchanged.
Returns
The new write position.

◆ advanceWritePos() [2/2]

T* advanceWritePos ( )
inline

Advance the write position by the number of elements in the write extent, wrapping to the beginning of the buffer if necessary.

Returns
The new write position.

◆ clear()

void clear ( )
virtual

Clear the buffer.

Resets the buffer to an "empty" state.

Reimplemented from AbstractBuffer< T >.

◆ fill()

uint_t fill ( const T &  value,
uint_t  count 
)

Fill the buffer with the given value.

Fills the requested number of items. If the requested count exceeds the number of items available to be written, only the available (free) items are filled.

Parameters
valueThe value to fill with.
countThe number of items to fill.
Returns
The number of items actually filled.

◆ getBase() [1/2]

T* getBase ( )
inlineinherited

Get a pointer to the base of the buffer.

◆ getBase() [2/2]

const T* getBase ( ) const
inlineinherited

Get a pointer to the base of the buffer.

◆ getFree()

uint_t getFree ( ) const
inline

Get the number of elements available to be written to the buffer.

◆ getPeekPos()

T* getPeekPos ( )
inline

Get the current peek position.

◆ getPeekRemaining()

uint_t getPeekRemaining ( ) const
inline

Get the number of elements available to be peeked, that is, the number of elements between the peek position and the write position.

◆ getReadExtent()

uint_t getReadExtent ( ) const

Get the read extent.

Returns
The number of contiguous elements that can be read beginning at the current read position, without wrapping to the beginning of the buffer.

◆ getReadPos()

T* getReadPos ( )
inline

Get the current read position.

◆ getRemaining()

uint_t getRemaining ( ) const
inlinevirtual

Get the number of elements available to be read from the buffer.

Implements AbstractBuffer< T >.

◆ getSize()

uint_t getSize ( ) const
inlineinherited

Get the size of the buffer.

Returns
The size, in elements.

◆ getWriteExtent()

uint_t getWriteExtent ( ) const

Get the write extent.

Returns
The number of contiguous elements that can be written beginning at the current write position, without wrapping to the beginning of the buffer.

◆ getWritePos()

T* getWritePos ( )
inline

Get the current write position.

◆ hasRemaining()

virtual bool hasRemaining ( ) const
inlinevirtualinherited

Test if the buffer has elements available to be read or written.

Reimplemented in Buffer< T >, and Buffer< char >.

◆ isEmpty()

bool isEmpty ( ) const
inline

Determine if the buffer is empty.

◆ isFull()

bool isFull ( ) const
inline

Determine if the buffer is full.

◆ isPartialRead()

bool isPartialRead ( ) const
inline

Determine if a partially-read element is in the buffer.

This will always be false when the template parameter is an object of size 1.

◆ isPartialWrite()

bool isPartialWrite ( ) const
inline

Determine if a partially-written element is in the buffer.

This will always be false when the template parameter is an object of size 1.

◆ markReadPos()

void markReadPos ( )

Marks the current read position.

As long as no writes are performed on the buffer, the read position can be rewound to the mark by calling rewindReadPos().

◆ markWritePos()

void markWritePos ( )

Marks the current write position.

As long as no reads are performed on the buffer, the write position can be rewound to the mark by calling rewindWritePos().

◆ peek()

uint_t peek ( const T &  value,
uint_t  maxlen,
bool &  found,
bool  resetPeek = true 
)

Scan forward from the current peek position for an element equal to the given value.

Parameters
valueThe value to scan for.
maxlenThe maximum number of elements to scan.
foundA flag that is set to true if the item was found, and false otherwise.
resetPeekIf true, reset the peek position to the read position before scanning.
Returns
The number of elements between the current read position and the peeked value, inclusive, if found; otherwise, the number of elements scanned (up to maxlen).

◆ read() [1/3]

uint_t read ( T *  buf,
uint_t  count 
)

Read data from the buffer into an array.

Parameters
bufThe base of the array.
countThe number of elements to write to the array.
Returns
The number of elements actually written.

◆ read() [2/3]

uint_t read ( Buffer< T > &  buffer,
uint_t  count = 0 
)

Read data from the buffer into a Buffer.

Parameters
bufferThe buffer to read into.
countThe number of elements to copy to the buffer, or 0 to write as much as possible.
Returns
The number of elements actually written.

◆ read() [3/3]

uint_t read ( Stream stream,
uint_t  count = 0 
)

Read data from the buffer and write it to a stream.

Parameters
streamThe stream to write to.
countThe number of bytes to write to the stream.
Returns
The number of bytes actually written.
Exceptions
IOExceptionIf an I/O error occurs.

◆ resetPeekPos()

void resetPeekPos ( )

Reset the peek position to the read position.

◆ rewindReadPos()

void rewindReadPos ( )

Rewinds the read position to the read mark, and clears the read mark.

If no read mark is set, the method does nothing.

◆ rewindWritePos()

void rewindWritePos ( )

Rewinds the write position to the write mark, and clears the write mark.

If no write mark is set, the method does nothing.

◆ setReadPosFromMark()

bool setReadPosFromMark ( uint_t  count)

Moves the read position relative to the read mark.

Parameters
countThe offset to set the read position to, relative to the read mark.
Returns
true on success, or false if the count is invalid or if no read mark is currently set.

◆ setSize()

void setSize ( uint_t  newSize)
virtual

Resize the buffer.

Parameters
newSizeThe new size, in elements.

Reimplemented from AbstractBuffer< T >.

◆ setWritePosFromMark()

bool setWritePosFromMark ( uint_t  count)

Moves the write position relative to the write mark.

Parameters
countThe offset to set the write position to, relative to the write mark.
Returns
true on success, or false if the count is invalid or if no write mark is currently set.

◆ write() [1/3]

uint_t write ( const T *  buf,
uint_t  count 
)

Write data from an array into the buffer.

Parameters
bufThe base of the array.
countThe number of elements to read from the array.
Returns
The number of elements actually read.

◆ write() [2/3]

uint_t write ( Buffer< T > &  buffer,
uint_t  count = 0 
)

Write data from a Buffer into the buffer.

Parameters
bufferThe buffer of data to write.
countThe number of elements to write from the buffer, or 0 to write as much as possible.
Returns
The number of elements actually read.

◆ write() [3/3]

uint_t write ( Stream stream,
uint_t  count = 0 
)

Read data from a stream and write it into the buffer.

Parameters
streamThe stream to read from.
countThe number of elements to write from the stream.
Returns
The number of elements actually written to the buffer.
Exceptions
IOExceptionIf an I/O error occurs.

Member Data Documentation

◆ _data

T* _data
protectedinherited

A pointer to the raw buffer.

◆ _owner

bool _owner
protectedinherited

Whether this object owns the raw buffer.

◆ _size

uint_t _size
protectedinherited

The size of the buffer.


The documentation for this class was generated from the following file: