LDA++
Events.hpp
1 #ifndef _LDAPLUSPLUS_EVENTS_EVENTS_HPP_
2 #define _LDAPLUSPLUS_EVENTS_EVENTS_HPP_
3 
4 
5 #include <list>
6 #include <memory>
7 #include <mutex>
8 #include <thread>
9 #include <unordered_set>
10 
11 namespace ldaplusplus {
12 namespace events {
13 
14 
18 class Event
19 {
20  public:
24  Event(std::string id);
25 
30  const std::string & id() const;
31 
32  private:
33  std::string id_;
34 };
35 
36 
41 {
42  public:
46  virtual void on_event(std::shared_ptr<Event> event) = 0;
47 
48  virtual ~EventListenerInterface(){};
49 };
50 
51 
56 {
57  public:
58  FunctionEventListener(std::function<void(std::shared_ptr<Event>)> listener);
59 
60  void on_event(std::shared_ptr<Event> event) override;
61 
62  private:
63  std::function<void(std::shared_ptr<Event>)> listener_;
64 };
65 
66 
71 {
72  public:
82  virtual void add_listener(std::shared_ptr<EventListenerInterface> listener) = 0;
83 
93  virtual void remove_listener(std::shared_ptr<EventListenerInterface> listener) = 0;
94 
105  virtual void dispatch(std::shared_ptr<Event> event) = 0;
106 
116  std::shared_ptr<EventListenerInterface> add_listener(
117  std::function<void(std::shared_ptr<Event>)> listener
118  ) {
119  auto l = std::make_shared<FunctionEventListener>(listener);
120 
121  add_listener(l);
122 
123  return l;
124  }
125 
136  template <class ListenerType, typename... Args>
137  std::shared_ptr<EventListenerInterface> add_listener(Args... args) {
138  auto l = std::make_shared<ListenerType>(args...);
139 
140  add_listener(l);
141 
142  return l;
143  }
144 
151  template <class EventType, typename... Args>
152  void dispatch(Args... args) {
153  auto event = std::make_shared<EventType>(args...);
154 
155  dispatch(event);
156  }
157 
158  virtual ~EventDispatcherInterface(){};
159 };
160 
161 
167 {
168  public:
169  void add_listener(std::shared_ptr<EventListenerInterface> listener) override;
170  void remove_listener(std::shared_ptr<EventListenerInterface> listener) override;
171  void dispatch(std::shared_ptr<Event> event) override;
172 
173  private:
174  std::list<std::shared_ptr<EventListenerInterface> > listeners_;
175 };
176 
177 
183 {
184  public:
186  event_dispatcher_(std::make_shared<EventDispatcher>())
187  {}
188 
189  std::shared_ptr<EventDispatcherInterface> get_event_dispatcher() {
190  return event_dispatcher_;
191  }
192 
193  void set_event_dispatcher(std::shared_ptr<EventDispatcherInterface> dispatcher) {
194  event_dispatcher_ = dispatcher;
195  }
196 
197  private:
198  std::shared_ptr<EventDispatcherInterface> event_dispatcher_;
199 };
200 
201 
207 {
208  public:
209  virtual void add_listener(std::shared_ptr<EventListenerInterface> listener) override;
210  virtual void remove_listener(std::shared_ptr<EventListenerInterface> listener) override;
211  virtual void dispatch(std::shared_ptr<Event> event) override;
212 
220  void process_events();
221 
222  private:
223  std::mutex listeners_mutex_;
224  std::list<std::shared_ptr<EventListenerInterface> > listeners_;
225 
226  std::mutex deleted_listeners_mutex_;
227  std::unordered_set<std::shared_ptr<EventListenerInterface> > deleted_listeners_;
228 
229  std::mutex events_mutex_;
230  std::list<std::shared_ptr<Event> > events_;
231 };
232 
233 
242 {
243  public:
245 
246  virtual void dispatch(std::shared_ptr<Event> event) override;
247 
248  private:
249  std::thread::id thread_id_;
250 };
251 
252 } // namespace events
253 } // namespace ldaplusplus
254 
255 #endif // _LDAPLUSPLUS_EVENTS_EVENTS_HPP_
Definition: Events.hpp:166
void dispatch(Args...args)
Definition: Events.hpp:152
const std::string & id() const
Definition: Events.cpp:11
std::shared_ptr< EventListenerInterface > add_listener(Args...args)
Definition: Events.hpp:137
Event(std::string id)
Definition: Events.cpp:8
Definition: Events.hpp:18
std::shared_ptr< EventListenerInterface > add_listener(std::function< void(std::shared_ptr< Event >)> listener)
Definition: Events.hpp:116
Definition: Document.hpp:11