uatools 1.0.0
 
Loading...
Searching...
No Matches
dispatcher.hpp
Go to the documentation of this file.
1
6
7#ifndef ESO_UATOOLS_PROTOCOL_BASE_DISPATCHER_HPP
8#define ESO_UATOOLS_PROTOCOL_BASE_DISPATCHER_HPP
9
10#include <functional>
11#include <list>
12
14
15 template <typename... Args>
16 class Dispatcher {
17 public:
18
19 using CbType = std::function<void(Args...)>;
20
21 class CbId {
22
23 public:
24 CbId() : valid(false) {}
25
26 private:
27
28 friend class Dispatcher<Args...>;
29
30 CbId(typename std::list<CbType>::iterator i)
31
32 : iter(i), valid(true)
33
34 {}
35
36 typename std::list<CbType>::iterator iter;
37 bool valid;
38 };
39
40 // register to be notified
42 if (cb)
43 {
44 cbs.push_back(cb);
45 return CbId(--cbs.end());
46 }
47
48 return CbId();
49 }
50
51 // unregister to be notified
52 void delCB(CbId &id) {
53 if (id.valid)
54 {
55 cbs.erase(id.iter);
56 }
57 }
58
59 void broadcast(Args... args) {
60 for (auto &cb : cbs)
61 {
62 cb(args...);
63 }
64 }
65
66 private:
67 std::list<CbType> cbs;
68 };
69
70 }
71
72#endif
Definition dispatcher.hpp:16
std::function< void(Args...)> CbType
Definition dispatcher.hpp:19
void broadcast(Args... args)
Definition dispatcher.hpp:59
CbId addCB(CbType cb)
Definition dispatcher.hpp:41
void delCB(CbId &id)
Definition dispatcher.hpp:52
Definition icomm_adapter_generic_v2.hpp:49