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

Inheritance graph
[legend]
Collaboration diagram for CircularBuffer< T >:

Collaboration graph
[legend]

List of all members.

Public Member Functions

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

Protected Attributes

T * _data
 A pointer to the raw buffer.
size_t _size
 The size of the buffer.


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 ( size_t  size  )  [inline]

Construct a new CircularBuffer with the given size.

Parameters:
size The capacity of the buffer, in elements.

~CircularBuffer (  )  throw () [inline]

Destructor.


Member Function Documentation

T * advancePeekPos ( size_t  count  )  throw () [inline]

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:
count The number of elements to advance.
Returns:
The new peek position.

T* advanceReadPos (  )  throw () [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.

T * advanceReadPos ( size_t  count  )  throw () [inline]

Advance the read position by the given number of elements.

Parameters:
count The 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.

T* advanceWritePos (  )  throw () [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.

T * advanceWritePos ( size_t  count  )  throw () [inline]

Advance the write position by the given number of elements.

Parameters:
count The 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.

void clear (  )  throw () [inline, virtual]

Clear the buffer.

Resets the buffer to an "empty" state.

Reimplemented from AbstractBuffer< T >.

size_t fill ( const T &  value,
size_t  count 
) throw () [inline]

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:
value The value to fill with.
count The number of items to fill.
Returns:
The number of items actually filled.

const T * getBase (  )  const throw () [inline, inherited]

Get a pointer to the base of the buffer.

T * getBase (  )  throw () [inline, inherited]

Get a pointer to the base of the buffer.

size_t getFree (  )  const throw () [inline]

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

T* getPeekPos (  )  throw () [inline]

Get the current peek position.

size_t getPeekRemaining (  )  const throw () [inline]

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

size_t getReadExtent (  )  const throw () [inline]

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.

T* getReadPos (  )  throw () [inline]

Get the current read position.

size_t getRemaining (  )  const throw () [inline, virtual]

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

Implements AbstractBuffer< T >.

size_t getSize (  )  const throw () [inline, inherited]

Get the size of the buffer.

Returns:
The size, in elements.

size_t getWriteExtent (  )  const throw () [inline]

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.

T* getWritePos (  )  throw () [inline]

Get the current write position.

virtual bool hasRemaining (  )  const throw () [inline, virtual, inherited]

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

Reimplemented in Buffer< T >.

bool isEmpty (  )  const throw () [inline]

Determine if the buffer is empty.

bool isFull (  )  const throw () [inline]

Determine if the buffer is full.

bool isPartialRead (  )  const throw () [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.

bool isPartialWrite (  )  const throw () [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.

size_t peek ( const T &  value,
size_t  maxlen,
bool &  found,
bool  resetPeek = true 
) throw () [inline]

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

Parameters:
value The value to scan for.
maxlen The maximum number of elements to scan.
found A flag that is set to true if the item was found, and false otherwise.
resetPeek If 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).

size_t read ( Stream stream,
size_t  count = 0 
) throw (IOException) [inline]

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

Parameters:
stream The stream to write to.
count The number of bytes to write to the stream.
Returns:
The number of bytes actually written.
Exceptions:
commoncpp::IOException If an I/O error occurs.

size_t read ( Buffer< T > &  buffer,
size_t  count = 0 
) throw () [inline]

Read data from the buffer into a Buffer.

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

size_t read ( T *  buf,
size_t  count 
) throw () [inline]

Read data from the buffer into an array.

Parameters:
buf The base of the array.
count The number of elements to write to the array.
Returns:
The number of elements actually written.

void resetPeekPos (  )  throw () [inline]

Reset the peek position to the read position.

void setSize ( size_t  newSize  )  [inline, virtual]

Resize the buffer.

Parameters:
newSize The new size, in elements.

Reimplemented from AbstractBuffer< T >.

size_t write ( Stream stream,
size_t  count = 0 
) throw (IOException) [inline]

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

Parameters:
stream The stream to read from.
count The number of elements to write from the stream.
Returns:
The number of elements actually written to the buffer.
Exceptions:
commoncpp::IOException If an I/O error occurs.

size_t write ( Buffer< T > &  buffer,
size_t  count = 0 
) throw () [inline]

Write data from a Buffer into the buffer.

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

size_t write ( const T *  buf,
size_t  count 
) throw () [inline]

Write data from an array into the buffer.

Parameters:
buf The base of the array.
count The number of elements to read from the array.
Returns:
The number of elements actually read.


Member Data Documentation

T * _data [protected, inherited]

A pointer to the raw buffer.

size_t _size [protected, inherited]

The size of the buffer.


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

Generated on Sat Apr 17 23:03:08 2010 for libcommonc++ by  doxygen 1.5.9