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