A simple ring buffer implementation based on std::vector. More...
#include <RingBuffer.h>
Classes | |
class | const_iterator |
Const iterator for RingBuffers. More... | |
class | iterator |
Public Types | |
typedef std::pair< T *, size_type > | array_range |
Array range as in boost::circular_buffer::array_range. | |
typedef std::pair< const T *, size_type > | const_array_range |
Array range as in boost::circular_buffer::const_array_range. | |
typedef std::vector< T > ::difference_type | difference_type |
typedef std::vector< T >::size_type | size_type |
typedef T | value_type |
Public Member Functions | |
array_range | arrayOne () |
const_array_range | arrayOne () const |
array_range | arrayTwo () |
const_array_range | arrayTwo () const |
const T & | at (size_type pos) const |
Read an arbitrary element from the ring buffer without removing it. | |
T & | at (size_type pos) |
Access an arbitrary element in the ring buffer without removing it. | |
const_iterator | begin () const |
iterator | begin () |
size_type | capacity () const |
Returns the capacity of the ring buffer. | |
void | clear () |
Clears the ring buffer. | |
bool | empty () const |
Returns true if the buffer is empty. | |
array_range | emptyArrayOne () |
const_array_range | emptyArrayOne () const |
array_range | emptyArrayTwo () |
const_array_range | emptyArrayTwo () const |
const_iterator | end () const |
iterator | end () |
void | fakeWrite (size_t count) |
const T & | front () const |
T & | front () |
bool | full () const |
Returns true if the buffer is full. | |
RingBuffer & | operator= (const RingBuffer< T > &other) |
Assignment operator. | |
T | read () |
Removes an element from the ring buffer provided there is one present. | |
size_type | reserve () const |
Returns the remaining reserve (free space) of the ring buffer. | |
RingBuffer (size_type capacity=cDEFAULT_CAPACITY) | |
Default constructor. | |
RingBuffer (const RingBuffer< T > &other) | |
Copy constructor. | |
void | setCapacity (size_type capacity) |
Changes the capacity of the ring buffer. | |
size_type | size () const |
Returns the current number of elements in the ring buffer. | |
void | skip () |
Removes an element from the ring buffer without returning it. | |
void | skip (size_type count) |
void | write (const T &val, bool overwrite=false) |
Adds an element to the ring buffer provided there is room. | |
~RingBuffer () | |
Static Public Attributes | |
static const size_type | cDEFAULT_CAPACITY = 32 |
Private Attributes | |
std::vector< T > | m_buffer |
size_type | m_read |
size_type | m_write |
A simple ring buffer implementation based on std::vector.
Definition at line 45 of file RingBuffer.h.
typedef std::pair<T*, size_type> icl_core::RingBuffer< T >::array_range |
Array range as in boost::circular_buffer::array_range.
Definition at line 52 of file RingBuffer.h.
typedef std::pair<const T*, size_type> icl_core::RingBuffer< T >::const_array_range |
Array range as in boost::circular_buffer::const_array_range.
Definition at line 54 of file RingBuffer.h.
typedef std::vector<T>::difference_type icl_core::RingBuffer< T >::difference_type |
Definition at line 50 of file RingBuffer.h.
typedef std::vector<T>::size_type icl_core::RingBuffer< T >::size_type |
Definition at line 49 of file RingBuffer.h.
typedef T icl_core::RingBuffer< T >::value_type |
Definition at line 48 of file RingBuffer.h.
icl_core::RingBuffer< T >::RingBuffer | ( | size_type | capacity = cDEFAULT_CAPACITY | ) | [inline] |
Default constructor.
Definition at line 202 of file RingBuffer.h.
icl_core::RingBuffer< T >::RingBuffer | ( | const RingBuffer< T > & | other | ) | [inline] |
Copy constructor.
Definition at line 207 of file RingBuffer.h.
icl_core::RingBuffer< T >::~RingBuffer | ( | ) | [inline] |
Destructor documentation which Jan Oberlaender hasn't done till now!
Definition at line 408 of file RingBuffer.h.
RingBuffer< T >::array_range icl_core::RingBuffer< T >::arrayOne | ( | ) |
Returns a pointer to the first contiguous filled block of data in the RingBuffer, and the block length. Since the RingBuffer is a contiguous block of memory, but its filled area may wrap around from the end of that block to the beginning, there may be two separate blocks to consider. arrayOne() provides access to the first block, while arrayTwo() provides access to the second block. Similarly, there may be two empty blocks: the first at the end of the internal buffer, after the RingBuffer's last element, and the second at the beginning of the internal buffer, before the RingBuffer's first element. emptyArrayOne() and emptyArrayTwo() provide access to those blocks.
These access methods are provided for old C APIs and external routines which need raw buffer pointers to write into.
Definition at line 29 of file RingBuffer.hpp.
RingBuffer< T >::const_array_range icl_core::RingBuffer< T >::arrayOne | ( | ) | const |
Returns a pointer to the first contiguous filled block of data in the RingBuffer, and the block length.
Definition at line 42 of file RingBuffer.hpp.
RingBuffer< T >::array_range icl_core::RingBuffer< T >::arrayTwo | ( | ) |
Returns a pointer to the second contiguous filled block of data in the RingBuffer, and the block length.
Definition at line 55 of file RingBuffer.hpp.
RingBuffer< T >::const_array_range icl_core::RingBuffer< T >::arrayTwo | ( | ) | const |
Returns a pointer to the second contiguous filled block of data in the RingBuffer, and the block length.
Definition at line 68 of file RingBuffer.hpp.
const T& icl_core::RingBuffer< T >::at | ( | size_type | pos | ) | const [inline] |
Read an arbitrary element from the ring buffer without removing it.
Throws an exception if the index is out of range.
pos | The position into the buffer. 0 is the oldest element currently present. |
Definition at line 269 of file RingBuffer.h.
T& icl_core::RingBuffer< T >::at | ( | size_type | pos | ) | [inline] |
Access an arbitrary element in the ring buffer without removing it.
Throws an exception if the index is out of range.
pos | The position into the buffer. 0 is the oldest element currently present. |
Definition at line 291 of file RingBuffer.h.
const_iterator icl_core::RingBuffer< T >::begin | ( | ) | const [inline] |
Definition at line 411 of file RingBuffer.h.
iterator icl_core::RingBuffer< T >::begin | ( | ) | [inline] |
Definition at line 413 of file RingBuffer.h.
size_type icl_core::RingBuffer< T >::capacity | ( | ) | const [inline] |
Returns the capacity of the ring buffer.
Definition at line 362 of file RingBuffer.h.
void icl_core::RingBuffer< T >::clear | ( | ) | [inline] |
Clears the ring buffer.
Definition at line 221 of file RingBuffer.h.
bool icl_core::RingBuffer< T >::empty | ( | ) | const [inline] |
Returns true
if the buffer is empty.
Definition at line 224 of file RingBuffer.h.
RingBuffer< T >::array_range icl_core::RingBuffer< T >::emptyArrayOne | ( | ) |
Returns a pointer to the first contiguous empty block of data in the RingBuffer, and the block length.
Definition at line 81 of file RingBuffer.hpp.
RingBuffer< T >::const_array_range icl_core::RingBuffer< T >::emptyArrayOne | ( | ) | const |
Returns a pointer to the first contiguous empty block of data in the RingBuffer, and the block length.
Definition at line 101 of file RingBuffer.hpp.
RingBuffer< T >::array_range icl_core::RingBuffer< T >::emptyArrayTwo | ( | ) |
Returns a pointer to the second contiguous empty block of data in the RingBuffer, and the block length.
Definition at line 121 of file RingBuffer.hpp.
RingBuffer< T >::const_array_range icl_core::RingBuffer< T >::emptyArrayTwo | ( | ) | const |
Returns a pointer to the second contiguous empty block of data in the RingBuffer, and the block length.
Definition at line 134 of file RingBuffer.hpp.
const_iterator icl_core::RingBuffer< T >::end | ( | ) | const [inline] |
Definition at line 416 of file RingBuffer.h.
iterator icl_core::RingBuffer< T >::end | ( | ) | [inline] |
Definition at line 418 of file RingBuffer.h.
void icl_core::RingBuffer< T >::fakeWrite | ( | size_t | count | ) |
Increases ring buffer size by count, at most up to its capacity. The added elements are not overwritten. Use this if you have filled buffer elements (from emptyArrayOne() and emptyArrayTwo()) manually, to adjust the ring buffer's write pointer.
Definition at line 160 of file RingBuffer.hpp.
const T& icl_core::RingBuffer< T >::front | ( | ) | const [inline] |
Definition at line 308 of file RingBuffer.h.
T& icl_core::RingBuffer< T >::front | ( | ) | [inline] |
Definition at line 309 of file RingBuffer.h.
bool icl_core::RingBuffer< T >::full | ( | ) | const [inline] |
Returns true
if the buffer is full.
Definition at line 227 of file RingBuffer.h.
RingBuffer& icl_core::RingBuffer< T >::operator= | ( | const RingBuffer< T > & | other | ) | [inline] |
Assignment operator.
Definition at line 212 of file RingBuffer.h.
T icl_core::RingBuffer< T >::read | ( | ) | [inline] |
Removes an element from the ring buffer provided there is one present.
Throws an exception if the buffer is empty.
Definition at line 333 of file RingBuffer.h.
size_type icl_core::RingBuffer< T >::reserve | ( | ) | const [inline] |
Returns the remaining reserve (free space) of the ring buffer.
Definition at line 365 of file RingBuffer.h.
void icl_core::RingBuffer< T >::setCapacity | ( | size_type | capacity | ) | [inline] |
Changes the capacity of the ring buffer.
If the new capacity is less than the current buffer size, only the latest capacity elements are kept, the rest are destroyed.
Definition at line 375 of file RingBuffer.h.
size_type icl_core::RingBuffer< T >::size | ( | ) | const [inline] |
Returns the current number of elements in the ring buffer.
Definition at line 349 of file RingBuffer.h.
void icl_core::RingBuffer< T >::skip | ( | ) | [inline] |
Removes an element from the ring buffer without returning it.
Definition at line 312 of file RingBuffer.h.
void icl_core::RingBuffer< T >::skip | ( | size_type | count | ) |
Removes a number of elements from the ring buffer without returning them. If the ring buffer is empty, nothing happens.
Definition at line 147 of file RingBuffer.hpp.
void icl_core::RingBuffer< T >::write | ( | const T & | val, |
bool | overwrite = false |
||
) | [inline] |
Adds an element to the ring buffer provided there is room.
If overwrite == false
, throws an exception if the element can not be added. If overwrite == true
, old elements are discarded to make room for new ones.
Definition at line 234 of file RingBuffer.h.
const size_type icl_core::RingBuffer< T >::cDEFAULT_CAPACITY = 32 [static] |
Definition at line 55 of file RingBuffer.h.
std::vector<T> icl_core::RingBuffer< T >::m_buffer [private] |
Definition at line 541 of file RingBuffer.h.
size_type icl_core::RingBuffer< T >::m_read [private] |
Definition at line 543 of file RingBuffer.h.
size_type icl_core::RingBuffer< T >::m_write [private] |
Definition at line 542 of file RingBuffer.h.