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