MADARA  3.4.1
madara::utility::CircularBuffer< T > Class Template Reference

General purpose circular buffer container. More...

#include <CircularBuffer.h>

Classes

class  const_iterator
 Const input iterator for elements of a CircularBuffer. Usual semantics. More...
 
class  iterator
 Mutable input iterator for elements of a CircularBuffer. Usual semantics. More...
 

Public Member Functions

 CircularBuffer ()=default
 Construct with zero capacity. More...
 
 CircularBuffer (CircularBuffer &&other) noexcept
 Move constructor. More...
 
 CircularBuffer (const CircularBuffer &other)
 Copy constructor. More...
 
 CircularBuffer (size_t capacity, size_t initial_index=0)
 Initialize with given capacity. More...
 
 ~CircularBuffer () noexcept
 Destructor. More...
 
size_t actual (size_t i) const
 Convert an actual index, to the modulused actual index within buffer. More...
 
T & at (size_t i)
 Get a reference to ith element. More...
 
const T & at (size_t i) const
 Get a const reference to ith element. More...
 
T & back ()
 Get a reference to the back element. Undefined behavior if empty. More...
 
const T & back () const
 Get a const reference to the back element. Undefined behavior if empty. More...
 
size_t back_actual () const
 Actual index of back within the buffer. More...
 
size_t back_index () const
 Index of back element. More...
 
iterator begin ()
 Create a mutable iterator to the front of the buffer. More...
 
const_iterator begin () const
 Create a const iterator to the front of the buffer. More...
 
size_t capacity () const
 Number of elements this buffer can hold without dropping any. More...
 
const_iterator cbegin () const
 Create a const iterator to the back of the buffer. More...
 
const_iterator cend () const
 Create a const iterator to the back of the buffer. More...
 
ssize_t check_range (size_t i) const
 Compares given index relative to existing range of elements (front_index() to back_index()) More...
 
void clear ()
 Empty the buffer, destructing all elements. More...
 
void discard_back ()
 Remove the back element and discard it. More...
 
void discard_front ()
 Remove the front element and discard it. More...
 
template<typename... Args>
T & emplace_back (Args &&... args)
 Construct a new element in-place in the back of the buffer. More...
 
bool empty () const
 Is this CircularBuffer empty? More...
 
iterator end ()
 Create a mutable iterator to the back of the buffer. More...
 
const_iterator end () const
 Create a const iterator to the back of the buffer. More...
 
T & front ()
 Get a reference to the front element. Undefined behavior if empty. More...
 
const T & front () const
 Get a const reference to the front element. Undefined behavior if empty. More...
 
size_t front_actual () const
 Actual index of front within the buffer. More...
 
size_t front_index () const
 Index of front element. More...
 
get_back ()
 Get a copy of the front element, or a default constructed value if the buffer is empty. More...
 
get_front ()
 Get a copy of the front element, or a default constructed value if the buffer is empty. More...
 
CircularBufferoperator= (CircularBuffer &&other) noexcept
 Move assignment operator. More...
 
CircularBufferoperator= (const CircularBuffer &other)
 Copy assignment operator. More...
 
T & operator[] (size_t i)
 Get a reference to the ith element. More...
 
const T & operator[] (size_t i) const
 Get a const reference to the ith element. More...
 
pop_back ()
 Remove the back element and return it. More...
 
pop_front ()
 Remove the front element and return it. More...
 
T & push_back (const T &val)
 Copy the value val to the back of the buffer. More...
 
T & push_back (T &&val)
 Move the value val to the back of the buffer. More...
 
void reserve (size_t size)
 Change the capacity of this CircularBuffer. More...
 
size_t size () const
 Number of elements this buffer currently holds, up to capacity. More...
 
void swap (CircularBuffer &other) noexcept
 Swaps contents with another CircularBuffer, without copying elements. More...
 

Private Member Functions

void throw_out_of_range (const char *func, size_t i) const
 

Private Attributes

size_t back_ = 0
 
size_t cap_ = 0
 
T * data_ = nullptr
 
