|
sdi_toolBox
|
#include <ringBuffer.h>
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.
| T | Element type stored in the buffer. |
| CAPACITY | Compile-time buffer capacity. Must be > 0. |
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. | |
| bool push | ( | const T & | value | ) |
Append a value to the buffer.
If the buffer is full, the oldest element is overwritten.
| value | Value to append (copied). |
Definition at line 196 of file ringBuffer.h.
| std::optional< T > pop | ( | ) |
Remove and return the oldest element from the buffer.
Definition at line 216 of file ringBuffer.h.
| bool pop | ( | T & | value | ) |
Remove the oldest element and store it in the provided reference.
| value | Output reference that receives the removed element. |
Definition at line 229 of file ringBuffer.h.
| std::optional< T > front | ( | ) | const |
Return (without removing) the oldest element.
Definition at line 243 of file ringBuffer.h.
| bool front | ( | T & | value | ) | const |
Copy the oldest element into the provided reference without removing it.
| value | Output reference that receives the element. |
Definition at line 253 of file ringBuffer.h.
| std::optional< T > back | ( | ) | const |
Return (without removing) the newest element.
Definition at line 264 of file ringBuffer.h.
| bool back | ( | T & | value | ) | const |
Copy the newest element into the provided reference without removing it.
| value | Output reference that receives the element. |
Definition at line 274 of file ringBuffer.h.
| bool empty | ( | ) | const |
Check whether the buffer is empty.
Definition at line 285 of file ringBuffer.h.
| bool full | ( | ) | const |
Check whether the buffer is full.
Definition at line 292 of file ringBuffer.h.
| std::size_t size | ( | ) | const |
Number of elements currently stored in the buffer.
Definition at line 299 of file ringBuffer.h.
|
constexpr |
Compile-time capacity of the buffer.
Definition at line 310 of file ringBuffer.h.
| 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.