Skip to content

Commit 127682a

Browse files
committed
Providing templated circular buffer with size configurable at run-time (heavily borrowed from api::RingBufferN
1 parent 0679ae8 commit 127682a

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

src/threading/CircularBuffer.hpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* This file is part of the Arduino_ThreadsafeIO library.
3+
* Copyright (c) 2021 Arduino SA.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef ARDUINO_THREADS_RINGBUFFER_HPP_
20+
#define ARDUINO_THREADS_RINGBUFFER_HPP_
21+
22+
/**************************************************************************************
23+
* INCLUDE
24+
**************************************************************************************/
25+
26+
#include <SharedPtr.h>
27+
28+
/**************************************************************************************
29+
* CLASS DECLARATION
30+
**************************************************************************************/
31+
32+
template <typename T>
33+
class CircularBuffer
34+
{
35+
public:
36+
37+
CircularBuffer(size_t const size);
38+
39+
void store(T const data);
40+
T read();
41+
bool isFull() const;
42+
bool isEmpty() const;
43+
44+
45+
private:
46+
47+
mbed::SharedPtr<T> _data;
48+
size_t const _size;
49+
size_t _head, _tail, _num_elems;
50+
51+
size_t next(size_t const idx);
52+
};
53+
54+
/**************************************************************************************
55+
* CTOR/DTOR
56+
**************************************************************************************/
57+
58+
template <typename T>
59+
CircularBuffer<T>::CircularBuffer(size_t const size)
60+
: _data{new T[size]}
61+
, _size{size}
62+
, _head{0}
63+
, _tail{0}
64+
, _num_elems{0}
65+
{
66+
}
67+
68+
/**************************************************************************************
69+
* PUBLIC MEMBER FUNCTIONS
70+
**************************************************************************************/
71+
72+
template <typename T>
73+
void CircularBuffer<T>::store(T const data)
74+
{
75+
if (!isFull())
76+
{
77+
_data.get()[_head] = data;
78+
_head = next(_head);
79+
_num_elems++;
80+
}
81+
}
82+
83+
template <typename T>
84+
T CircularBuffer<T>::read()
85+
{
86+
if (isEmpty())
87+
return T{0};
88+
89+
T const value = _data.get()[_tail];
90+
_tail = next(_tail);
91+
_num_elems--;
92+
93+
return value;
94+
}
95+
96+
template <typename T>
97+
bool CircularBuffer<T>::isFull() const
98+
{
99+
return (_num_elems == _size);
100+
}
101+
102+
template <typename T>
103+
bool CircularBuffer<T>::isEmpty() const
104+
{
105+
return (_num_elems == 0);
106+
}
107+
108+
/**************************************************************************************
109+
* PRIVATE MEMBER FUNCTIONS
110+
**************************************************************************************/
111+
112+
template <typename T>
113+
size_t CircularBuffer<T>::next(size_t const idx)
114+
{
115+
return ((idx + 1) % _size);
116+
}
117+
118+
#endif /* ARDUINO_THREADS_RINGBUFFER_HPP_ */

0 commit comments

Comments
 (0)