-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.h
More file actions
345 lines (276 loc) · 9.18 KB
/
callback.h
File metadata and controls
345 lines (276 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef CPPTOOLS_CALLBACK_H
#define CPPTOOLS_CALLBACK_H
#include <functional>
#include <vector>
#include <chrono>
#include <atomic>
#include <mutex>
template<typename T>
class Wrapper {
public:
using Getter = std::function<T(const T& value)>;
using Setter = std::function<T(const T& value, const T& new_value)>;
using SimpleSetter = std::function<T(const T& new_value)>;
Wrapper(
const T& value,
const Getter& getter,
const Setter& setter = [](const T& value __attribute__((unused)), const T& new_value) {return new_value;}
): _inner(value), _getter(getter), _setter(setter) {}
Wrapper(
const T& value,
const Getter& getter,
const SimpleSetter& setter
): Wrapper(value, getter,
[&setter](const T& value __attribute__((unused)), const T& new_value){
return setter(new_value);
}
) {}
T get() const {
return _getter(_inner);
}
Wrapper& set(const T& value) {
_inner = _setter(_inner, value);
return *this;
}
Wrapper& operator=(const T& value) {
return set(value);
}
operator T() {
return get();
}
private:
T _inner;
Getter _getter;
Setter _setter;
};
namespace {
std::mutex THREADED_WRAPPER_GLOBAL_MUTEX;
}
template<typename T>
class ThreadedWrapper: Wrapper<T> {
using super = Wrapper<T>;
public:
using Getter = typename super::Getter;
using Setter = typename super::Setter;
using SimpleSetter = typename super::SimpleSetter;
ThreadedWrapper(
std::mutex& wmutex,
const T& value,
const Getter& getter,
const Setter& setter = [](const T& value __attribute__((unused)), const T& new_value) {return new_value;}
): super(value, getter, setter), _mutex(wmutex) {}
ThreadedWrapper(
std::mutex& wmutex,
const T& value,
const Getter& getter,
const SimpleSetter& setter
): super(value, getter, setter), _mutex(wmutex) {}
ThreadedWrapper(
const T& value,
const Getter& getter,
const Setter& setter = [](const T& value __attribute__((unused)), const T& new_value) {return new_value;}
): ThreadedWrapper(THREADED_WRAPPER_GLOBAL_MUTEX, value, getter, setter) {}
ThreadedWrapper(
const T& value,
const Getter& getter,
const SimpleSetter& setter
): ThreadedWrapper(THREADED_WRAPPER_GLOBAL_MUTEX, value, getter, setter) {}
T get() const {
std::lock_guard<std::mutex> guard(_mutex);
return this->get();
}
ThreadedWrapper& set(const T& value) {
// FIXME: one thread trying multiple access block itself
// https://en.cppreference.com/w/cpp/thread/recursive_mutex
// Use try_lock?
std::lock_guard<std::mutex> guard(_mutex);
return this->set(value);
}
ThreadedWrapper& operator=(const T& value) {
return set(value);
}
operator T() {
return get();
}
private:
std::mutex& _mutex;
};
template<typename T>
class AtomicWrapper: Wrapper<std::atomic<T>> {};
template<typename T>
class Subject: Wrapper<T> {
using super = Wrapper<T>;
public:
using Getter = typename super::Getter;
using Setter = typename super::Setter;
using Callback = std::function<void(const T& value)>;
using CallbackList = std::vector<Callback>;
Subject(
const T& value,
const Getter& getter,
const Setter& setter,
const CallbackList& subscriptions = {}
): super(value, getter, setter), _subscriptions(subscriptions) {}
Subject(
const T& value,
const Getter& getter,
const CallbackList& subscriptions = {}
): super(value, getter), _subscriptions(subscriptions) {}
Subject(
const T& value,
const CallbackList& subscriptions = {}
): super(value, [](const T& value){ return value; }), _subscriptions(subscriptions) {}
void subscribe(const Callback& callback) {
_subscriptions.push_back(callback);
}
void forEach(const T& value) {
for(const auto& callback: _subscriptions) {
callback(value);
}
}
void propagate() {
forEach(this->get());
}
void set(const T& value) {
super::set(value);
propagate();
}
private:
CallbackList _subscriptions;
};
template<typename Duration>
class BInterval {
using Clock = std::chrono::high_resolution_clock;
using Unit = std::chrono::milliseconds;
public:
using type = decltype(Clock::now());
using TimedCallBack = std::function<void(Duration)>;
using CallBack = std::function<void(void)>;
BInterval(Duration interval): _interval(interval) {}
static BInterval Seconds(int seconds) {
return BInterval(seconds * 1000);
}
static BInterval Minutes(int minutes) {
return Seconds(minutes * 60);
}
static BInterval FPS(int fps) {
return BInterval(1000 / fps);
}
void runWhile(const TimedCallBack& callback, const bool& sentinel) {
type last(now());
Duration elapsed = 0;
while(sentinel) {
type current = now();
Duration dt = delta(last, current);
if(dt) {
elapsed += dt;
last = current;
}
while(elapsed > _interval) {
elapsed -= _interval;
callback(_interval);
}
}
}
void runWhile(const CallBack& callback, const bool& sentinel) {
runWhile(toTimed(callback), sentinel);
}
void runFor(const TimedCallBack& callback, Duration duration) {
bool sentinelle = true;
runWhile(
[&duration, &sentinelle, &callback](Duration delta) {
callback(delta);
sentinelle = (duration -= delta) > 0;
},
sentinelle
);
}
void runFor(const CallBack& callback, Duration duration) {
runFor(toTimed(callback), duration);
}
void run(const TimedCallBack& callback) {
bool sentinelle = true;
runWhile(callback, sentinelle);
}
void run(const CallBack& callback) {
run(toTimed(callback));
}
private:
static TimedCallBack toTimed(const CallBack& callback) {
return [&callback](Duration duration __attribute__((unused))) {
callback();
};
}
static type now() {
return Clock::now();
}
static Duration delta(type first, type second) {
return (Duration)std::chrono::duration_cast<Unit>(second - first).count();
}
Duration _interval;
};
using Interval = BInterval<double>;
template<typename Duration>
class BDebounce {
using Clock = std::chrono::high_resolution_clock;
using Unit = std::chrono::milliseconds;
public:
using type = decltype(Clock::now());
BDebounce(Duration interval): _interval(interval) {
reset();
}
static BDebounce Seconds(int seconds) {
return BDebounce(seconds * 1000);
}
static BDebounce Minutes(int minutes) {
return Seconds(minutes * 60);
}
void reset() {
_last = now() - Unit((int)_interval);
}
bool check() {
type current = now();
Duration dt = delta(_last, current);
if(dt < _interval) {
return false;
}
_last = current;
return true;
}
operator bool() {
return check();
}
private:
static type now() {
return Clock::now();
}
static Duration delta(type first, type second) {
return (Duration)std::chrono::duration_cast<Unit>(second - first).count();
}
Duration _interval;
type _last;
};
using Debounce = BDebounce<double>;
template<typename T, typename N=T>
class Aggregate {
public:
using Func = std::function<T(const T& aggr, const N& value)>;
Aggregate(const T& initial, const Func& f): _value(initial), _func(f) {}
Aggregate(const Func& f): Aggregate(T(), f) {}
const T& get() const {
return _value;
}
const T& next(const N& value) {
return _value = _func(get(), value);
}
const T& operator()() {
return get();
}
const T& operator()(const N& value) {
return next(value);
}
private:
T _value;
Func _func;
};
#endif // CPPTOOLS_CALLBACK_H