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