Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_CircularBuffer.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Feb 27, 2015
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
9#include <cfg/cs_Strings.h>
10#include <logging/cs_Logger.h>
11
12#include <cstdlib>
13
14#include "common/cs_Types.h"
15
16#define LOGCircularBufferDebug LOGvv
17
18#define CS_CIRCULAR_BUFFER_INDEX_NOT_FOUND 0xFFFF
19
26template <class T>
28public:
32 : _array(nullptr), _capacity(capacity), _head(0), _tail(-1), _contentsSize(0), _allocatedSelf(false) {}
33
36 virtual ~CircularBuffer() { deinit(); }
37
38 uint16_t getMaxByteSize(uint16_t capacity) { return capacity * sizeof(T); }
39 uint16_t getMaxByteSize() { return getMaxByteSize(_capacity); }
40 uint16_t getMaxSize(uint16_t byteSize) { return byteSize / sizeof(T); }
41
48 bool init() {
49 if (_array != nullptr) {
50 return false;
51 }
52 // Allocate memory
54 _array = (T*)calloc(_capacity, sizeof(T));
55 if (_array == nullptr) {
57 return false;
58 }
59
60 _allocatedSelf = true;
61 // Also call clear to make sure we start with a clean buffer
62 clear();
63 return true;
64 }
65
69 bool deinit() {
70 if (_array != nullptr && _allocatedSelf) {
71 free(_array);
72 }
73 _allocatedSelf = false;
74 _array = nullptr;
75 return true;
76 }
77
84 bool assign(buffer_ptr_t buffer, uint16_t bufferSize) {
85 if (getMaxSize(bufferSize) < _capacity || _allocatedSelf) {
86 LOGd(FMT_ERR_ASSIGN_BUFFER, buffer, bufferSize);
87 return false;
88 }
90 _array = (T*)buffer;
91
92 // Also call clear to make sure we start with a clean buffer
93 clear();
94 return true;
95 }
96
101 bool release() {
102 if (_allocatedSelf) {
103 return false;
104 }
105 _array = nullptr;
106 return true;
107 }
108
112 bool isInitialized() { return _array != nullptr; }
113
114 T* getBuffer() { return _array; }
115
122 void clear() {
123 _head = 0;
124 _tail = -1;
125 _contentsSize = 0;
126 }
127
132 uint16_t size() const { return _contentsSize; }
133
141 uint16_t capacity() const { return _capacity; }
142
147 bool empty() const { return size() == 0; }
148
153 bool full() const { return size() == capacity(); }
154
163 void push(const T& value) {
164 incTail();
165 if (_contentsSize > _capacity) {
166 incHead();
167 }
168 _array[_tail] = value;
169 }
170
177 bool pushUnique(const T& value) {
179 push(value);
180 return true;
181 }
182 return false;
183 }
184
192 const T& pop() {
193 T* res = &(_array[_head]);
194 incHead();
195 return *res;
196 }
197
206 T& peek() const { return _array[_head]; }
207
212 T& operator[](uint16_t idx) const { return _array[(_head + idx) % _capacity]; }
213
220 uint16_t find(const T& value) const {
221 for (uint16_t index = 0; index < size(); ++index) {
222 if (operator[](index) == value) {
223 return index;
224 }
225 }
227 }
228
229private:
232
234 uint16_t _capacity;
235
237 uint16_t _head;
238
240 uint16_t _tail;
241
244
247
253 void incTail() {
254 ++_tail;
255 _tail %= _capacity;
257 }
258
264 void incHead() {
265 ++_head;
266 _head %= _capacity;
268 }
269};
Circular Buffer implementation.
Definition: cs_CircularBuffer.h:27
virtual ~CircularBuffer()
Default destructor.
Definition: cs_CircularBuffer.h:36
uint16_t size() const
Returns the number of elements stored.
Definition: cs_CircularBuffer.h:132
bool release()
Release the buffer that was assigned.
Definition: cs_CircularBuffer.h:101
void clear()
Clears the buffer.
Definition: cs_CircularBuffer.h:122
uint16_t getMaxByteSize(uint16_t capacity)
Definition: cs_CircularBuffer.h:38
bool full() const
Checks if the buffer is full.
Definition: cs_CircularBuffer.h:153
uint16_t getMaxByteSize()
Definition: cs_CircularBuffer.h:39
uint16_t getMaxSize(uint16_t byteSize)
Definition: cs_CircularBuffer.h:40
CircularBuffer(uint16_t capacity)
Default constructor.
Definition: cs_CircularBuffer.h:31
uint16_t find(const T &value) const
Find a value in the buffer.
Definition: cs_CircularBuffer.h:220
bool _allocatedSelf
Whether the array was allocated by init() or not.
Definition: cs_CircularBuffer.h:246
uint16_t capacity() const
Returns the capacity of the buffer.
Definition: cs_CircularBuffer.h:141
void incHead()
Increases the head.
Definition: cs_CircularBuffer.h:264
bool isInitialized()
Returns true when the buffer has been allocated, either via init() or via assign().
Definition: cs_CircularBuffer.h:112
bool empty() const
Checks if the buffer is empty.
Definition: cs_CircularBuffer.h:147
uint16_t _capacity
The capacity of the buffer (maximum number of elements)
Definition: cs_CircularBuffer.h:234
const T & pop()
Get the oldest element.
Definition: cs_CircularBuffer.h:192
uint16_t _head
Index of the head (next element to be removed)
Definition: cs_CircularBuffer.h:237
T * getBuffer()
Definition: cs_CircularBuffer.h:114
bool assign(buffer_ptr_t buffer, uint16_t bufferSize)
Assign the buffer used to store the data, instead of allocating it via init().
Definition: cs_CircularBuffer.h:84
T & operator[](uint16_t idx) const
Returns the Nth value, starting from oldest element.
Definition: cs_CircularBuffer.h:212
bool pushUnique(const T &value)
Add an element to the end of the buffer, but only when it's not already in the buffer.
Definition: cs_CircularBuffer.h:177
bool init()
Initializes and allocates memory for the buffer based on the capacity.
Definition: cs_CircularBuffer.h:48
bool deinit()
Definition: cs_CircularBuffer.h:69
void incTail()
Increases the tail.
Definition: cs_CircularBuffer.h:253
void push(const T &value)
Add an element to the end of the buffer.
Definition: cs_CircularBuffer.h:163
uint16_t _contentsSize
Number of elements stored in the buffer.
Definition: cs_CircularBuffer.h:243
uint16_t _tail
Index of the tail (where the next element will be inserted)
Definition: cs_CircularBuffer.h:240
T & peek() const
Peek at the oldest element without removing it.
Definition: cs_CircularBuffer.h:206
T * _array
Pointer to the array storing the elements.
Definition: cs_CircularBuffer.h:231
#define LOGCircularBufferDebug
Definition: cs_CircularBuffer.h:16
#define CS_CIRCULAR_BUFFER_INDEX_NOT_FOUND
Definition: cs_CircularBuffer.h:18
#define LOGw(fmt,...)
Definition: cs_Logger.h:92
#define LOGd(fmt,...)
Definition: cs_Logger.h:90
#define FMT_ALLOCATE_MEMORY
Definition: cs_Strings.h:186
#define FMT_ERR_ASSIGN_BUFFER
Definition: cs_Strings.h:187
#define STR_ERR_ALLOCATE_MEMORY
Definition: cs_Strings.h:102
#define FMT_ASSIGN_BUFFER_LEN
Definition: cs_Strings.h:188
uint8_t * buffer_ptr_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 10 May....
Definition: cs_Typedefs.h:19