libboxes
boxes is a set of specialised containers built on top of STL
Public Member Functions | List of all members
boxes::RingBuffer< T, SizeV > Class Template Reference

Implements a fixed-size double ended queue. More...

#include <ring_buffer.hpp>

Public Member Functions

 RingBuffer (const RingBuffer< T, SizeV > &other)
 Copy constructs a RingBuffer from another. More...
 
 RingBuffer (RingBuffer< T, SizeV > &&other)
 Move constructs a RingBuffer from another. More...
 
RingBuffer< T, SizeV > & operator= (const RingBuffer< T, SizeV > &other)
 Copy the contents of another RingBuffer into this one. More...
 
RingBuffer< T, SizeV > & operator= (RingBuffer< T, SizeV > &&other)
 Moves the contents of another RingBuffer into this one. More...
 
iterator begin ()
 Returns an iterator to the first element in the RingBuffer. More...
 
iterator end ()
 Returns an iterator to the end of the RingBuffer. More...
 
iterator cbegin () const
 Returns a const iterator to the first element in the RingBuffer. More...
 
iterator cend () const
 Returns a const iterator to the end of the RingBuffer. More...
 
bool empty () const BOXES_NOTHROW
 Returns true if the RingBuffer is empty. More...
 
bool full () const BOXES_NOTHROW
 Returns true if the RingBuffer is full. More...
 
T & back ()
 Returns the last element in the queue. More...
 
T & front ()
 Returns the first element in the queue. More...
 
const T & front () const
 Version of front that can be used with const RingBuffers. More...
 
const T & back () const
 Version of back that can be used with const RingBuffers. More...
 
T & operator[] (size_type pos)
 Allows for random access to the elements in the RingBuffer. More...
 
const T & operator[] (size_type pos) const
 Returns a const reference to the element at position pos More...
 
T & at (size_type pos)
 Allows for random access to the elements in the RingBuffer. More...
 
const T & at (size_type pos) const
 Const version of at More...
 
bool operator== (const RingBuffer< T, SizeV > &other) const
 Returns true if the contents of this RingBuffer are equal to another. More...
 
bool operator!= (const RingBuffer< T, SizeV > &other) const
 Returns true if the contents of this RingBuffer are not equal to another. More...
 
void swap (RingBuffer< T, SizeV > &other)
 Swaps the contents of this RingBuffer with another. More...
 
template<typename Arg >
bool push_back (Arg &&value)
 Inserts a new element at the end of the RingBuffer. More...
 
template<typename Arg >
bool push_front (Arg &&value)
 Inserts a new element at the front of the RingBuffer. More...
 
bool pop_back ()
 Removes the last element from the RingBuffer. More...
 
bool pop_front ()
 Removes the first element from the RingBuffer. More...
 
size_type size () const BOXES_NOTHROW
 Returns the number of elements in the RingBuffer. More...
 
void clear ()
 Clears the contents of the RingBuffer. More...
 

Detailed Description

template<typename T, std::size_t SizeV>
class boxes::RingBuffer< T, SizeV >

Implements a fixed-size double ended queue.

Elements can be added to the front of the buffer in O(1) time, and removed from the back in O(1) time as well. Additionally, the buffer implements iterators to allow traversal of the contents.

Template Parameters
TThe type of the elements to be stored in the buffer
SizeVThe maximum number of elements that can be stored in the buffer
Examples
ring_buffer_example.cpp.

Definition at line 31 of file ring_buffer.hpp.

Constructor & Destructor Documentation

◆ RingBuffer() [1/2]

template<typename T , std::size_t SizeV>
boxes::RingBuffer< T, SizeV >::RingBuffer ( const RingBuffer< T, SizeV > &  other)
inline

Copy constructs a RingBuffer from another.

Parameters
other

Definition at line 99 of file ring_buffer.hpp.

◆ RingBuffer() [2/2]

template<typename T , std::size_t SizeV>
boxes::RingBuffer< T, SizeV >::RingBuffer ( RingBuffer< T, SizeV > &&  other)
inline

Move constructs a RingBuffer from another.

Parameters
other

Definition at line 107 of file ring_buffer.hpp.

Member Function Documentation

◆ at() [1/2]

template<typename T , std::size_t SizeV>
T & boxes::RingBuffer< T, SizeV >::at ( size_type  pos)
inline

Allows for random access to the elements in the RingBuffer.

The semantics of this method are the same as the operator[] method, but bounds checking is performed. std::out_of_range will be thrown if pos is greater than or equal to the size of the RingBuffer.

Parameters
posThe position of the element to be accessed
Returns
A reference to the element at position pos

