libcommonc++
0.7
|
A circular buffer that can be used to efficiently transfer data between buffers and/or streams. More...
#include <CircularBuffer.h++>
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... | |
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.
CircularBuffer | ( | uint_t | size | ) |
Construct a new CircularBuffer with the given size.
size | The capacity of the buffer, in elements. |
~CircularBuffer | ( | ) |
Destructor.
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.
count | The number of elements to advance. |
T* advanceReadPos | ( | uint_t | count | ) |
Advance the read position by the given number of elements.
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. |
|
inline |
Advance the read position by the number of elements in the read extent, wrapping to the beginning of the buffer if necessary.
T* advanceWritePos | ( | uint_t | count | ) |
Advance the write position by the given number of elements.
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. |
|
inline |
Advance the write position by the number of elements in the write extent, wrapping to the beginning of the buffer if necessary.
|
virtual |
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.
value | The value to fill with. |
count | The number of items to fill. |
|
inlineinherited |
Get a pointer to the base of the buffer.
|
inlineinherited |
Get a pointer to the base of the buffer.
|
inline |
Get the number of elements available to be written to the buffer.
|
inline |
Get the current peek position.
|
inline |
Get the number of elements available to be peeked, that is, the number of elements between the peek position and the write position.
uint_t getReadExtent | ( | ) | const |
Get the read extent.
|
inline |
Get the current read position.
|
inlinevirtual |
Get the number of elements available to be read from the buffer.
Implements AbstractBuffer< T >.
|
inlineinherited |
Get the size of the buffer.
uint_t getWriteExtent | ( | ) | const |
Get the write extent.
|
inline |
Get the current write position.
|
inlinevirtualinherited |
Test if the buffer has elements available to be read or written.
Reimplemented in Buffer< T >, and Buffer< char >.
|
inline |
Determine if the buffer is empty.
|
inline |
Determine if the buffer is full.
|
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.
|
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.
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().
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().
Scan forward from the current peek position for an element equal to the given value.
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. |
Read data from the buffer into an array.
buf | The base of the array. |
count | The number of elements to write to the array. |
Read data from the buffer into a Buffer.
buffer | The buffer to read into. |
count | The number of elements to copy to the buffer, or 0 to write as much as possible. |
Read data from the buffer and write it to a stream.
stream | The stream to write to. |
count | The number of bytes to write to the stream. |
IOException | If an I/O error occurs. |
void resetPeekPos | ( | ) |
Reset the peek position to the read position.
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.
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.
bool setReadPosFromMark | ( | uint_t | count | ) |
Moves the read position relative to the read mark.
count | The offset to set the read position to, relative to the read mark. |
true
on success, or false
if the count is invalid or if no read mark is currently set.
|
virtual |
Resize the buffer.
newSize | The new size, in elements. |
Reimplemented from AbstractBuffer< T >.
bool setWritePosFromMark | ( | uint_t | count | ) |
Moves the write position relative to the write mark.
count | The offset to set the write position to, relative to the write mark. |
true
on success, or false
if the count is invalid or if no write mark is currently set. Write data from an array into the buffer.
buf | The base of the array. |
count | The number of elements to read from the array. |
Write data from a Buffer into the buffer.
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. |
Read data from a stream and write it into the buffer.
stream | The stream to read from. |
count | The number of elements to write from the stream. |
IOException | If an I/O error occurs. |
|
protectedinherited |
A pointer to the raw buffer.
|
protectedinherited |
Whether this object owns the raw buffer.
|
protectedinherited |
The size of the buffer.