qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::ring_buffer< T, N, Overwrite > Class Template Reference

A fixed-size ring buffer (circular buffer) implementation. More...

#include <ring_buffer.h>

Public Types

using value_type = T
using reference = T &
using const_reference = T const &
using pointer = T *
using const_pointer = T const *
using size_type = size_t
using iterator = detail::ring_buffer_iterator<T, N, false, Overwrite>
using const_iterator = detail::ring_buffer_iterator<T, N, true, Overwrite>

Public Member Functions

 ring_buffer () noexcept=default
 Default constructor.
 ring_buffer (ring_buffer const &rhs) noexcept(std::is_nothrow_copy_constructible_v< value_type >)
 Copy constructor.
ring_bufferoperator= (ring_buffer const &rhs) noexcept(std::is_nothrow_copy_constructible_v< value_type >)
 Copy assignment operator.
template<typename U>
void push_back (U &&value)
 Add an element to the back of the buffer.
void pop_front () noexcept
 Remove the oldest element from the buffer.
reference back () noexcept
 Access the newest element in the buffer.
const_reference back () const noexcept
 Access the newest element in the buffer (const version)
reference front () noexcept
 Access the oldest element in the buffer.
const_reference front () const noexcept
 Access the oldest element in the buffer (const version)
reference operator[] (size_type index) noexcept
 Access an element by index.
const_reference operator[] (size_type index) const noexcept
 Access an element by index (const version)
iterator begin () noexcept
 Get an iterator to the first element.
iterator end () noexcept
 Get an iterator to the end of the buffer.
const_iterator cbegin () const noexcept
 Get a const iterator to the first element.
const_iterator cend () const noexcept
 Get a const iterator to the end of the buffer.
bool empty () const noexcept
 Check if the buffer is empty.
bool full () const noexcept
 Check if the buffer is full.
size_type capacity () const noexcept
 Get the capacity of the buffer.
void clear () noexcept
 Clear all elements from the buffer.
 ~ring_buffer ()
 Destructor.

Detailed Description

template<typename T, size_t N, bool Overwrite>
class qb::ring_buffer< T, N, Overwrite >

A fixed-size ring buffer (circular buffer) implementation.

Template Parameters
TThe type of elements stored in the ring buffer
NThe capacity of the ring buffer
OverwriteWhether to overwrite old elements when the buffer is full (defaults to true)

Ring buffer is a circular data structure with a fixed size that can efficiently add and remove elements from either end. When the buffer is full, new elements either overwrite the oldest ones (if Overwrite=true) or are discarded (if Overwrite=false).

Template Parameters
TThe type of elements stored in the ring buffer
NThe capacity of the ring buffer
OverwriteWhether to overwrite old elements when the buffer is full (defaults to true)

Constructor & Destructor Documentation

◆ ring_buffer()

template<typename T, size_t N, bool Overwrite>
qb::ring_buffer< T, N, Overwrite >::ring_buffer ( ring_buffer< T, N, Overwrite > const & rhs)
inlinenoexcept

Copy constructor.

Parameters
rhsThe ring buffer to copy from

Member Function Documentation

◆ operator=()

template<typename T, size_t N, bool Overwrite>
ring_buffer & qb::ring_buffer< T, N, Overwrite >::operator= ( ring_buffer< T, N, Overwrite > const & rhs)
inlinenoexcept

Copy assignment operator.

Parameters
rhsThe ring buffer to copy from
Returns
Reference to this ring buffer

◆ push_back()

template<typename T, size_t N, bool Overwrite>
template<typename U>
void qb::ring_buffer< T, N, Overwrite >::push_back ( U && value)
inline

Add an element to the back of the buffer.

If the buffer is full and Overwrite is true, the oldest element will be overwritten. If the buffer is full and Overwrite is false, the element will be discarded.

Template Parameters
UType of the value to add
Parameters
valueThe value to add

◆ back() [1/2]

template<typename T, size_t N, bool Overwrite>
reference qb::ring_buffer< T, N, Overwrite >::back ( )
inlinenodiscardnoexcept

Access the newest element in the buffer.

Returns
Reference to the newest element

◆ back() [2/2]

template<typename T, size_t N, bool Overwrite>
const_reference qb::ring_buffer< T, N, Overwrite >::back ( ) const
inlinenodiscardnoexcept

Access the newest element in the buffer (const version)

Returns
Const reference to the newest element

◆ front() [1/2]

template<typename T, size_t N, bool Overwrite>
reference qb::ring_buffer< T, N, Overwrite >::front ( )
inlinenodiscardnoexcept

Access the oldest element in the buffer.

Returns
Reference to the oldest element

◆ front() [2/2]

template<typename T, size_t N, bool Overwrite>
const_reference qb::ring_buffer< T, N, Overwrite >::front ( ) const
inlinenodiscardnoexcept

Access the oldest element in the buffer (const version)

Returns
Const reference to the oldest element

◆ operator[]() [1/2]

template<typename T, size_t N, bool Overwrite>
reference qb::ring_buffer< T, N, Overwrite >::operator[] ( size_type index)
inlinenodiscardnoexcept

Access an element by index.

Parameters
indexThe index of the element to access
Returns
Reference to the element at the specified index

◆ operator[]() [2/2]

template<typename T, size_t N, bool Overwrite>
const_reference qb::ring_buffer< T, N, Overwrite >::operator[] ( size_type index) const
inlinenodiscardnoexcept

Access an element by index (const version)

Parameters
indexThe index of the element to access
Returns
Const reference to the element at the specified index

◆ begin()

template<typename T, size_t N, bool Overwrite>
iterator qb::ring_buffer< T, N, Overwrite >::begin ( )
inlinenodiscardnoexcept

Get an iterator to the first element.

Returns
Iterator to the first element

◆ end()

template<typename T, size_t N, bool Overwrite>
iterator qb::ring_buffer< T, N, Overwrite >::end ( )
inlinenodiscardnoexcept

Get an iterator to the end of the buffer.

Returns
Iterator to the position after the last element

◆ cbegin()

template<typename T, size_t N, bool Overwrite>
const_iterator qb::ring_buffer< T, N, Overwrite >::cbegin ( ) const
inlinenodiscardnoexcept

Get a const iterator to the first element.

Returns
Const iterator to the first element

◆ cend()

template<typename T, size_t N, bool Overwrite>
const_iterator qb::ring_buffer< T, N, Overwrite >::cend ( ) const
inlinenodiscardnoexcept

Get a const iterator to the end of the buffer.

Returns
Const iterator to the position after the last element

◆ empty()

template<typename T, size_t N, bool Overwrite>
bool qb::ring_buffer< T, N, Overwrite >::empty ( ) const
inlinenodiscardnoexcept

Check if the buffer is empty.

Returns
true if the buffer is empty, false otherwise

◆ full()

template<typename T, size_t N, bool Overwrite>
bool qb::ring_buffer< T, N, Overwrite >::full ( ) const
inlinenodiscardnoexcept

Check if the buffer is full.

Returns
true if the buffer is full, false otherwise

◆ capacity()

template<typename T, size_t N, bool Overwrite>
size_type qb::ring_buffer< T, N, Overwrite >::capacity ( ) const
inlinenodiscardnoexcept

Get the capacity of the buffer.

Returns
The maximum number of elements the buffer can hold