Sunshine master
Self-hosted game stream host for Moonlight.
round_robin.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <iterator>
9
19 template<class V, class T>
20 class it_wrap_t {
21 public:
25 using iterator_category = std::random_access_iterator_tag;
29 using value_type = V;
33 using difference_type = V;
37 using pointer = V *;
41 using const_pointer = V const *;
45 using reference = V &;
49 using const_reference = V const &;
50
54 typedef T iterator;
58 typedef std::ptrdiff_t diff_t;
59
67 while (step-- > 0) {
68 ++_this();
69 }
70
71 return _this();
72 }
73
81 while (step-- > 0) {
82 --_this();
83 }
84
85 return _this();
86 }
87
95 iterator new_ = _this();
96
97 return new_ += step;
98 }
99
107 iterator new_ = _this();
108
109 return new_ -= step;
110 }
111
119 diff_t step = 0;
120 while (first != _this()) {
121 ++step;
122 ++first;
123 }
124
125 return step;
126 }
127
134 _this().inc();
135 return _this();
136 }
137
144 _this().dec();
145 return _this();
146 }
147
154 iterator new_ = _this();
155
156 ++_this();
157
158 return new_;
159 }
160
167 iterator new_ = _this();
168
169 --_this();
170
171 return new_;
172 }
173
180 return *_this().get();
181 }
182
189 return *_this().get();
190 }
191
198 return &*_this();
199 }
200
207 return &*_this();
208 }
209
216 bool operator!=(const iterator &other) const {
217 return !(_this() == other);
218 }
219
226 bool operator<(const iterator &other) const {
227 return !(_this() >= other);
228 }
229
236 bool operator>=(const iterator &other) const {
237 return _this() == other || _this() > other;
238 }
239
246 bool operator<=(const iterator &other) const {
247 return _this() == other || _this() < other;
248 }
249
256 bool operator==(const iterator &other) const {
257 return _this().eq(other);
258 };
259
266 bool operator>(const iterator &other) const {
267 return _this().gt(other);
268 }
269
270 private:
271 iterator &_this() {
272 return *static_cast<iterator *>(this);
273 }
274
275 const iterator &_this() const {
276 return *static_cast<const iterator *>(this);
277 }
278 };
279
283 template<class V, class It>
284 class round_robin_t: public it_wrap_t<V, round_robin_t<V, It>> {
285 public:
289 using iterator = It;
293 using pointer = V *;
294
302 _begin(begin),
303 _end(end),
304 _pos(begin) {
305 }
306
310 void inc() {
311 ++_pos;
312
313 if (_pos == _end) {
314 _pos = _begin;
315 }
316 }
317
321 void dec() {
322 if (_pos == _begin) {
323 _pos = _end;
324 }
325
326 --_pos;
327 }
328
335 bool eq(const round_robin_t &other) const {
336 return *_pos == *other._pos;
337 }
338
344 pointer get() const {
345 return &*_pos;
346 }
347
348 private:
349 It _begin;
350 It _end;
351
352 It _pos;
353 };
354
362 template<class V, class It>
364 return round_robin_t<V, It>(begin, end);
365 }
366} // namespace round_robin_util
CRTP base that provides iterator operators for round-robin iterators.
Definition round_robin.h:20
iterator operator+(diff_t step)
Return a copy advanced by a wrapped offset.
Definition round_robin.h:94
bool operator==(const iterator &other) const
Compare whether two wrapped iterator positions are equal.
Definition round_robin.h:256
bool operator>=(const iterator &other) const
Compare whether this wrapped position sorts at or after another one.
Definition round_robin.h:236
bool operator<=(const iterator &other) const
Compare whether this wrapped position sorts at or before another one.
Definition round_robin.h:246
diff_t operator-(iterator first)
Count wrapped increments needed to reach this iterator from another one.
Definition round_robin.h:118
bool operator<(const iterator &other) const
Compare whether this wrapped position sorts before another one.
Definition round_robin.h:226
iterator operator++(int)
Advance to the next element and return the previous iterator position.
Definition round_robin.h:153
bool operator!=(const iterator &other) const
Compare whether two wrapped iterator positions differ.
Definition round_robin.h:216
V difference_type
Difference type used for iterator movement.
Definition round_robin.h:33
reference operator*()
Dereference the current element in the cycled range.
Definition round_robin.h:179
V value_type
Value type exposed by the wrapped iterator.
Definition round_robin.h:29
T iterator
Concrete iterator type supplied by the CRTP child.
Definition round_robin.h:54
pointer operator->()
Access the current element in the cycled range.
Definition round_robin.h:197
V const * const_pointer
Const pointer to a value in the cycled range.
Definition round_robin.h:41
V & reference
Mutable reference to a value in the cycled range.
Definition round_robin.h:45
iterator operator+=(diff_t step)
Advance this iterator by repeatedly wrapping at the end of the range.
Definition round_robin.h:66
iterator operator--()
Move to the previous element, wrapping to the end when needed.
Definition round_robin.h:143
iterator operator-=(diff_t step)
Move this iterator backward by repeatedly wrapping at the beginning of the range.
Definition round_robin.h:80
const_reference operator*() const
Dereference the current element in the cycled range.
Definition round_robin.h:188
iterator operator++()
Advance to the next element, wrapping back to the beginning when needed.
Definition round_robin.h:133
bool operator>(const iterator &other) const
Compare whether this wrapped position sorts after another one.
Definition round_robin.h:266
const_pointer operator->() const
Access the current element in the cycled range.
Definition round_robin.h:206
std::random_access_iterator_tag iterator_category
Iterator category advertised to standard algorithms.
Definition round_robin.h:25
iterator operator-(diff_t step)
Return a copy moved backward by a wrapped offset.
Definition round_robin.h:106
V const & const_reference
Const reference to a value in the cycled range.
Definition round_robin.h:49
V * pointer
Mutable pointer to a value in the cycled range.
Definition round_robin.h:37
std::ptrdiff_t diff_t
Signed offset type used when moving through the range.
Definition round_robin.h:58
iterator operator--(int)
Move to the previous element and return the previous iterator position.
Definition round_robin.h:166
Iterator that cycles indefinitely over a fixed begin/end range.
Definition round_robin.h:284
bool eq(const round_robin_t &other) const
Compare two iterators for equality.
Definition round_robin.h:335
void dec()
Move the iterator to the previous element.
Definition round_robin.h:321
V * pointer
Mutable pointer to values in the cycled range.
Definition round_robin.h:293
It iterator
Underlying iterator type for the cycled range.
Definition round_robin.h:289
round_robin_t(iterator begin, iterator end)
Construct a round-robin iterator over a fixed range.
Definition round_robin.h:301
pointer get() const
Return the currently wrapped value or handle.
Definition round_robin.h:344
void inc()
Advance the iterator to the next element.
Definition round_robin.h:310
A round-robin iterator utility.
Definition round_robin.h:15
round_robin_t< V, It > make_round_robin(It begin, It end)
Create a round-robin iterator over a fixed range.
Definition round_robin.h:363