RingBuffer< T, CAPACITY > Class Template Reference

`#include <ringBuffer.h>`

## Detailed Description

template<class T, std::size_t CAPACITY>
class sdi_toolBox::common::utils::RingBuffer< T, CAPACITY >

Fixed-size ring (circular) buffer with compile-time capacity and overwrite-on-full behavior.

This template implements a simple circular buffer with a compile-time fixed capacity. When the buffer is full and a new element is pushed, the oldest element is overwritten.

Template Parameters

  T | Element type stored in the buffer.
  CAPACITY | Compile-time buffer capacity. Must be > 0.

Note
  The class provides both optional-returning and out-parameter overloads for pop/front/back to suit different runtime constraints.

Warning
  Instantiating with CAPACITY == 0 is forbidden (static_assert).

Example:
  RingBuffer<int, 8> rb;
  rb.push(1);
  int v;
  if (rb.pop(v))
  {
  ...
  }
  sdi_toolBox::common::utils::RingBuffer
  Fixed-size ring (circular) buffer with compile-time capacity and overwrite-on-full behavior.
  Definition ringBuffer.h:50
  sdi_toolBox::common::utils::RingBuffer::push
  bool push(const T &value)
  Append a value to the buffer.
  Definition ringBuffer.h:196
  sdi_toolBox::common::utils::RingBuffer::pop
  std::optional< T > pop()
  Remove and return the oldest element from the buffer.
  Definition ringBuffer.h:216

Definition at line 49 of file ringBuffer.h.

## Public Member Functions

Write operations
bool | push (const T &value)
| Append a value to the buffer.
Read operations
std::optional< T > | pop ()
| Remove and return the oldest element from the buffer.
bool | pop (T &value)
| Remove the oldest element and store it in the provided reference.
std::optional< T > | front () const
| Return (without removing) the oldest element.
bool | front (T &value) const
| Copy the oldest element into the provided reference without removing it.
std::optional< T > | back () const
| Return (without removing) the newest element.
bool | back (T &value) const
| Copy the newest element into the provided reference without removing it.
State queries
bool | empty () const
| Check whether the buffer is empty.
bool | full () const
| Check whether the buffer is full.
std::size_t | size () const
| Number of elements currently stored in the buffer.
constexpr std::size_t | capacity () const
| Compile-time capacity of the buffer.
Modifiers
void | clear ()
| Clear the buffer and reset internal indices.

## Member Function Documentation

## push()

template<class T , std::size_t CAPACITY>

bool push | ( | const T & | value | ) |

Append a value to the buffer.

If the buffer is full, the oldest element is overwritten.

Parameters

  value | Value to append (copied).

Returns
  true if the value was added without overwriting, false if an overwrite occurred.

Definition at line 196 of file ringBuffer.h.

## pop() [1/2]

template<class T , std::size_t CAPACITY>

std::optional< T > pop | ( |  | ) |

Remove and return the oldest element from the buffer.

Returns
  std::optional<T> The oldest element if present, std::nullopt if empty.

Definition at line 216 of file ringBuffer.h.

## pop() [2/2]

template<class T , std::size_t CAPACITY>

bool pop | ( | T & | value | ) |

Remove the oldest element and store it in the provided reference.

Parameters

  value | Output reference that receives the removed element.

Returns
  true if an element was removed, false if the buffer was empty.

Definition at line 229 of file ringBuffer.h.

## front() [1/2]

template<class T , std::size_t CAPACITY>

std::optional< T > front | ( |  | ) | const

Return (without removing) the oldest element.

Returns
  std::optional<T> The oldest element if present, std::nullopt if empty.

Definition at line 243 of file ringBuffer.h.

## front() [2/2]

template<class T , std::size_t CAPACITY>

bool front | ( | T & | value | ) | const

Copy the oldest element into the provided reference without removing it.

Parameters

  value | Output reference that receives the element.

Returns
  true if the element was copied, false if the buffer is empty.

Definition at line 253 of file ringBuffer.h.

## back() [1/2]

template<class T , std::size_t CAPACITY>

std::optional< T > back | ( |  | ) | const

Return (without removing) the newest element.

Returns
  std::optional<T> The newest element if present, std::nullopt if empty.

Definition at line 264 of file ringBuffer.h.

## back() [2/2]

template<class T , std::size_t CAPACITY>

bool back | ( | T & | value | ) | const

Copy the newest element into the provided reference without removing it.

Parameters

  value | Output reference that receives the element.

Returns
  true if the element was copied, false if the buffer is empty.

Definition at line 274 of file ringBuffer.h.

## empty()

template<class T , std::size_t CAPACITY>

bool empty | ( |  | ) | const

Check whether the buffer is empty.

Returns
  true if empty, false otherwise.

Definition at line 285 of file ringBuffer.h.

## full()

template<class T , std::size_t CAPACITY>

bool full | ( |  | ) | const

Check whether the buffer is full.

Returns
  true if full, false otherwise.

Definition at line 292 of file ringBuffer.h.

## size()

template<class T , std::size_t CAPACITY>

std::size_t size | ( |  | ) | const

Number of elements currently stored in the buffer.

Returns
  Current size (0 .. CAPACITY).

Definition at line 299 of file ringBuffer.h.

## capacity()

template<class T , std::size_t CAPACITY>

constexpr std::size_t capacity | ( |  | ) | const

constexpr

Compile-time capacity of the buffer.

Returns
  The maximum number of elements the buffer can hold.

Definition at line 310 of file ringBuffer.h.

## clear()

template<class T , std::size_t CAPACITY>

void clear | ( |  | ) |

Clear the buffer and reset internal indices.

After calling clear(), empty() returns true and size() returns 0.

Definition at line 317 of file ringBuffer.h.