Definition at line 265 of file ring_buffer.hpp.

◆ at() [2/2]

template<typename T , std::size_t SizeV>
const T & boxes::RingBuffer< T, SizeV >::at ( size_type  pos) const
inline

Const version of at

Parameters
posThe position of the element to be accessed
Returns
A const reference to the element at position pos

Definition at line 278 of file ring_buffer.hpp.

◆ back() [1/2]

template<typename T , std::size_t SizeV>
T & boxes::RingBuffer< T, SizeV >::back ( )
inline

Returns the last element in the queue.

Since this RingBuffer is a FIFO, the back element is the one that was most recently inserted into the RingBuffer.

Returns
A reference to the last element in the queue

Definition at line 194 of file ring_buffer.hpp.

◆ back() [2/2]

template<typename T , std::size_t SizeV>
const T & boxes::RingBuffer< T, SizeV >::back ( ) const
inline

Version of back that can be used with const RingBuffers.

Returns
A reference to the last element in the queue

Definition at line 228 of file ring_buffer.hpp.

◆ begin()

template<typename T , std::size_t SizeV>
iterator boxes::RingBuffer< T, SizeV >::begin ( )
inline

Returns an iterator to the first element in the RingBuffer.

The first element in the RingBuffer is the one that was inserted first, is retrievable with the front method.

Returns
an iterator to the first element in the RingBuffer

Definition at line 149 of file ring_buffer.hpp.

◆ cbegin()

template<typename T , std::size_t SizeV>
iterator boxes::RingBuffer< T, SizeV >::cbegin ( ) const
inline

Returns a const iterator to the first element in the RingBuffer.

Returns
an iterator to the first element in the RingBuffer

Definition at line 163 of file ring_buffer.hpp.

◆ cend()

template<typename T , std::size_t SizeV>
iterator boxes::RingBuffer< T, SizeV >::cend ( ) const
inline

Returns a const iterator to the end of the RingBuffer.

Returns
an iterator to the end of the RingBuffer

Definition at line 170 of file ring_buffer.hpp.

◆ clear()

template<typename T , std::size_t SizeV>
void boxes::RingBuffer< T, SizeV >::clear ( )
inline

Clears the contents of the RingBuffer.

This method will remove all elements from the RingBuffer, leaving it in an empty state.

Definition at line 424 of file ring_buffer.hpp.

◆ empty()

template<typename T , std::size_t SizeV>
bool boxes::RingBuffer< T, SizeV >::empty ( ) const
inline

Returns true if the RingBuffer is empty.

Returns
true if the RingBuffer is empty
Examples
ring_buffer_example.cpp.

Definition at line 177 of file ring_buffer.hpp.

◆ end()

template<typename T , std::size_t SizeV>
iterator boxes::RingBuffer< T, SizeV >::end ( )
inline

Returns an iterator to the end of the RingBuffer.

Returns
an iterator to the end of the RingBuffer

Definition at line 156 of file ring_buffer.hpp.

◆ front() [1/2]

template<typename T , std::size_t SizeV>
T & boxes::RingBuffer< T, SizeV >::front ( )
inline

Returns the first element in the queue.

Since this RingBuffer is a FIFO, the front element is the one that was inserted into the RingBuffer first.

Returns
A reference to the first element in the queue
Examples
ring_buffer_example.cpp.

Definition at line 209 of file ring_buffer.hpp.

◆ front() [2/2]

template<typename T , std::size_t SizeV>
const T & boxes::RingBuffer< T, SizeV >::front ( ) const
inline

Version of front that can be used with const RingBuffers.

Returns
A reference to the first element in the queue

Definition at line 221 of file ring_buffer.hpp.

◆ full()

template<typename T , std::size_t SizeV>
bool boxes::RingBuffer< T, SizeV >::full ( ) const
inline

Returns true if the RingBuffer is full.

Returns
true if the RingBuffer is full
Examples
ring_buffer_example.cpp.

Definition at line 184 of file ring_buffer.hpp.

◆ operator!=()

template<typename T , std::size_t SizeV>
bool boxes::RingBuffer< T, SizeV >::operator!= ( const RingBuffer< T, SizeV > &  other) const
inline

Returns true if the contents of this RingBuffer are not equal to another.

Performs a lexicographical comparison of the contents of this RingBuffer.

Parameters
otherThe RingBuffer to compare with
Returns
true if the contents of this RingBuffer are not equal to another

Definition at line 313 of file ring_buffer.hpp.

◆ operator=() [1/2]

template<typename T , std::size_t SizeV>
RingBuffer< T, SizeV > & boxes::RingBuffer< T, SizeV >::operator= ( const RingBuffer< T, SizeV > &  other)
inline

