ScummVM API documentation
ringbuffer.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include "common/util.h"
8 #include <stddef.h>
9 
10 namespace TwinE {
11 
16 template<typename TYPE, size_t SIZE = 64u>
17 class RingBuffer {
18 protected:
19  size_t _size;
20  size_t _front;
21  size_t _back;
22  TYPE _buffer[SIZE];
23 
24 public:
25  using value_type = TYPE;
26 
27  RingBuffer() : _size(0u), _front(0u), _back(SIZE - 1u) {
28  }
29 
30  class iterator {
31  private:
32  RingBuffer *_ringBuffer;
33  size_t _idx;
34 
35  public:
36  iterator(RingBuffer *ringBuffer, size_t idx) : _ringBuffer(ringBuffer), _idx(idx) {
37  }
38 
39  inline const TYPE &operator*() const {
40  return _ringBuffer->_buffer[_idx % _ringBuffer->capacity()];
41  }
42 
43  inline TYPE &operator*() {
44  return _ringBuffer->_buffer[_idx % _ringBuffer->capacity()];
45  }
46 
47  inline const TYPE &operator()() const {
48  return _ringBuffer->_buffer[_idx % _ringBuffer->capacity()];
49  }
50 
51  inline TYPE &operator()() {
52  return _ringBuffer->_buffer[_idx % _ringBuffer->capacity()];
53  }
54 
55  iterator &operator++() {
56  ++_idx;
57  return *this;
58  }
59 
60  inline bool operator!=(const iterator &rhs) const {
61  return _ringBuffer != rhs._ringBuffer || _idx != rhs._idx;
62  }
63 
64  inline bool operator==(const iterator &rhs) const {
65  return _ringBuffer == rhs._ringBuffer && _idx == rhs._idx;
66  }
67 
68  inline const TYPE *operator->() const {
69  return &_ringBuffer->_buffer[_idx % _ringBuffer->capacity()];
70  }
71  };
72 
73  iterator begin() {
74  return iterator(this, _front);
75  }
76 
77  iterator end() {
78  return iterator(this, _front + _size);
79  }
80 
81  iterator begin() const {
82  return iterator(const_cast<RingBuffer *>(this), _front);
83  }
84 
85  iterator end() const {
86  return iterator(const_cast<RingBuffer *>(this), _front + _size);
87  }
88 
89  inline size_t size() const {
90  return _size;
91  }
92 
93  constexpr size_t capacity() const {
94  return SIZE;
95  }
96 
97  inline bool empty() const {
98  return _size == 0;
99  }
100 
105  const TYPE &front() const {
106  return _buffer[_front];
107  }
108 
113  TYPE &front() {
114  return _buffer[_front];
115  }
116 
121  const TYPE &back() const {
122  return _buffer[_back];
123  }
124 
129  TYPE &back() {
130  return _buffer[_back];
131  }
132 
137  void clear() {
138  _front = 0u;
139  _back = SIZE - 1u;
140  _size = 0u;
141  }
142 
146  void push_back(const TYPE &x) {
147  _back = (_back + 1u) % SIZE;
148  if (_size == SIZE) {
149  _front = (_front + 1u) % SIZE;
150  } else {
151  ++_size;
152  }
153  _buffer[_back] = x;
154  }
155 
160  template<typename... _Args>
161  void emplace_back(_Args &&...args) {
162  _back = (_back + 1u) % SIZE;
163  if (_size == SIZE) {
164  _front = (_front + 1u) % SIZE;
165  } else {
166  ++_size;
167  }
168  _buffer[_back] = Common::move(TYPE{Common::forward<_Args>(args)...});
169  }
170 
174  void pop() {
175  if (_size == 0) {
176  return;
177  }
178  --_size;
179  _front = (_front + 1u) % SIZE;
180  }
181 
185  void erase_back(const size_t n) {
186  if (n >= _size) {
187  clear();
188  return;
189  }
190  _size -= n;
191  _back = (_front + _size - 1u) % SIZE;
192  }
193 
197  void erase_front(const size_t n) {
198  if (n >= _size) {
199  clear();
200  return;
201  }
202  _front = (_front + n) % SIZE;
203  _size -= n;
204  }
205 
206  inline TYPE &operator[](size_t i) {
207  return _buffer[(_front + i) % SIZE];
208  }
209 
210  inline const TYPE &operator[](size_t i) const {
211  return _buffer[(_front + i) % SIZE];
212  }
213 };
214 
215 } // namespace TwinE
void pop()
Removes element from the beginning of the buffer.
Definition: ringbuffer.h:174
void emplace_back(_Args &&...args)
Pushes an elements to the end of the buffer.
Definition: ringbuffer.h:161
Non allocating buffer class holds a maximum of given entries and allows an endless insert by overrind...
Definition: ringbuffer.h:17
Definition: ringbuffer.h:30
void erase_back(const size_t n)
Erase the given amount of elements from the end of the buffer.
Definition: ringbuffer.h:185
void clear()
Clears the whole ring buffer.
Definition: ringbuffer.h:137
TYPE & front()
Access to the first element in the buffer.
Definition: ringbuffer.h:113
void push_back(const TYPE &x)
Pushes an element to the end of the buffer.
Definition: ringbuffer.h:146
void erase_front(const size_t n)
Erase the given amount of elements from the beginning of the buffer.
Definition: ringbuffer.h:197
const TYPE & back() const
Access to the last element in the buffer.
Definition: ringbuffer.h:121
Definition: achievements_tables.h:27
const TYPE & front() const
Access to the first element in the buffer.
Definition: ringbuffer.h:105
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
TYPE & back()
Access to the last element in the buffer.
Definition: ringbuffer.h:129