Easy Navigation
Loading...
Searching...
No Matches
CircularBuffer< T > Class Template Reference

Fixed-size circular buffer, thread-safe (mutex-based), copyable. More...

#include <CircularBuffer.hpp>

Classes

struct  DebugSlotView
 

Public Member Functions

std::size_t capacity () const noexcept
 Maximum number of elements that can be stored.
 
 CircularBuffer (CircularBuffer &&)=delete
 Move operations are optional.
 
 CircularBuffer (const CircularBuffer &other)
 Copy constructor.
 
 CircularBuffer (std::size_t capacity)
 Construct a buffer with the given capacity.
 
void clear () noexcept
 Remove all elements, keeping the allocated storage.
 
bool empty () const noexcept
 True if the buffer is empty.
 
bool full () const noexcept
 True if the buffer is full.
 
bool latest (T &out) const
 Get a copy of the newest element without removing it.
 
const T & latest_ref () const
 Get a const reference to the newest element without removing it.
 
CircularBufferoperator= (CircularBuffer &&)=delete
 
CircularBufferoperator= (const CircularBuffer &other)
 Copy assignment.
 
bool pop (T &out)
 Pop the oldest element.
 
void push (const T &value)
 Push a new element (copy). Overwrites the oldest if the buffer is full.
 
void push (T &&value)
 Push a new element (move). Overwrites the oldest if the buffer is full.
 
DebugSlotView raw_slot (std::size_t idx) const
 
std::size_t size () const noexcept
 Current number of valid elements.
 

Detailed Description

template<typename T>
class easynav::CircularBuffer< T >

Fixed-size circular buffer, thread-safe (mutex-based), copyable.

All storage is allocated in the constructor. Push and pop never allocate. Copying the buffer creates a new internal storage and copies the content. This class is safe for multiple producers and consumers, but access is serialized by a single mutex.

Constructor & Destructor Documentation

◆ CircularBuffer() [1/3]

template<typename T>
CircularBuffer ( std::size_t capacity)
explicit

Construct a buffer with the given capacity.

◆ CircularBuffer() [2/3]

template<typename T>
CircularBuffer ( const CircularBuffer< T > & other)

Copy constructor.

Creates a new buffer with the same capacity and copies all elements and index state. The internal mutex is not shared.

◆ CircularBuffer() [3/3]

template<typename T>
CircularBuffer ( CircularBuffer< T > && )
delete

Move operations are optional.

For now we delete them to avoid surprising semantics.

Member Function Documentation

◆ capacity()

template<typename T>
std::size_t capacity ( ) const
noexcept

Maximum number of elements that can be stored.

◆ clear()

template<typename T>
void clear ( )
noexcept

Remove all elements, keeping the allocated storage.

◆ empty()

template<typename T>
bool empty ( ) const
noexcept

True if the buffer is empty.

◆ full()

template<typename T>
bool full ( ) const
noexcept

True if the buffer is full.

◆ latest()

template<typename T>
bool latest ( T & out) const

Get a copy of the newest element without removing it.

Parameters
outDestination for the newest element.
Returns
False if the buffer is empty.

◆ latest_ref()

template<typename T>
const T & latest_ref ( ) const

Get a const reference to the newest element without removing it.

◆ operator=() [1/2]

template<typename T>
CircularBuffer & operator= ( CircularBuffer< T > && )
delete

◆ operator=() [2/2]

template<typename T>
CircularBuffer & operator= ( const CircularBuffer< T > & other)

Copy assignment.

◆ pop()

template<typename T>
bool pop ( T & out)

Pop the oldest element.

Parameters
outDestination for the oldest element.
Returns
False if the buffer is empty.

◆ push() [1/2]

template<typename T>
void push ( const T & value)

Push a new element (copy). Overwrites the oldest if the buffer is full.

◆ push() [2/2]

template<typename T>
void push ( T && value)

Push a new element (move). Overwrites the oldest if the buffer is full.

◆ raw_slot()

template<typename T>
DebugSlotView raw_slot ( std::size_t idx) const

◆ size()

template<typename T>
std::size_t size ( ) const
noexcept

Current number of valid elements.


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