forked from ptarabbia/picoevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicoevents.h
428 lines (385 loc) · 11 KB
/
picoevents.h
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright 2019 Patrice Tarabbia
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <list>
#include <functional>
#include <tuple>
#include <utility>
/*
A simple mechanism to notify events between various UI elements
Not thread-safe (but can be easily made so with mutexes protecting
the add / replace / remove / notify methods
A typical use case is, you have a button with a on/off state that
can be changed in different parts of the application, and you also
need other objects to react when the state of the button changes
so first you define an event:
picoevents::Event<const int> buttonStateChangedEvent;
you'll have somewhere, something that hold the current state with
get/set methods
bool getCurrentButtonState() const
{
return _buttonState;
}
void setCurrentButtonState(bool value) const
{
buttonStateChangedEvent.notify(value);
}
Note how setCurrentButtonState doesn't change the value, but instead
notifies the event. So you need to handle the event:
buttonStateChangedEvent.add([&](bool v) { this->_buttonState=v;});
Now any object in the UI can change the state of the button by calling:
buttonStateChangedEvent.notify(value);
and any object in the UI can be notifed of button change by adding:
buttonStateChangedEvent.add([&](bool v) { dosomething.... });
All of this, without these objects having any knowledge of each other !
You can also prepare a notification, then trigger it at a later time:
auto notifier = buttonStateChangedEvent.makeNotifier(false);
notifier.trigger(); // call buttonStateChangedEvent.notify(false);
That way you can prepare a notification on a thread, and trigger it on
a different one.
*/
namespace picoevents
{
class ScopedCallbackIDBase
{
public:
ScopedCallbackIDBase() = default;
virtual ~ScopedCallbackIDBase() {}
};
template<typename ...T>
class Event
{
public:
using Callback = std::function<void(T...)>;
using CallbackID = typename std::list<Callback>::iterator;
void setEnabled(bool b)
{
_enabled = b;
}
bool isEnabled() const
{
return _enabled;
}
CallbackID empty_callback()
{
return _callbacks.end();
}
CallbackID add(const Callback& c, bool first=false)
{
if (first)
{
_callbacks.push_front(c);
return _callbacks.begin();
}
else
{
_callbacks.push_back(c);
return std::prev(_callbacks.end());
}
}
void remove(CallbackID &c)
{
if (c != _callbacks.end())
{
if (c == _nextCallback)
{
_nextCallback++;
}
_callbacks.erase(c);
c = _callbacks.end();
}
}
bool replace(CallbackID id, Callback& c)
{
*id = c;
return true;
}
// overloading () so you can do event(a, b, c); instead of event.notify(a, b, c);
void operator()(T... t)
{
notify(t...);
}
void notify(T... t) const
{
if (_enabled)
{
for (CallbackID c = _callbacks.begin(); c != _callbacks.end();)
{
// as erase only invalidates the current iterator,
// we can perform erase() while calling notify by preloading
// the next one
_nextCallback = std::next(c);
(*c)(t...);
c = _nextCallback;
}
_nextCallback = _callbacks.end();
}
}
// RAII way to temporary disable an event
class ScopedDisable
{
Event& _e;
bool _prev;
public:
ScopedDisable(Event& e) : _e(e), _prev(_e.isEnabled())
{
_e.setEnabled(false);
}
~ScopedDisable()
{
_e.setEnabled(_prev);
}
};
class Notifier
{
// https://stackoverflow.com/questions/12742877/remove-reference-from-stdtuple-members
template <typename... TT>
using tuple_with_removed_refs = std::tuple<typename std::remove_reference<TT>::type...>;
template <typename... TT>
tuple_with_removed_refs<TT...> remove_ref_from_tuple_members(std::tuple<TT...> const& t) {
return tuple_with_removed_refs<TT...>{ t };
}
public:
Notifier(Event& e, T... t) :
_t(std::tuple<T...>(t...)),
_e(e) {}
void trigger()
{
std::apply([&](auto &&... args) { _e.notify(args...); }, _t);
}
private:
tuple_with_removed_refs<T...> _t;
Event& _e;
};
class ScopedNotifier
{
Notifier _n;
public:
ScopedNotifier(const Notifier& n):
_n(n)
{
}
~ScopedNotifier()
{
_n.trigger();
}
};
Notifier makeNotifier(T... t)
{
return Notifier(*this, t...);
}
class ScopedCallbackID: public ScopedCallbackIDBase
{
Event& _event;
Event::CallbackID _cb;
public:
ScopedCallbackID(Event& event, const Callback& c, bool first=false) : _event(event)
{
_cb = _event.add(c, first);
}
ScopedCallbackID(ScopedCallbackID&& other) : _event(other._event), _cb(other._cb)
{
other._cb = _event.empty_callback();
}
virtual ~ScopedCallbackID()
{
_event.remove(_cb);
}
void replace(Event& event, const Callback& c, bool first = false)
{
_event.remove(_cb);
_event = event;
_cb = _event.add(c, first);
}
void invoke(T... t)
{
(*_cb)(t...);
}
Event& getEvent()
{
return _event;
}
};
private:
// could possibly use std::forward_list<> but then removing
// callbacks becomes more tricky
mutable std::list<Callback> _callbacks;
bool _enabled = true;
mutable CallbackID _nextCallback = _callbacks.end();
};
//
// if you are using a lot of callbacks for the entire lifetime of an object, it can be
// tedious to store each callbackID in a ScopedCallbackID.
// Instead you can make your object inherit from ScopedCallbacksHolder, and use
// addCallback() to define events callbacks (for possibly a lot of different event types)
// that will automatically be removed when the object is deleted
//
class ScopedCallbacksHolder
{
std::vector< std::unique_ptr<ScopedCallbackIDBase> > _callbacks;
public:
ScopedCallbacksHolder() = default;
virtual ~ScopedCallbacksHolder()
{
}
template<typename E>
typename E::ScopedCallbackID* addCallback(E& e, const typename E::Callback& c, bool first = false)
{
typename E::ScopedCallbackID* res = new typename E::ScopedCallbackID(e, c, first);
_callbacks.emplace_back(res);
return res;
}
void removeAllCallbacks()
{
_callbacks.clear();
}
template<typename E>
void removeCallback(typename E::ScopedCallbackID * r)
{
auto it = _callbacks.begin();
while (it != _callbacks.end())
{
if (it->get() == r) break;
it++;
}
// because _callbacks contains unique_ptr, this actually deletes the original r
if (it != _callbacks.end()) _callbacks.erase(it);
}
};
// added listeners are removed when the value is deleted
// if a listener is deleted before the value, it needs to be removed with removeCallback() first !
template<typename T, typename ET=T>
class Value : public ScopedCallbacksHolder
{
T _t;
Event<ET> _e;
public:
Value(const T& t) : _t(t) {}
virtual ~Value()
{
removeAllCallbacks(); // remove callbacks before deleting _e
}
void set(const T& t)
{
_t = t;
notify();
}
void set(T&& t)
{
_t = t;
notify();
}
const T& get() const
{
return _t;
}
Event<ET>& getEvent()
{
return _e;
}
void notify()
{
_e.notify(_t);
}
typename Event<ET>::ScopedCallbackID* addListener(const typename Event<ET>::Callback& c, bool first = false)
{
return addCallback(_e, c, first);
}
};
}
// Example code below
#if 0
class Test
{
picoevents::Event<const int>& _event;
picoevents::Event<const int>::CallbackID _onEventTriggered;
public:
Test(picoevents::Event<const int>& event) : _event(event)
{
picoevents::Event<const int>::Callback callback = [&](const int i)
{
printf("Event Triggered %d\n", i);
};
_onEventTriggered = _event.add(callback);
}
~Test()
{
_event.remove(_onEventTriggered);
}
};
int main(int argc, char** argv)
{
{
// simple event with int argument
picoevents::Event<const int> event;
event.notify(1);
{
Test* t1 = new Test(event);
Test t2(event);
event.notify(2); // notify twice
delete t1;
event.notify(3); // notify once
}
event.notify(4); // dont notify
}
{
// event with int&, handler modifies argument
picoevents::Event<int&> event2;
event2.add(
[&](int& k)
{
k++;
}
);
int z = 5;
event2.notify(z);
printf("z = %d\n", z);
}
{
// event with multiple arguments
picoevents::Event<int, float, const std::string&> event3;
picoevents::Event<int, float, const std::string&>::Callback cb =
[&](int k, float v, const std::string& s)
{
printf("%d %f %s\n", k, v, s.c_str());
};
event3.add(cb);
event3.notify(3, 0.25f, "3 args event");
}
{
// creating a notifier, then triggering it. With delayed notifiers, you can't use references in the parameters
picoevents::Event<int, float, std::string> event4;
event4.add(
[&](int k, float v, const std::string s)
{
printf("%d %f %s\n", k, v, s.c_str());
});
picoevents::Event<int, float, std::string>::Notifier notifier(event4, 1, -0.5f, "Delayed notifier");
notifier.trigger();
}
}
#endif