Skip to content

Class loon::RingBuffer

template <typename T, size_t N>

ClassList > loon > RingBuffer

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

  • #include <ring_buffer.hpp>

Public Functions

Type Name
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.
std::optional< T > back ()
Returns the back element without removing it.
size_t capacity () const
Returns the maximum capacity of the buffer.
bool discard ()
Discards the front element without returning it.
bool empty () const
Checks if the buffer is empty.
std::optional< T > front ()
Returns the front element without removing it.
bool full () const
Checks if the buffer is full.
bool overrides () const
Checks if override mode is enabled.
std::optional< T > pop ()
Removes and returns the front element.
bool push (const T & value)
Pushes a value to the back of the buffer.
size_t size () const
Returns the current number of elements.

Detailed Description

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:

  • T The element type to store.
  • N The fixed capacity of the buffer (must be > 0).

** **

loon::RingBuffer<int, 10> buffer;
buffer.push(42);
buffer.push(43);
auto val = buffer.pop();  // returns 42

Public Functions Documentation

function RingBuffer [1/2]

Constructs an empty RingBuffer with default behavior (reject when full).

loon::RingBuffer::RingBuffer () = default


function RingBuffer [2/2]

Constructs an empty RingBuffer with configurable override behavior.

inline explicit loon::RingBuffer::RingBuffer (
    bool override_when_full
) 

Parameters:

  • override_when_full If true, push() overwrites oldest element when full. If false, push() returns false when full.

function back

Returns the back element without removing it.

inline std::optional< T > loon::RingBuffer::back () 

Returns:

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


function capacity

Returns the maximum capacity of the buffer.

inline size_t loon::RingBuffer::capacity () const

Returns:

The compile-time capacity N.


function discard

Discards the front element without returning it.

inline bool loon::RingBuffer::discard () 

Returns:

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


function empty

Checks if the buffer is empty.

inline bool loon::RingBuffer::empty () const

Returns:

true if the buffer contains no elements.


function front

Returns the front element without removing it.

inline std::optional< T > loon::RingBuffer::front () 

Returns:

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


function full

Checks if the buffer is full.

inline bool loon::RingBuffer::full () const

Returns:

true if the buffer contains N elements.


function overrides

Checks if override mode is enabled.

inline bool loon::RingBuffer::overrides () const

Returns:

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


function pop

Removes and returns the front element.

inline std::optional< T > loon::RingBuffer::pop () 

Returns:

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


function push

Pushes a value to the back of the buffer.

inline bool loon::RingBuffer::push (
    const T & value
) 

Parameters:

  • value The value to push (copied).

Returns:

true if the value was added, false if buffer is full and override is disabled.


function size

Returns the current number of elements.

inline size_t loon::RingBuffer::size () const

Returns:

The number of elements in the buffer.



The documentation for this class was generated from the following file include/loon/ring_buffer.hpp