20template <
class T,
size_t N>
23 static_assert(N > 0,
"Circular buffer size cannot be zero.");
34 : m_buffer{buffer}, m_index{index} {}
62 : m_buffer{buffer}, m_index{index} {}
120 size_t size()
const {
return m_length; }
130 const T&
front()
const {
return (*
this)[0]; }
138 T&
back() {
return m_data[(m_front + m_length - 1) % N]; }
146 const T&
back()
const {
return m_data[(m_front + m_length - 1) % N]; }
154 m_front = ModuloDec(m_front);
156 m_data[m_front] = value;
169 m_data[(m_front + m_length) % N] = value;
175 m_front = ModuloInc(m_front);
185 template <
class... Args>
187 m_front = ModuloDec(m_front);
189 m_data[m_front] = T{args...};
202 template <
class... Args>
204 m_data[(m_front + m_length) % N] = T{args...};
210 m_front = ModuloInc(m_front);
221 T& temp = m_data[m_front];
222 m_front = ModuloInc(m_front);
235 return m_data[(m_front + m_length) % N];
249 T&
operator[](
size_t index) {
return m_data[(m_front + index) % N]; }
255 return m_data[(m_front + index) % N];
259 std::array<T, N> m_data;
272 size_t ModuloInc(
size_t index) {
return (index + 1) % N; }
279 size_t ModuloDec(
size_t index) {
Definition: static_circular_buffer.h:53
std::forward_iterator_tag iterator_category
Definition: static_circular_buffer.h:55
const_iterator(const static_circular_buffer *buffer, size_t index)
Definition: static_circular_buffer.h:61
bool operator==(const const_iterator &) const =default
const_iterator operator++(int)
Definition: static_circular_buffer.h:68
const_iterator & operator++()
Definition: static_circular_buffer.h:64
const_reference operator*() const
Definition: static_circular_buffer.h:74
const T & const_reference
Definition: static_circular_buffer.h:59
T * pointer
Definition: static_circular_buffer.h:58
T value_type
Definition: static_circular_buffer.h:56
std::ptrdiff_t difference_type
Definition: static_circular_buffer.h:57
Definition: static_circular_buffer.h:25
iterator operator++(int)
Definition: static_circular_buffer.h:40
T value_type
Definition: static_circular_buffer.h:28
reference operator*()
Definition: static_circular_buffer.h:46
std::ptrdiff_t difference_type
Definition: static_circular_buffer.h:29
bool operator==(const iterator &) const =default
T & reference
Definition: static_circular_buffer.h:31
iterator & operator++()
Definition: static_circular_buffer.h:36
iterator(static_circular_buffer *buffer, size_t index)
Definition: static_circular_buffer.h:33
T * pointer
Definition: static_circular_buffer.h:30
std::forward_iterator_tag iterator_category
Definition: static_circular_buffer.h:27
This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
Definition: static_circular_buffer.h:21
const T & back() const
Returns value at back of buffer.
Definition: static_circular_buffer.h:146
T & operator[](size_t index)
Definition: static_circular_buffer.h:249
iterator end()
Returns end iterator.
Definition: static_circular_buffer.h:89
void emplace_back(Args &&... args)
Push a new value onto the back of the buffer that is constructed with the provided constructor argume...
Definition: static_circular_buffer.h:203
void push_front(T value)
Push a new value onto the front of the buffer.
Definition: static_circular_buffer.h:153
void reset()
Empties internal buffer.
Definition: static_circular_buffer.h:241
T & back()
Returns value at back of buffer.
Definition: static_circular_buffer.h:138
T pop_back()
Pop value at back of buffer.
Definition: static_circular_buffer.h:233
T pop_front()
Pop value at front of buffer.
Definition: static_circular_buffer.h:220
void push_back(T value)
Push a new value onto the back of the buffer.
Definition: static_circular_buffer.h:168
const_iterator end() const
Returns end iterator.
Definition: static_circular_buffer.h:101
const T & operator[](size_t index) const
Definition: static_circular_buffer.h:254
const_iterator begin() const
Returns begin iterator.
Definition: static_circular_buffer.h:96
const_iterator cbegin() const
Returns begin iterator.
Definition: static_circular_buffer.h:108
const T & front() const
Returns value at front of buffer.
Definition: static_circular_buffer.h:130
void emplace_front(Args &&... args)
Push a new value onto the front of the buffer that is constructed with the provided constructor argum...
Definition: static_circular_buffer.h:186
iterator begin()
Returns begin iterator.
Definition: static_circular_buffer.h:84
const_iterator cend() const
Returns end iterator.
Definition: static_circular_buffer.h:113
size_t size() const
Returns number of elements in buffer.
Definition: static_circular_buffer.h:120
T & front()
Returns value at front of buffer.
Definition: static_circular_buffer.h:125
Definition: ntcore_cpp.h:26