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