size_t front_ = 0
 

Friends

class const_iterator
 
class iterator
 

Detailed Description

template<typename T>
class madara::utility::CircularBuffer< T >

General purpose circular buffer container.

Differs from some implementations in that it tracks an always-growing index, enabling multiple independent consumers. Overwritten elements are safely seen as missing, and will not silently alias the newer elements overwriting them.

Definition at line 31 of file CircularBuffer.h.

Constructor & Destructor Documentation

◆ CircularBuffer() [1/4]

template<typename T >
madara::utility::CircularBuffer< T >::CircularBuffer ( )
default

Construct with zero capacity.

Not useful in this state. Does not allocate to the heap.

◆ CircularBuffer() [2/4]

template<typename T >
madara::utility::CircularBuffer< T >::CircularBuffer ( size_t  capacity,
size_t  initial_index = 0 
)
inlineexplicit

Initialize with given capacity.

Up to that many contiguous elements will be held at once. If more than that many elements are added to the back of the buffer, elements from the front will silently be removed.

Definition at line 45 of file CircularBuffer.h.

◆ CircularBuffer() [3/4]

template<typename T >
madara::utility::CircularBuffer< T >::CircularBuffer ( const CircularBuffer< T > &  other)
inline

Copy constructor.

Abides by typical semantics.

Definition at line 56 of file CircularBuffer.h.

◆ CircularBuffer() [4/4]

template<typename T >
madara::utility::CircularBuffer< T >::CircularBuffer ( CircularBuffer< T > &&  other)
inlinenoexcept

Move constructor.

Abides by typical semantics.

Definition at line 68 of file CircularBuffer.h.

◆ ~CircularBuffer()

template<typename T >
madara::utility::CircularBuffer< T >::~CircularBuffer ( )
inlinenoexcept

Destructor.

Abides by typical semantics.

Definition at line 95 of file CircularBuffer.h.

Member Function Documentation

◆ actual()

template<typename T >
size_t madara::utility::CircularBuffer< T >::actual ( size_t  i) const
inline

Convert an actual index, to the modulused actual index within buffer.

Definition at line 165 of file CircularBuffer.h.

◆ at() [1/2]

template<typename T >
T& madara::utility::CircularBuffer< T >::at ( size_t  i)
inline

Get a reference to ith element.

Throws std::out_of_range if this index does not currently exist.

Definition at line 624 of file CircularBuffer.h.

◆ at() [2/2]

template<typename T >
const T& madara::utility::CircularBuffer< T >::at ( size_t  i) const
inline

Get a const reference to ith element.

Throws std::out_of_range if this index does not currently exist.

Definition at line 634 of file CircularBuffer.h.

◆ back() [1/2]

template<typename T >
T& madara::utility::CircularBuffer< T >::back ( )
inline

Get a reference to the back element. Undefined behavior if empty.

Definition at line 550 of file CircularBuffer.h.

◆ back() [2/2]

template<typename T >
const T& madara::utility::CircularBuffer< T >::back ( ) const
inline

Get a const reference to the back element. Undefined behavior if empty.

Definition at line 556 of file CircularBuffer.h.

◆ back_actual()

template<typename T >
size_t madara::utility::CircularBuffer< T >::back_actual ( ) const
inline

Actual index of back within the buffer.

Definition at line 177 of file CircularBuffer.h.

◆ back_index()

template<typename T >
size_t madara::utility::CircularBuffer< T >::back_index ( ) const
inline

Index of back element.

Definition at line 159 of file CircularBuffer.h.

◆ begin() [1/2]

template<typename T >
iterator madara::utility::CircularBuffer< T >::begin ( )
inline

Create a mutable iterator to the front of the buffer.

Definition at line 488 of file CircularBuffer.h.

◆ begin() [2/2]

template<typename T >
const_iterator madara::utility::CircularBuffer< T >::begin ( ) const
inline

Create a const iterator to the front of the buffer.

Definition at line 500 of file CircularBuffer.h.

◆ capacity()

