12345678910111213141516171819202122232425262728293031323334353637 |
- #ifndef RINGBUFFER_H
- #define RINGBUFFER_H
- #include "utils/Array.h"
- template<typename T, int N>
- class RingBuffer final {
- Array<T, N> data;
- int writeIndex = 0;
- int readIndex = 0;
- int values = 0;
- public:
- bool write(const T& t) {
- if(values >= N) {
- return true;
- }
- data[writeIndex] = t;
- writeIndex = (writeIndex + 1) % N;
- values++;
- return false;
- }
- bool canRead() const {
- return values > 0;
- }
- T read() {
- values -= values > 0;
- T& t = data[readIndex];
- readIndex = (readIndex + 1) % N;
- return t;
- }
- };
- #endif
|