26 RingBuffer(
size_t size = 128) { resize(size); }
40 bool write(
const T&
value) {
41 if (isFull())
return false;
42 _buffer[_tail] =
value;
43 _tail = (_tail + 1) % _capacity;
48 int writeArray(
const T* data,
size_t len) {
50 for (
size_t i = 0;
i <
len; ++
i) {
60 size_t available()
const {
return _count; }
68 bool isFull()
const {
return _count == _capacity; }
70 bool isEmpty()
const {
return _count == 0; }
72 size_t size()
const {
return _capacity; }
76 if (_count == 0)
return false;
78 _head = (_head + 1) % _capacity;
86 if (_count == 0)
return -1;
88 _head = (_head + 1) % _capacity;
90 return static_cast<int>(
value);
96 while (
n <
static_cast<int>(
len) && _count > 0) {
97 dest[
n] = _buffer[_head];
98 _head = (_head + 1) % _capacity;
107 if (_count == 0)
return false;
108 out = _buffer[_head];
114 if (_count == 0)
return -1;
115 return static_cast<int>(_buffer[_head]);
122 T* _buffer =
nullptr;
123 size_t _capacity = 0;
Efficient lock-free ring buffer for storing data.
size_t availableForWrite() const
Returns available space for writing.
bool read(T &out)
Read and remove the next element. Returns true if successful.
int peek() const
Peek (uint8_t specialization compatibility). Returns -1 if empty.
int readArray(T *dest, size_t len)
Read up to len elements into dest, returns number of elements read.
bool peek(T &out) const
Peek at the next element without removing it. Returns true if successful.
Small, header-only vector replacement for non-STL environments.