Copy the contents of another RingBuffer into this one.

Parameters
other
Returns
Reference to this RingBuffer

Definition at line 119 of file ring_buffer.hpp.

◆ operator=() [2/2]

template<typename T , std::size_t SizeV>
RingBuffer< T, SizeV > & boxes::RingBuffer< T, SizeV >::operator= ( RingBuffer< T, SizeV > &&  other)
inline

Moves the contents of another RingBuffer into this one.

Parameters
other
Returns
Reference to this RingBuffer

Definition at line 133 of file ring_buffer.hpp.

◆ operator==()

template<typename T , std::size_t SizeV>
bool boxes::RingBuffer< T, SizeV >::operator== ( const RingBuffer< T, SizeV > &  other) const
inline

Returns true if the contents of this RingBuffer are equal to another.

Performs a lexicographical comparison of the contents of this RingBuffer.

Parameters
otherThe RingBuffer to compare with
Returns
true if the contents of this RingBuffer are equal to another

Definition at line 290 of file ring_buffer.hpp.

◆ operator[]() [1/2]

template<typename T , std::size_t SizeV>
T & boxes::RingBuffer< T, SizeV >::operator[] ( size_type  pos)
inline

Allows for random access to the elements in the RingBuffer.

The element at position pos is the posth element inserted with posth push_back call.

No bounds checking is performed. It's safe to use this method with pos greater than or equal to the size of the RingBuffer. If pos is greater than or equal to the size of the RingBuffer, the pos will wrap around.

Parameters
posThe position of the element to be accessed
Returns
A reference to the element at position pos

Definition at line 243 of file ring_buffer.hpp.

◆ operator[]() [2/2]

template<typename T , std::size_t SizeV>
const T & boxes::RingBuffer< T, SizeV >::operator[] ( size_type  pos) const
inline

Returns a const reference to the element at position pos

Parameters
posThe position of the element to be accessed
Returns
A const reference to the element at position pos

Definition at line 251 of file ring_buffer.hpp.

◆ pop_back()

template<typename T , std::size_t SizeV>
bool boxes::RingBuffer< T, SizeV >::pop_back ( )
inline

Removes the last element from the RingBuffer.

The element to be removed is the element that was most recently inserted with push_back.

Returns
true if an element was removed. If the RingBuffer is empty, this will return false.

Definition at line 379 of file ring_buffer.hpp.

◆ pop_front()

template<typename T , std::size_t SizeV>
bool boxes::RingBuffer< T, SizeV >::pop_front ( )
inline

Removes the first element from the RingBuffer.

The element to be removed is the element retrieable with the front method.

Returns
true if an element was removed. If the RingBuffer is empty, this will return false.
Examples
ring_buffer_example.cpp.

Definition at line 397 of file ring_buffer.hpp.

◆ push_back()

template<typename T , std::size_t SizeV>
template<typename Arg >
bool boxes::RingBuffer< T, SizeV >::push_back ( Arg &&  value)
inline

Inserts a new element at the end of the RingBuffer.

Newly inserted elements will be placed at the end of the RingBuffer. Newly inserted element can be accessed using the back method.

Template Parameters
ArgThe type of the element to be inserted
Parameters
valueThe value of the element to be inserted
Returns
true if the element was successfully inserted
Examples
ring_buffer_example.cpp.

Definition at line 340 of file ring_buffer.hpp.

◆ push_front()

template<typename T , std::size_t SizeV>
template<typename Arg >
bool boxes::RingBuffer< T, SizeV >::push_front ( Arg &&  value)
inline

Inserts a new element at the front of the RingBuffer.

Newly inserted elements will be placed at the front of the RingBuffer. Newly inserted element can be accessed using the front method.

Template Parameters
ArgThe type of the element to be inserted
Parameters
valueThe value of the element to be inserted
Returns
true if the element was successfully inserted

Definition at line 360 of file ring_buffer.hpp.

◆ size()

template<typename T , std::size_t SizeV>
size_type boxes::RingBuffer< T, SizeV >::size ( ) const
inline

Returns the number of elements in the RingBuffer.

Returns
The number of elements in the RingBuffer

Definition at line 411 of file ring_buffer.hpp.

◆ swap()

template<typename T , std::size_t SizeV>
void boxes::RingBuffer< T, SizeV >::swap ( RingBuffer< T, SizeV > &  other)
inline

Swaps the contents of this RingBuffer with another.

Required to implement the Container requirements of the C++ standard.

Parameters
otherThe RingBuffer to swap with

Definition at line 324 of file ring_buffer.hpp.


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