12345678910111213141516171819202122232425262728293031 |
- #ifndef RINGBUFFER_H
- #define RINGBUFFER_H
- #include "common/utils/Array.h"
- template<typename T, uint N>
- class RingBuffer {
- public:
- void write(const T& t) {
- data[writeIndex] = t;
- writeIndex = (writeIndex + 1) % N;
- }
- bool canRead() const {
- return writeIndex != readIndex;
- }
- T read() {
- T& t = data[readIndex];
- readIndex = (readIndex + 1) % N;
- return t;
- }
- private:
- Array<T, N> data;
- uint writeIndex = 0;
- uint readIndex = 0;
- };
- #endif
|