Classes | Public Types | Public Member Functions | Static Public Attributes | Private Attributes | List of all members
icl_core::RingBuffer< T > Class Template Reference

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_typearray_range
 Array range as in boost::circular_buffer::array_range. More...
 
typedef std::pair< const T *, size_typeconst_array_range
 Array range as in boost::circular_buffer::const_array_range. More...
 
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. More...
 
T & at (size_type pos)
 Access an arbitrary element in the ring buffer without removing it. More...
 
const_iterator begin () const
 
iterator begin ()
 
size_type capacity () const
 Returns the capacity of the ring buffer. More...
 
void clear ()
 Clears the ring buffer. More...
 
bool empty () const
 Returns true if the buffer is empty. More...
 
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. More...
 
RingBufferoperator= (const RingBuffer< T > &other)
 Assignment operator. More...
 
read ()
 Removes an element from the ring buffer provided there is one present. More...
 
size_type reserve () const
 Returns the remaining reserve (free space) of the ring buffer. More...
 
 RingBuffer (size_type capacity=cDEFAULT_CAPACITY)
 Default constructor. More...
 
 RingBuffer (const RingBuffer< T > &other)
 Copy constructor. More...
 
void setCapacity (size_type capacity)
 Changes the capacity of the ring buffer. More...
 
size_type size () const
 Returns the current number of elements in the ring buffer. More...
 
void skip ()
 Removes an element from the ring buffer without returning it. More...
 
void skip (size_type count)
 
void write (const T &val, bool overwrite=false)
 Adds an element to the ring buffer provided there is room. More...
 
 ~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
 

Detailed Description

template<typename T>
class icl_core::RingBuffer< T >

A simple ring buffer implementation based on std::vector.

Note
This ring buffer is only suitable for scalar or POD data types! This is because elements are not deconstructed upon removal, except when the RingBuffer itself is destroyed.

Definition at line 45 of file RingBuffer.h.

Member Typedef Documentation

template<typename T>
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.

template<typename T>
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.

template<typename T>
typedef std::vector<T>::difference_type icl_core::RingBuffer< T >::difference_type

Definition at line 50 of file RingBuffer.h.

template<typename T>
typedef std::vector<T>::size_type icl_core::RingBuffer< T >::size_type

Definition at line 49 of file RingBuffer.h.

template<typename T>
typedef T icl_core::RingBuffer< T >::value_type

Definition at line 48 of file RingBuffer.h.

Constructor & Destructor Documentation

template<typename T>
icl_core::RingBuffer< T >::RingBuffer ( size_type  capacity = cDEFAULT_CAPACITY)
inline

Default constructor.

Definition at line 202 of file RingBuffer.h.

template<typename T>
icl_core::RingBuffer< T >::RingBuffer ( const RingBuffer< T > &  other)
inline

Copy constructor.

Definition at line 207 of file RingBuffer.h.

template<typename T>
icl_core::RingBuffer< T >::~RingBuffer ( )
inline

Destructor documentation which Jan Oberlaender hasn't done till now!

Definition at line 408 of file RingBuffer.h.

Member Function Documentation

template<typename T >
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.

template<typename T >
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.

See also
arrayOne()

Definition at line 42 of file RingBuffer.hpp.

template<typename T >
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.

See also
arrayOne()

Definition at line 55 of file RingBuffer.hpp.

template<typename T >
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.

See also
arrayOne()

Definition at line 68 of file RingBuffer.hpp.

template<typename T>
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.

Parameters
posThe position into the buffer. 0 is the oldest element currently present.

Definition at line 269 of file RingBuffer.h.

template<typename T>
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.

Parameters
posThe position into the buffer. 0 is the oldest element currently present.

Definition at line 291 of file RingBuffer.h.

template<typename T>
const_iterator icl_core::RingBuffer< T >::begin ( ) const
inline

Definition at line 411 of file RingBuffer.h.

template<typename T>
iterator icl_core::RingBuffer< T >::begin ( )
inline

Definition at line 413 of file RingBuffer.h.

template<typename T>
size_type icl_core::RingBuffer< T >::capacity ( ) const
inline

Returns the capacity of the ring buffer.

Definition at line 362 of file RingBuffer.h.

template<typename T>
void icl_core::RingBuffer< T >::clear ( )
inline

Clears the ring buffer.

Definition at line 221 of file RingBuffer.h.

template<typename T>
bool icl_core::RingBuffer< T >::empty ( ) const
inline

Returns true if the buffer is empty.

Definition at line 224 of file RingBuffer.h.

template<typename T >
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.

See also
arrayOne()

Definition at line 81 of file RingBuffer.hpp.

template<typename T >
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.

See also
arrayOne()

Definition at line 101 of file RingBuffer.hpp.

template<typename T >
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.

See also
arrayOne()

Definition at line 121 of file RingBuffer.hpp.

template<typename T >
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.

See also
arrayOne()

Definition at line 134 of file RingBuffer.hpp.

template<typename T>
const_iterator icl_core::RingBuffer< T >::end ( ) const
inline

Definition at line 416 of file RingBuffer.h.

template<typename T>
iterator icl_core::RingBuffer< T >::end ( )
inline

Definition at line 418 of file RingBuffer.h.

template<typename T >
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.

template<typename T>
const T& icl_core::RingBuffer< T >::front ( ) const
inline

Definition at line 308 of file RingBuffer.h.

template<typename T>
T& icl_core::RingBuffer< T >::front ( )
inline

Definition at line 309 of file RingBuffer.h.

template<typename T>
bool icl_core::RingBuffer< T >::full ( ) const
inline

Returns true if the buffer is full.

Definition at line 227 of file RingBuffer.h.

template<typename T>
RingBuffer& icl_core::RingBuffer< T >::operator= ( const RingBuffer< T > &  other)
inline

Assignment operator.

Definition at line 212 of file RingBuffer.h.

template<typename T>
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.

template<typename T>
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.

template<typename T>
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.

template<typename T>
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.

template<typename T>
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.

template<typename T >
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.

template<typename T>
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.

Member Data Documentation

template<typename T>
const size_type icl_core::RingBuffer< T >::cDEFAULT_CAPACITY = 32
static

Definition at line 55 of file RingBuffer.h.

template<typename T>
std::vector<T> icl_core::RingBuffer< T >::m_buffer
private

Definition at line 541 of file RingBuffer.h.

template<typename T>
size_type icl_core::RingBuffer< T >::m_read
private

Definition at line 543 of file RingBuffer.h.

template<typename T>
size_type icl_core::RingBuffer< T >::m_write
private

Definition at line 542 of file RingBuffer.h.


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


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:59