30 ~
RingBuffer() {
if (_aucBuffer!=
nullptr)
delete[] _aucBuffer; }
36 uint8_t value = _aucBuffer[_iTail];
37 _iTail = nextIndex(_iTail);
44 bool isFull() {
return available() == max_size; }
46 bool isEmpty() {
return available() == 0; }
49 bool write(uint8_t data) {
52 _aucBuffer[_iHead] = data;
53 _iHead = nextIndex(_iHead);
60 int write(
const uint8_t *data,
size_t len) {
61 for (
int j = 0; j < len; j++) {
62 if (!write(data[j])) {
77 int available() {
return _numElems; }
80 int availableForWrite() {
return (max_size - _numElems); }
82 void resize(
int len) {
84 if (this->max_size==len && _aucBuffer!=
nullptr){
88 if (_aucBuffer !=
nullptr) {
93 _aucBuffer =
new uint8_t[max_size];
94 assert(_aucBuffer !=
nullptr);
100 int size() {
return max_size; }
107 return _aucBuffer[_iTail];
112 if (idx > available())
114 int tmp_tail = (_iTail + idx) % max_size;
115 return _aucBuffer[tmp_tail];
120 if (len==0)
return 0;
121 static uint8_t tmp_copy[PEEK_MAX];
122 assert(len < PEEK_MAX);
123 assert(idx+len<available());
124 memset(&tmp_copy, 0, PEEK_MAX);
125 for (
int j = 0; j < len; j++) {
126 tmp_copy[j] = peek(idx + j);
133 int result = peek(idx);
140 int len = strlen(str);
141 return strncmp(str, (
char*)
peekStr(0, len), len) == 0;
146 assert(available() >= offset);
147 for (
int j = 0; j < offset; j++) {
153 uint8_t *_aucBuffer =
nullptr;
159 int nextIndex(
int index) {
return (uint32_t)(index + 1) % max_size; }
RingBuffer that supports extensive peek operations to access and compare the next values.
Definition: RingBuffer.h:16
uint8_t operator[](int idx)
alternaive syntax to peek(idx)
Definition: RingBuffer.h:132
void consume(int offset)
Removes the next n characters from the ringbuffer.
Definition: RingBuffer.h:145
bool equals(const char *str)
Compares the string with the current peek values.
Definition: RingBuffer.h:139
uint8_t * peekStr(int idx, int len)
returns a temporary copy of the requested bytes
Definition: RingBuffer.h:119
int size()
Returns the maximum capacity of the buffer.
Definition: RingBuffer.h:100
int peek(int idx)
peeks the idx next entry from the buffer
Definition: RingBuffer.h:111