RingBuffer.h 525 B

12345678910111213141516171819202122232425262728293031
  1. #ifndef RINGBUFFER_H
  2. #define RINGBUFFER_H
  3. #include "common/utils/Array.h"
  4. template<typename T, uint N>
  5. class RingBuffer final {
  6. public:
  7. void write(const T& t) {
  8. data[writeIndex] = t;
  9. writeIndex = (writeIndex + 1) % N;
  10. }
  11. bool canRead() const {
  12. return writeIndex != readIndex;
  13. }
  14. T read() {
  15. T& t = data[readIndex];
  16. readIndex = (readIndex + 1) % N;
  17. return t;
  18. }
  19. private:
  20. Array<T, N> data;
  21. uint writeIndex = 0;
  22. uint readIndex = 0;
  23. };
  24. #endif