28template <
typename T,
size_t N>
37 explicit RingBuffer(
bool override_when_full) : override(override_when_full) {}
42 bool push(
const T& value) {
48 read = (read + 1) % N;
52 buffer[write] = value;
53 write = (write + 1) % N;
60 std::optional<T>
pop() {
64 T value = buffer[read];
65 read = (read + 1) % N;
85 return buffer[(write - 1 + N) % N];
94 read = (read + 1) % N;
101 [[nodiscard]]
size_t capacity()
const {
return N; }
105 [[nodiscard]]
bool empty()
const {
return count == 0; }
109 [[nodiscard]]
bool full()
const {
return count == N; }
113 [[nodiscard]]
bool overrides()
const {
return override; }
117 [[nodiscard]]
size_t size()
const {
return count; }
120 std::array<T, N> buffer;
124 bool override =
false;
A fixed-size circular buffer (ring buffer) with FIFO semantics.
std::optional< T > pop()
Removes and returns the front element.
bool overrides() const
Checks if override mode is enabled.
size_t size() const
Returns the current number of elements.
bool empty() const
Checks if the buffer is empty.
RingBuffer(bool override_when_full)
Constructs an empty RingBuffer with configurable override behavior.
bool full() const
Checks if the buffer is full.
std::optional< T > back()
Returns the back element without removing it.
std::optional< T > front()
Returns the front element without removing it.
bool discard()
Discards the front element without returning it.
bool push(const T &value)
Pushes a value to the back of the buffer.
RingBuffer()=default
Constructs an empty RingBuffer with default behavior (reject when full).
size_t capacity() const
Returns the maximum capacity of the buffer.