Template Class CircularBuffer

Nested Relationships

Nested Types

Class Documentation

template<typename T>
class CircularBuffer

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.

Public Functions

inline explicit CircularBuffer(std::size_t capacity)

Construct a buffer with the given capacity.

inline CircularBuffer(const CircularBuffer &other)

Copy constructor.

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

inline CircularBuffer &operator=(const CircularBuffer &other)

Copy assignment.

CircularBuffer(CircularBuffer&&) = delete

Move operations are optional. For now we delete them to avoid surprising semantics.

CircularBuffer &operator=(CircularBuffer&&) = delete
inline std::size_t capacity() const noexcept

Maximum number of elements that can be stored.

inline std::size_t size() const noexcept

Current number of valid elements.

inline bool empty() const noexcept

True if the buffer is empty.

inline bool full() const noexcept

True if the buffer is full.

inline void clear() noexcept

Remove all elements, keeping the allocated storage.

inline void push(const T &value)

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

inline void push(T &&value)

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

inline bool pop(T &out)

Pop the oldest element.

Parameters:

out – Destination for the oldest element.

Returns:

False if the buffer is empty.

inline bool latest(T &out) const

Get a copy of the newest element without removing it.

Parameters:

out – Destination for the newest element.

Returns:

False if the buffer is empty.

inline const T &latest_ref() const

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

inline DebugSlotView raw_slot(std::size_t idx) const
struct DebugSlotView

Public Members

bool has_value
const T &value