template<typename T >
size_t madara::utility::CircularBuffer< T >::capacity ( ) const
inline

Number of elements this buffer can hold without dropping any.

Definition at line 141 of file CircularBuffer.h.

◆ cbegin()

template<typename T >
const_iterator madara::utility::CircularBuffer< T >::cbegin ( ) const
inline

Create a const iterator to the back of the buffer.

Definition at line 512 of file CircularBuffer.h.

◆ cend()

template<typename T >
const_iterator madara::utility::CircularBuffer< T >::cend ( ) const
inline

Create a const iterator to the back of the buffer.

Definition at line 518 of file CircularBuffer.h.

◆ check_range()

template<typename T >
ssize_t madara::utility::CircularBuffer< T >::check_range ( size_t  i) const
inline

Compares given index relative to existing range of elements (front_index() to back_index())

Parameters
ithe index to check for existence within range
Returns
0 if index is within range of existing elements. Otherwise, if index < front_index(), return index - front_index() (negative). Else, if index i > back_index(), return index - back_index() (positive).

Definition at line 584 of file CircularBuffer.h.

◆ clear()

template<typename T >
void madara::utility::CircularBuffer< T >::clear ( void  )
inline

Empty the buffer, destructing all elements.

Definition at line 183 of file CircularBuffer.h.

◆ discard_back()

template<typename T >
void madara::utility::CircularBuffer< T >::discard_back ( )
inline

Remove the back element and discard it.

Definition at line 666 of file CircularBuffer.h.

◆ discard_front()

template<typename T >
void madara::utility::CircularBuffer< T >::discard_front ( )
inline

Remove the front element and discard it.

Definition at line 643 of file CircularBuffer.h.

◆ emplace_back()

template<typename T >
template<typename... Args>
T& madara::utility::CircularBuffer< T >::emplace_back ( Args &&...  args)
inline

Construct a new element in-place in the back of the buffer.

Will discard the front element if size() already equals capacity().

Returns
a reference to the new element

Definition at line 693 of file CircularBuffer.h.

◆ empty()

template<typename T >
bool madara::utility::CircularBuffer< T >::empty ( ) const
inline

Is this CircularBuffer empty?

Definition at line 135 of file CircularBuffer.h.

◆ end() [1/2]

template<typename T >
iterator madara::utility::CircularBuffer< T >::end ( )
inline

Create a mutable iterator to the back of the buffer.

Definition at line 494 of file CircularBuffer.h.

◆ end() [2/2]

template<typename T >
const_iterator madara::utility::CircularBuffer< T >::end ( ) const
inline

Create a const iterator to the back of the buffer.

Definition at line 506 of file CircularBuffer.h.

◆ front() [1/2]

template<typename T >
T& madara::utility::CircularBuffer< T >::front ( )
inline

Get a reference to the front element. Undefined behavior if empty.

Definition at line 538 of file CircularBuffer.h.

◆ front() [2/2]

template<typename T >
const T& madara::utility::CircularBuffer< T >::front ( ) const
inline

Get a const reference to the front element. Undefined behavior if empty.

Definition at line 544 of file CircularBuffer.h.

◆ front_actual()

template<typename T >
size_t madara::utility::CircularBuffer< T >::front_actual ( ) const
inline

Actual index of front within the buffer.

Definition at line 171 of file CircularBuffer.h.

◆ front_index()

template<typename T >
size_t madara::utility::CircularBuffer< T >::front_index ( ) const
inline

Index of front element.

Definition at line 153 of file CircularBuffer.h.

◆ get_back()

template<typename T >
T madara::utility::CircularBuffer< T >::get_back ( )
inline

Get a copy of the front element, or a default constructed value if the buffer is empty.

Definition at line 570 of file CircularBuffer.h.

◆ get_front()

template<typename T >
T madara::utility::CircularBuffer< T >::get_front ( )
inline

Get a copy of the front element, or a default constructed value if the buffer is empty.

Definition at line 563 of file CircularBuffer.h.

◆ operator=() [1/2]

