Loading...
Searching...
No Matches
containers::RingBuffer< T, N > Class Template Reference

A fixed-capacity circular buffer. More...

#include <containers.hpp>

Public Member Functions

 RingBuffer ()=default
constexpr std::size_t capacity () const
 Get the maximum capacity.
bool empty () const
 Check if the buffer is empty.
bool full () const
 Check if the buffer is at capacity.
T read ()
 Read and remove the oldest value.
std::size_t size () const
 Get the number of elements currently stored.
void write (const T &value)
 Write a value to the buffer.

Detailed Description

template<typename T, std::size_t N>
class containers::RingBuffer< T, N >

A fixed-capacity circular buffer.

Template Parameters
TElement type.
NMaximum capacity (compile-time constant).

Uses a head/tail index scheme:

\[ \text{next}(i) = (i + 1) \bmod N \]

Warning
Writing to a full buffer throws BufferOverflow. Use full() to check before writing.
Todo
Add an overwrite-on-full policy as a template parameter.

Definition at line 151 of file containers.hpp.

Constructor & Destructor Documentation

◆ RingBuffer()

template<typename T, std::size_t N>
containers::RingBuffer< T, N >::RingBuffer ( )
default

Member Function Documentation

◆ capacity()

template<typename T, std::size_t N>
std::size_t containers::RingBuffer< T, N >::capacity ( ) const
inlinenodiscardconstexpr

Get the maximum capacity.

Definition at line 190 of file containers.hpp.

190{ return N; }
A fixed-capacity circular buffer.

◆ empty()

template<typename T, std::size_t N>
bool containers::RingBuffer< T, N >::empty ( ) const
inlinenodiscard

Check if the buffer is empty.

Definition at line 181 of file containers.hpp.

181{ return size_ == 0; }

Referenced by read().

◆ full()

template<typename T, std::size_t N>
bool containers::RingBuffer< T, N >::full ( ) const
inlinenodiscard

Check if the buffer is at capacity.

Definition at line 184 of file containers.hpp.

184{ return size_ == N; }

Referenced by write().

◆ read()

template<typename T, std::size_t N>
T containers::RingBuffer< T, N >::read ( )
inline

Read and remove the oldest value.

Returns
The oldest value.
Exceptions
BufferUnderflowif the buffer is empty.

Definition at line 172 of file containers.hpp.

172 {
173 if (empty()) throw BufferUnderflow();
174 T val = buf_[head_];
175 head_ = (head_ + 1) % N;
176 --size_;
177 return val;
178 }
bool empty() const
Check if the buffer is empty.

References empty().

◆ size()

template<typename T, std::size_t N>
std::size_t containers::RingBuffer< T, N >::size ( ) const
inlinenodiscard

Get the number of elements currently stored.

Definition at line 187 of file containers.hpp.

187{ return size_; }

◆ write()

template<typename T, std::size_t N>
void containers::RingBuffer< T, N >::write ( const T & value)
inline

Write a value to the buffer.

Parameters
valueThe value to enqueue.
Exceptions
BufferOverflowif the buffer is full.

Definition at line 160 of file containers.hpp.

160 {
161 if (full()) throw BufferOverflow();
162 buf_[tail_] = value;
163 tail_ = (tail_ + 1) % N;
164 ++size_;
165 }
bool full() const
Check if the buffer is at capacity.

References full().


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