Generic container-like circular buffer class. More...
#include <CircularBuffer.hxx>
Public Member Functions | |
CircularBuffer () | |
Default constructor. | |
CircularBuffer (TSize bufferSize) | |
Constructor which initializes the buffer to a certain capacity. | |
TSize | GetBufferSize () const |
Returns the circular buffer's buffer size. | |
void | SetBufferSize (TSize size) |
Resizes the buffer to a new capacity (or buffer size). | |
TSize | GetWriteIndex () const |
Returns the index of the element that the buffer is about to write (ie. | |
TSize | GetReadIndex () const |
Returns the index of the element that the buffer is about to read (ie. | |
TSize | GetInitialReadOffset () const |
Returns the initial read offset. | |
void | SetInitialReadOffset (TSize size) |
Sets the initial read offset. | |
TSize | GetInitialWriteOffset () const |
Returns the initial write offset. | |
void | SetInitialWriteOffset (TSize size) |
Sets the initial write offset. | |
void | Init () |
Initializes the buffer by setting all elements to 0 and making sure read/write indices are set correctly (taking into account initial read/write offsets). | |
void | SetBufferToZero () |
Sets all elements in circular buffer to zero. | |
void | InitPointers () |
Initializes read/write indices (taking into account initial read/write offsets). | |
void | Read (T &element) |
Reads a single element at the current read index into element. | |
void | NonCopyRead (Array< T > &buffer) |
void | Read (T *buffer) |
Reads read size number of elements starting at the current read index into buffer. | |
void | Read (Array< T > &in, TSize offset=0) |
Reads read size number of elements starting at the current read index into the array at the specified offset (default: offset = 0). | |
void | Write (const T &element) |
Writes a single element at the current write index. | |
void | Write (const T *buffer) |
Writes write size number of elements starting at the current write index from buffer. | |
void | Write (const Array< T > &in, TSize offset=0) |
Writes write size number of elements starting at the current write index from the array starting at the specified offset (default: offset = 0). | |
void | Add (const T &elem) |
Does an additive write of a single element at the current write index. | |
void | Add (const Array< T > &in, TSize offset=0) |
Does an additive write of write size number of elements at the current write index onwards from the specified input array starting at the specified offset (default: offset = 0) onwards. | |
void | IncreaseReadIndex (TSize step=1) |
Increases the read index by the specified number of elements (default: step = 1). | |
void | IncreaseWriteIndex (TSize step=1) |
Increases the write index by the specified number of elements (default: step = 1). | |
void | DecreaseReadIndex (TSize step=1) |
Decreases the read index by the specified number of elements (default: step = 1). | |
void | DecreaseWriteIndex (TSize step=1) |
Decreases the write index by the specified number of elements (default: step = 1). | |
T & | GetPtrToElement (int absPos) |
Returns a pointer (well, reference) to the element at the specified index. | |
TSize | GetReadSize () const |
Returns the current read size. | |
TSize | GetWriteSize () const |
Returns the current write size. | |
void | SetReadSize (TSize size) |
Sets the read size. | |
void | SetWriteSize (TSize size) |
Sets the write size. | |
Protected Attributes | |
Array< T > | mBuffer |
TSize | mLogicalSize |
TSize | mReadSize |
TSize | mWriteSize |
TSize | mWriteIndex |
TSize | mReadIndex |
TSize | mInitialReadOffset |
TSize | mInitialWriteOffset |
Generic container-like circular buffer class.
It can be pre-allocated to a certain capacity and resized at any time (not while doing concurrent reading/writing). This circular buffer is not thread-safe. You can either read/write single elements at a time or read/write in blocks. The size of these blocks is specified the read size and write size respectively. The circular buffer must be initialized (by calling Init()) prior to being used. An initial read offset can be specified which indicates the index at which the buffer starts reading after it's been initialized (default: 0).
Definition at line 46 of file CircularBuffer.hxx.
CLAM::CircularBuffer< T >::CircularBuffer | ( | ) | [inline] |
Default constructor.
Read and write indices will be initialized to 0. Read and write sizes will be initialized to 1. Initial read and write offsets will be initialized to 0. The buffer size will be 0.
Definition at line 66 of file CircularBuffer.hxx.
CLAM::CircularBuffer< T >::CircularBuffer | ( | TSize | bufferSize | ) | [inline] |
Constructor which initializes the buffer to a certain capacity.
This capacity (or buffer size) can be arbitrary (not power-of-two, etc), but must be greater than 0.
Definition at line 82 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Add | ( | const Array< T > & | in, | |
TSize | offset = 0 | |||
) | [inline] |
Does an additive write of write size number of elements at the current write index onwards from the specified input array starting at the specified offset (default: offset = 0) onwards.
Note: not very efficient.
Definition at line 360 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Add | ( | const T & | elem | ) | [inline] |
Does an additive write of a single element at the current write index.
It 'mixes' the old element's content with the new one.
Definition at line 347 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Add().
void CLAM::CircularBuffer< T >::DecreaseReadIndex | ( | TSize | step = 1 |
) | [inline] |
Decreases the read index by the specified number of elements (default: step = 1).
Definition at line 397 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::DecreaseWriteIndex | ( | TSize | step = 1 |
) | [inline] |
Decreases the write index by the specified number of elements (default: step = 1).
Definition at line 411 of file CircularBuffer.hxx.
TSize CLAM::CircularBuffer< T >::GetBufferSize | ( | ) | const [inline] |
Returns the circular buffer's buffer size.
Definition at line 99 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::DecreaseReadIndex(), CLAM::CircularBuffer< TData >::DecreaseWriteIndex(), CLAM::CircularBuffer< TData >::GetPtrToElement(), CLAM::CircularBuffer< TData >::IncreaseReadIndex(), CLAM::CircularBuffer< TData >::IncreaseWriteIndex(), CLAM::CircularBuffer< TData >::InitPointers(), CLAM::CircularBuffer< TData >::NonCopyRead(), CLAM::CircularBuffer< TData >::Read(), CLAM::CircularBuffer< TData >::SetBufferToZero(), CLAM::CircularBuffer< TData >::SetInitialReadOffset(), CLAM::CircularBuffer< TData >::SetInitialWriteOffset(), CLAM::CircularBuffer< TData >::SetReadSize(), CLAM::CircularBuffer< TData >::SetWriteSize(), and CLAM::CircularBuffer< TData >::Write().
TSize CLAM::CircularBuffer< T >::GetInitialReadOffset | ( | ) | const [inline] |
Returns the initial read offset.
That is, the index at which the buffer starts reading after it has been initialized.
Definition at line 139 of file CircularBuffer.hxx.
TSize CLAM::CircularBuffer< T >::GetInitialWriteOffset | ( | ) | const [inline] |
Returns the initial write offset.
That is, the index at which the buffer starts writeing after it has been initialized.
Definition at line 162 of file CircularBuffer.hxx.
T& CLAM::CircularBuffer< T >::GetPtrToElement | ( | int | absPos | ) | [inline] |
Returns a pointer (well, reference) to the element at the specified index.
If the specified index is out of bounds it will 'wrap-around'.
Definition at line 426 of file CircularBuffer.hxx.
TSize CLAM::CircularBuffer< T >::GetReadIndex | ( | ) | const [inline] |
Returns the index of the element that the buffer is about to read (ie.
on the next read-like call).
Definition at line 129 of file CircularBuffer.hxx.
TSize CLAM::CircularBuffer< T >::GetReadSize | ( | ) | const [inline] |
Returns the current read size.
The read size determines how many elements will read at once when doing 'chunk' reads.
Definition at line 440 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::GetBufferSize(), CLAM::CircularBuffer< TData >::Read(), and CLAM::CircularBuffer< TData >::SetBufferSize().
TSize CLAM::CircularBuffer< T >::GetWriteIndex | ( | ) | const [inline] |
Returns the index of the element that the buffer is about to write (ie.
on the next write-like call).
Definition at line 120 of file CircularBuffer.hxx.
TSize CLAM::CircularBuffer< T >::GetWriteSize | ( | ) | const [inline] |
Returns the current write size.
The write size determines how many elements will be written at once when doing 'chunk' writes.
Definition at line 450 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Add(), and CLAM::CircularBuffer< TData >::Write().
void CLAM::CircularBuffer< T >::IncreaseReadIndex | ( | TSize | step = 1 |
) | [inline] |
Increases the read index by the specified number of elements (default: step = 1).
Definition at line 372 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::NonCopyRead(), and CLAM::CircularBuffer< TData >::Read().
void CLAM::CircularBuffer< T >::IncreaseWriteIndex | ( | TSize | step = 1 |
) | [inline] |
Increases the write index by the specified number of elements (default: step = 1).
Definition at line 384 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Add(), and CLAM::CircularBuffer< TData >::Write().
void CLAM::CircularBuffer< T >::Init | ( | ) | [inline] |
Initializes the buffer by setting all elements to 0 and making sure read/write indices are set correctly (taking into account initial read/write offsets).
Definition at line 187 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::InitPointers | ( | ) | [inline] |
Initializes read/write indices (taking into account initial read/write offsets).
Definition at line 207 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Init(), and CLAM::CircularBuffer< TData >::SetBufferSize().
void CLAM::CircularBuffer< T >::NonCopyRead | ( | Array< T > & | buffer | ) | [inline] |
Definition at line 233 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Read | ( | Array< T > & | in, | |
TSize | offset = 0 | |||
) | [inline] |
Reads read size number of elements starting at the current read index into the array at the specified offset (default: offset = 0).
The destination array must be large enough to hold read size number of elements at the specified offset. If reading is out of bounds it will wrap-around. Note: define CLAM_HIGH_OPTIMIZATIONS for this function to be efficient.
Definition at line 280 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Read | ( | T * | buffer | ) | [inline] |
Reads read size number of elements starting at the current read index into buffer.
If the read size + read index is out of bounds (ie. past the end of the buffer) the reading will 'wrap-around'.
Definition at line 253 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Read | ( | T & | element | ) | [inline] |
Reads a single element at the current read index into element.
Definition at line 226 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Read().
void CLAM::CircularBuffer< T >::SetBufferSize | ( | TSize | size | ) | [inline] |
Resizes the buffer to a new capacity (or buffer size).
This capacity (or buffer size) can be arbitrary (not power-of-two, etc). If size is set to 0 or less the buffer will be deallocated.
Definition at line 109 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::CircularBuffer(), and CLAM::CircularBuffer< TData >::SetReadSize().
void CLAM::CircularBuffer< T >::SetBufferToZero | ( | ) | [inline] |
Sets all elements in circular buffer to zero.
Definition at line 196 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Init().
void CLAM::CircularBuffer< T >::SetInitialReadOffset | ( | TSize | size | ) | [inline] |
Sets the initial read offset.
That is, the index at which the buffer starts reading after it has been initialized. Init() or InitPointers() should be called after this method.
Definition at line 150 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::SetInitialWriteOffset | ( | TSize | size | ) | [inline] |
Sets the initial write offset.
That is, the index at which the buffer starts writeing after it has been initialized. Init() or InitPointers() should be called after this method.
Definition at line 173 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::SetReadSize | ( | TSize | size | ) | [inline] |
Sets the read size.
This read size must be greater than or equal to 0 and less than or equal to the buffer size.
Definition at line 459 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::SetWriteSize | ( | TSize | size | ) | [inline] |
Sets the write size.
This write size must be greater than or equal to 0 and less than or equal to the buffer size.
Definition at line 471 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Write | ( | const Array< T > & | in, | |
TSize | offset = 0 | |||
) | [inline] |
Writes write size number of elements starting at the current write index from the array starting at the specified offset (default: offset = 0).
The input array must have atleast write size number of elements from the specified offset onwards. Note: define CLAM_HIGH_OPTIMIZATIONS for this method to be efficient.
Definition at line 331 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Write | ( | const T * | buffer | ) | [inline] |
Writes write size number of elements starting at the current write index from buffer.
If it must write out of bounds, it will 'wrap-around'.
Definition at line 306 of file CircularBuffer.hxx.
void CLAM::CircularBuffer< T >::Write | ( | const T & | element | ) | [inline] |
Writes a single element at the current write index.
Definition at line 295 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Write().
Array<T> CLAM::CircularBuffer< T >::mBuffer [protected] |
Definition at line 49 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Add(), CLAM::CircularBuffer< TData >::GetBufferSize(), CLAM::CircularBuffer< TData >::GetPtrToElement(), CLAM::CircularBuffer< TData >::NonCopyRead(), CLAM::CircularBuffer< TData >::Read(), CLAM::CircularBuffer< TData >::SetBufferSize(), CLAM::CircularBuffer< TData >::SetBufferToZero(), and CLAM::CircularBuffer< TData >::Write().
TSize CLAM::CircularBuffer< T >::mInitialReadOffset [protected] |
Definition at line 55 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::CircularBuffer(), CLAM::CircularBuffer< TData >::GetInitialReadOffset(), CLAM::CircularBuffer< TData >::InitPointers(), and CLAM::CircularBuffer< TData >::SetInitialReadOffset().
TSize CLAM::CircularBuffer< T >::mInitialWriteOffset [protected] |
Definition at line 56 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::CircularBuffer(), CLAM::CircularBuffer< TData >::GetInitialWriteOffset(), CLAM::CircularBuffer< TData >::InitPointers(), and CLAM::CircularBuffer< TData >::SetInitialWriteOffset().
TSize CLAM::CircularBuffer< T >::mLogicalSize [protected] |
Definition at line 50 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::CircularBuffer(), CLAM::CircularBuffer< TData >::DecreaseReadIndex(), CLAM::CircularBuffer< TData >::DecreaseWriteIndex(), CLAM::CircularBuffer< TData >::IncreaseReadIndex(), CLAM::CircularBuffer< TData >::IncreaseWriteIndex(), and CLAM::CircularBuffer< TData >::InitPointers().
TSize CLAM::CircularBuffer< T >::mReadIndex [protected] |
Definition at line 54 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::CircularBuffer(), CLAM::CircularBuffer< TData >::DecreaseReadIndex(), CLAM::CircularBuffer< TData >::GetReadIndex(), CLAM::CircularBuffer< TData >::IncreaseReadIndex(), CLAM::CircularBuffer< TData >::InitPointers(), CLAM::CircularBuffer< TData >::NonCopyRead(), and CLAM::CircularBuffer< TData >::Read().
TSize CLAM::CircularBuffer< T >::mReadSize [protected] |
TSize CLAM::CircularBuffer< T >::mWriteIndex [protected] |
Definition at line 53 of file CircularBuffer.hxx.
Referenced by CLAM::CircularBuffer< TData >::Add(), CLAM::CircularBuffer< TData >::CircularBuffer(), CLAM::CircularBuffer< TData >::DecreaseWriteIndex(), CLAM::CircularBuffer< TData >::GetWriteIndex(), CLAM::CircularBuffer< TData >::IncreaseWriteIndex(), CLAM::CircularBuffer< TData >::InitPointers(), and CLAM::CircularBuffer< TData >::Write().
TSize CLAM::CircularBuffer< T >::mWriteSize [protected] |