Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
round_robin.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <iterator>
8
15 template <class V, class T>
16 class it_wrap_t {
17 public:
18 using iterator_category = std::random_access_iterator_tag;
19 using value_type = V;
20 using difference_type = V;
21 using pointer = V *;
22 using const_pointer = V const *;
23 using reference = V &;
24 using const_reference = V const &;
25
26 typedef T iterator;
27 typedef std::ptrdiff_t diff_t;
28
29 iterator
30 operator+=(diff_t step) {
31 while (step-- > 0) {
32 ++_this();
33 }
34
35 return _this();
36 }
37
38 iterator
39 operator-=(diff_t step) {
40 while (step-- > 0) {
41 --_this();
42 }
43
44 return _this();
45 }
46
47 iterator
48 operator+(diff_t step) {
49 iterator new_ = _this();
50
51 return new_ += step;
52 }
53
54 iterator
55 operator-(diff_t step) {
56 iterator new_ = _this();
57
58 return new_ -= step;
59 }
60
61 diff_t
62 operator-(iterator first) {
63 diff_t step = 0;
64 while (first != _this()) {
65 ++step;
66 ++first;
67 }
68
69 return step;
70 }
71
72 iterator
73 operator++() {
74 _this().inc();
75 return _this();
76 }
77 iterator
78 operator--() {
79 _this().dec();
80 return _this();
81 }
82
83 iterator
84 operator++(int) {
85 iterator new_ = _this();
86
87 ++_this();
88
89 return new_;
90 }
91
92 iterator
93 operator--(int) {
94 iterator new_ = _this();
95
96 --_this();
97
98 return new_;
99 }
100
101 reference
102 operator*() { return *_this().get(); }
103 const_reference
104 operator*() const { return *_this().get(); }
105
106 pointer
107 operator->() { return &*_this(); }
108 const_pointer
109 operator->() const { return &*_this(); }
110
111 bool
112 operator!=(const iterator &other) const {
113 return !(_this() == other);
114 }
115
116 bool
117 operator<(const iterator &other) const {
118 return !(_this() >= other);
119 }
120
121 bool
122 operator>=(const iterator &other) const {
123 return _this() == other || _this() > other;
124 }
125
126 bool
127 operator<=(const iterator &other) const {
128 return _this() == other || _this() < other;
129 }
130
131 bool
132 operator==(const iterator &other) const { return _this().eq(other); };
133 bool
134 operator>(const iterator &other) const { return _this().gt(other); }
135
136 private:
137 iterator &
138 _this() { return *static_cast<iterator *>(this); }
139 const iterator &
140 _this() const { return *static_cast<const iterator *>(this); }
141 };
142
143 template <class V, class It>
144 class round_robin_t: public it_wrap_t<V, round_robin_t<V, It>> {
145 public:
146 using iterator = It;
147 using pointer = V *;
148
149 round_robin_t(iterator begin, iterator end):
150 _begin(begin), _end(end), _pos(begin) {}
151
152 void
153 inc() {
154 ++_pos;
155
156 if (_pos == _end) {
157 _pos = _begin;
158 }
159 }
160
161 void
162 dec() {
163 if (_pos == _begin) {
164 _pos = _end;
165 }
166
167 --_pos;
168 }
169
170 bool
171 eq(const round_robin_t &other) const {
172 return *_pos == *other._pos;
173 }
174
175 pointer
176 get() const {
177 return &*_pos;
178 }
179
180 private:
181 It _begin;
182 It _end;
183
184 It _pos;
185 };
186
187 template <class V, class It>
189 make_round_robin(It begin, It end) {
190 return round_robin_t<V, It>(begin, end);
191 }
192} // namespace round_robin_util
Definition round_robin.h:16
Definition round_robin.h:144
A round-robin iterator utility.
Definition round_robin.h:14