loon
High-performance header-only C++ library for low-latency applications
Loading...
Searching...
No Matches
loon::RingBuffer< T, N > Class Template Reference

A fixed-size circular buffer (ring buffer) with FIFO semantics. More...

#include <ring_buffer.hpp>

Public Member Functions

 RingBuffer ()=default
 Constructs an empty RingBuffer with default behavior (reject when full).
 
 RingBuffer (bool override_when_full)
 Constructs an empty RingBuffer with configurable override behavior.
 
bool push (const T &value)
 Pushes a value to the back of the buffer.
 
std::optional< T > pop ()
 Removes and returns the front element.
 
std::optional< T > front ()
 Returns the front element without removing it.
 
std::optional< T > back ()
 Returns the back element without removing it.
 
bool discard ()
 Discards the front element without returning it.
 
size_t capacity () const
 Returns the maximum capacity of the buffer.
 
bool empty () const
 Checks if the buffer is empty.
 
bool full () const
 Checks if the buffer is full.
 
bool overrides () const
 Checks if override mode is enabled.
 
size_t size () const
 Returns the current number of elements.
 

Detailed Description

template<typename T, size_t N>
class loon::RingBuffer< T, N >

A fixed-size circular buffer (ring buffer) with FIFO semantics.

RingBuffer provides O(1) push and pop operations with a compile-time fixed capacity. When full, it can either reject new elements or override the oldest element depending on configuration.

Template Parameters
TThe element type to store.
NThe fixed capacity of the buffer (must be > 0).
Example
buffer.push(42);
buffer.push(43);
auto val = buffer.pop(); // returns 42
A fixed-size circular buffer (ring buffer) with FIFO semantics.
std::optional< T > pop()
Removes and returns the front element.
bool push(const T &value)
Pushes a value to the back of the buffer.

Definition at line 29 of file ring_buffer.hpp.

Constructor & Destructor Documentation

◆ RingBuffer()

template<typename T , size_t N>
loon::RingBuffer< T, N >::RingBuffer ( bool  override_when_full)
inlineexplicit

Constructs an empty RingBuffer with configurable override behavior.

Parameters
override_when_fullIf true, push() overwrites oldest element when full. If false, push() returns false when full.

Definition at line 37 of file ring_buffer.hpp.

Member Function Documentation

◆ back()

template<typename T , size_t N>
std::optional< T > loon::RingBuffer< T, N >::back ( )
inline

Returns the back element without removing it.

Returns
The back element, or std::nullopt if empty.

Definition at line 81 of file ring_buffer.hpp.

References loon::RingBuffer< T, N >::empty().

◆ capacity()

template<typename T , size_t N>
size_t loon::RingBuffer< T, N >::capacity ( ) const
inline

Returns the maximum capacity of the buffer.

Returns
The compile-time capacity N.

Definition at line 101 of file ring_buffer.hpp.

◆ discard()

template<typename T , size_t N>
bool loon::RingBuffer< T, N >::discard ( )
inline

Discards the front element without returning it.

Returns
true if an element was discarded, false if buffer was empty.

Definition at line 90 of file ring_buffer.hpp.

References loon::RingBuffer< T, N >::empty().

◆ empty()

template<typename T , size_t N>
bool loon::RingBuffer< T, N >::empty ( ) const
inline

Checks if the buffer is empty.

Returns
true if the buffer contains no elements.

Definition at line 105 of file ring_buffer.hpp.

Referenced by loon::RingBuffer< T, N >::back(), loon::RingBuffer< T, N >::discard(), loon::RingBuffer< T, N >::front(), and loon::RingBuffer< T, N >::pop().

◆ front()

template<typename T , size_t N>
std::optional< T > loon::RingBuffer< T, N >::front ( )
inline

Returns the front element without removing it.

Returns
The front element, or std::nullopt if empty.

Definition at line 72 of file ring_buffer.hpp.

References loon::RingBuffer< T, N >::empty().

◆ full()

template<typename T , size_t N>
bool loon::RingBuffer< T, N >::full ( ) const
inline

Checks if the buffer is full.

Returns
true if the buffer contains N elements.

Definition at line 109 of file ring_buffer.hpp.

Referenced by loon::RingBuffer< T, N >::push().

◆ overrides()

template<typename T , size_t N>
bool loon::RingBuffer< T, N >::overrides ( ) const
inline

Checks if override mode is enabled.

Returns
true if push() will override oldest element when full.

Definition at line 113 of file ring_buffer.hpp.

◆ pop()

template<typename T , size_t N>
std::optional< T > loon::RingBuffer< T, N >::pop ( )
inline

Removes and returns the front element.

Returns
The front element, or std::nullopt if empty.

Definition at line 60 of file ring_buffer.hpp.

References loon::RingBuffer< T, N >::empty().

◆ push()

template<typename T , size_t N>
bool loon::RingBuffer< T, N >::push ( const T &  value)
inline

Pushes a value to the back of the buffer.

Parameters
valueThe value to push (copied).
Returns
true if the value was added, false if buffer is full and override is disabled.

Definition at line 42 of file ring_buffer.hpp.

References loon::RingBuffer< T, N >::full().

◆ size()

template<typename T , size_t N>
size_t loon::RingBuffer< T, N >::size ( ) const
inline

Returns the current number of elements.

Returns
The number of elements in the buffer.

Definition at line 117 of file ring_buffer.hpp.


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