123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #ifndef RINGBUFFER_H
- #define RINGBUFFER_H
- #include "data/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;
- }
- void clear() {
- writeIndex = 0;
- readIndex = 0;
- values = 0;
- }
- };
- #endif
|