-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
CircularBuffer.hpp
208 lines (178 loc) · 6 KB
/
CircularBuffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
CircularBuffer.hpp - Circular buffer library for Arduino.
Copyright (c) 2017 Roberto Lo Giacco.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CIRCULAR_BUFFER_H_
#define CIRCULAR_BUFFER_H_
#include <stdint.h>
#include <stddef.h>
#ifdef CIRCULAR_BUFFER_DEBUG
#include <Print.h>
#endif
namespace Helper {
/** @private */
template<bool FITS8, bool FITS16> struct Index {
using Type = uint32_t;
};
/** @private */
template<> struct Index<false, true> {
using Type = uint16_t;
};
/** @private */
template<> struct Index<true, true> {
using Type = uint8_t;
};
}
/**
* @brief Implements a circular buffer that supports LIFO and FIFO operations.
*
* @tparam T The type of the data to store in the buffer.
* @tparam S The maximum number of elements that can be stored in the buffer.
* @tparam IT The data type of the index. Typically should be left as default.
*/
template<typename T, size_t S, typename IT = typename Helper::Index<(S <= UINT8_MAX), (S <= UINT16_MAX)>::Type> class CircularBuffer {
public:
/**
* @brief The buffer capacity.
*
* Read only as it cannot ever change.
*/
static constexpr IT capacity = static_cast<IT>(S);
/**
* @brief Aliases the index type.
*
* Can be used to obtain the right index type with `decltype(buffer)::index_t`.
*/
using index_t = IT;
/**
* @brief Create an empty circular buffer.
*/
constexpr CircularBuffer();
// disable the copy constructor
/** @private */
CircularBuffer(const CircularBuffer&) = delete;
/** @private */
CircularBuffer(CircularBuffer&&) = delete;
// disable the assignment operator
/** @private */
CircularBuffer& operator=(const CircularBuffer&) = delete;
/** @private */
CircularBuffer& operator=(CircularBuffer&&) = delete;
/**
* @brief Adds an element to the beginning of buffer.
*
* @return `false` iff the addition caused overwriting to an existing element.
*/
bool unshift(T value);
/**
* @brief Adds an element to the end of buffer.
*
* @return `false` iff the addition caused overwriting to an existing element.
*/
bool push(T value);
/**
* @brief Removes an element from the beginning of the buffer.
*
* @warning Calling this operation on an empty buffer has an unpredictable behaviour.
*/
T shift();
/**
* @brief Removes an element from the end of the buffer.
*
* @warning Calling this operation on an empty buffer has an unpredictable behaviour.
*/
T pop();
/**
* @brief Returns the element at the beginning of the buffer.
*
* @return The element at the beginning of the buffer.
*/
T inline first() const;
/**
* @brief Returns the element at the end of the buffer.
*
* @return The element at the end of the buffer.
*/
T inline last() const;
/**
* @brief Array-like access to buffer.
*
* Calling this operation using and index value greater than `size - 1` returns the tail element.
*
* @warning Calling this operation on an empty buffer has an unpredictable behaviour.
*/
T operator [] (IT index) const;
/**
* @brief Returns how many elements are actually stored in the buffer.
*
* @return The number of elements stored in the buffer.
*/
IT inline size() const;
/**
* @brief Returns how many elements can be safely pushed into the buffer.
*
* @return The number of elements that can be safely pushed into the buffer.
*/
IT inline available() const;
/**
* @brief Check if the buffer is empty.
*
* @return `true` iff no elements can be removed from the buffer.
*/
bool inline isEmpty() const;
/**
* @brief Check if the buffer is full.
*
* @return `true` if no elements can be added to the buffer without overwriting existing elements.
*/
bool inline isFull() const;
/**
* @brief Resets the buffer to a clean status, making all buffer positions available.
*
* @note This does not clean up any dynamically allocated memory stored in the buffer.
* Clearing a buffer that points to heap-allocated memory may cause a memory leak, if it's not properly cleaned up.
*/
void inline clear();
/**
* @brief Copies the buffer content into the provided array.
*
* @note No verification is done about the provided array length, it's the user responsibility to ensure the array provides enough space to accomodate
* all the elements currently stored in the buffer. After the function returns the elements in the buffer can be found starting at index 0 and up to the buffer size() at the moment of the copyToArray function call.
*/
void copyToArray(T* dest) const;
/**
* @brief Copies the buffer content into the provided array calling the provided conversion function for each and every element of the buffer.
*
* @note No verification is done about the provided array length, it's the user responsibility to ensure the array provides enough space to accomodate
* all the elements currently stored in the buffer. After the function returns the elements in the buffer can be found starting at index 0 and up to the buffer size() at the moment of the copyToArray function call.
*
* @param convertFn the conversion function to call for each item stored in the buffer
*/
template<typename R>
void copyToArray(R* dest, R (&convertFn)(const T&)) const;
#ifdef CIRCULAR_BUFFER_DEBUG
void inline debug(Print* out);
void inline debugFn(Print* out, void (*printFunction)(Print*, T));
#endif
private:
T buffer[S];
T *head;
T *tail;
#ifndef CIRCULAR_BUFFER_INT_SAFE
IT count;
#else
volatile IT count;
#endif
};
#include <CircularBuffer.tpp>
#endif