forked from rlogiacco/CircularBuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularBuffer.h
More file actions
105 lines (84 loc) · 2.5 KB
/
CircularBuffer.h
File metadata and controls
105 lines (84 loc) · 2.5 KB
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
/*
CircularBuffer.h - Circular buffer library for Arduino.
Copyright (c) 2017 Roberto Lo Giacco. All right reserved.
This library 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 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __CIRCULAR_BUFFER__
#define __CIRCULAR_BUFFER__
#include <inttypes.h>
#ifndef CIRCULAR_BUFFER_XS
#define __CB_ST__ uint16_t
#else
#define __CB_ST__ uint8_t
#endif
template<typename T, __CB_ST__ S> class CircularBuffer {
public:
CircularBuffer();
~CircularBuffer();
/**
* Adds an element to the beginning of buffer: the operation returns `false` if the addition caused overwriting an existing element.
*/
bool unshift(T value);
/**
* Adds an element to the end of buffer: the operation returns `false` if the addition caused overwriting an existing element.
*/
bool push(T value);
/**
* Removes an element from the beginning of the buffer.
*/
T shift();
/**
* Removes an element from the end of the buffer.
*/
T pop();
/**
* Returns the element at the beginning of the buffer.
*/
T inline first();
/**
* Returns the element at the end of the buffer.
*/
T inline last();
/**
* Array-like access to buffer
*/
T operator [] (__CB_ST__ index);
/**
* Returns how many elements are actually stored in the buffer.
*/
__CB_ST__ inline size();
/**
* Returns how many elements can be safely pushed into the buffer.
*/
__CB_ST__ inline available();
/**
* Returns `true` if no elements can be removed from the buffer.
*/
bool inline isEmpty();
/**
* Returns `true` if no elements can be added to the buffer without overwriting existing elements.
*/
bool inline isFull();
/**
* Resets the buffer to a clean status, dropping any reference to current elements
* and making all buffer positions available again.
*/
void inline clear();
private:
T buffer[S];
T *head;
T *tail;
uint16_t count;
};
#include <CircularBuffer.tpp>
#endif