template<typename T >
CircularBuffer& madara::utility::CircularBuffer< T >::operator= ( CircularBuffer< T > &&  other)
inlinenoexcept

Move assignment operator.

Abides by typical semantics.

Definition at line 86 of file CircularBuffer.h.

◆ operator=() [2/2]

template<typename T >
CircularBuffer& madara::utility::CircularBuffer< T >::operator= ( const CircularBuffer< T > &  other)
inline

Copy assignment operator.

Abides by typical semantics.

Definition at line 76 of file CircularBuffer.h.

◆ operator[]() [1/2]

template<typename T >
T& madara::utility::CircularBuffer< T >::operator[] ( size_t  i)
inline

Get a reference to the ith element.

Undefined behavior if empty, or if index is outside front_index() and back_index().

Definition at line 525 of file CircularBuffer.h.

◆ operator[]() [2/2]

template<typename T >
const T& madara::utility::CircularBuffer< T >::operator[] ( size_t  i) const
inline

Get a const reference to the ith element.

Undefined behavior if empty, or if index is outside front_index() and back_index().

Definition at line 532 of file CircularBuffer.h.

◆ pop_back()

template<typename T >
T madara::utility::CircularBuffer< T >::pop_back ( )
inline

Remove the back element and return it.

Definition at line 675 of file CircularBuffer.h.

◆ pop_front()

template<typename T >
T madara::utility::CircularBuffer< T >::pop_front ( )
inline

Remove the front element and return it.

Definition at line 652 of file CircularBuffer.h.

◆ push_back() [1/2]

template<typename T >
T& madara::utility::CircularBuffer< T >::push_back ( const T &  val)
inline

Copy the value val to the back of the buffer.

Will discard the front element if size() already equals capacity().

Returns
a reference to the new element

Definition at line 710 of file CircularBuffer.h.

◆ push_back() [2/2]

template<typename T >
T& madara::utility::CircularBuffer< T >::push_back ( T &&  val)
inline

Move the value val to the back of the buffer.

Will discard the front element if size() already equals capacity().

Returns
a reference to the new element

Definition at line 721 of file CircularBuffer.h.

◆ reserve()

template<typename T >
void madara::utility::CircularBuffer< T >::reserve ( size_t  size)
inline

Change the capacity of this CircularBuffer.

Unlike vectors, this operation will always set capacity to exactly the size given. This operation always moves each element individually.

Definition at line 118 of file CircularBuffer.h.

◆ size()

template<typename T >
size_t madara::utility::CircularBuffer< T >::size ( void  ) const
inline

Number of elements this buffer currently holds, up to capacity.

Definition at line 147 of file CircularBuffer.h.

◆ swap()

template<typename T >
void madara::utility::CircularBuffer< T >::swap ( CircularBuffer< T > &  other)
inlinenoexcept

Swaps contents with another CircularBuffer, without copying elements.

Definition at line 104 of file CircularBuffer.h.

◆ throw_out_of_range()

template<typename T >
void madara::utility::CircularBuffer< T >::throw_out_of_range ( const char *  func,
size_t  i 
) const
inlineprivate

Definition at line 600 of file CircularBuffer.h.

Friends And Related Function Documentation

◆ const_iterator

template<typename T >
friend class const_iterator
friend

Definition at line 735 of file CircularBuffer.h.

◆ iterator

template<typename T >
friend class iterator
friend

Definition at line 734 of file CircularBuffer.h.

Member Data Documentation

◆ back_

template<typename T >
size_t madara::utility::CircularBuffer< T >::back_ = 0
private

Definition at line 728 of file CircularBuffer.h.

◆ cap_

template<typename T >
size_t madara::utility::CircularBuffer< T >::cap_ = 0
private

Definition at line 732 of file CircularBuffer.h.

◆ data_

template<typename T >
T* madara::utility::CircularBuffer< T >::data_ = nullptr
private

Definition at line 730 of file CircularBuffer.h.

◆ front_

template<typename T >
size_t madara::utility::CircularBuffer< T >::front_ = 0
private

Definition at line 727 of file CircularBuffer.h.


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