InfiniTime.git

ref: fc5424cb72e477c5f1bbfaeddb5c50b851a965ae

src/utility/CircularBuffer.h


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#include <array>
#include <cstddef>

namespace Pinetime {
  namespace Utility {
    template <class T, size_t S>
    struct CircularBuffer {
      constexpr size_t Size() const {
        return S;
      }

      size_t Idx() const {
        return idx;
      }

      T& operator[](size_t n) {
        return data[(idx + n) % S];
      }

      const T& operator[](size_t n) const {
        return data[(idx + n) % S];
      }

      void operator++() {
        idx++;
        idx %= S;
      }

      void operator++(int) {
        operator++();
      }

      void operator--() {
        if (idx > 0) {
          idx--;
        } else {
          idx = S - 1;
        }
      }

      void operator--(int) {
        operator--();
      }

      std::array<T, S> data;
      size_t idx = 0;
    };
  }
}