libfilezilla
event_handler.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_EVENT_HANDLER
2 #define LIBFILEZILLA_EVENT_HANDLER
3 
4 #include "event_loop.hpp"
5 
9 namespace fz {
10 
56 {
57  child_event_handler
58 };
59 
60 class FZ_PUBLIC_SYMBOL event_handler
61 {
62 public:
63  event_handler() = delete;
64 
65  explicit event_handler(event_loop& loop);
66  virtual ~event_handler();
67 
68  event_handler(event_handler const& h);
70 
71  event_handler& operator=(event_handler const&) = delete;
72 
79  void remove_handler();
80 
87  virtual void operator()(event_base const&) = 0;
88 
95  template<typename T, typename... Args>
96  void send_event(Args&&... args) {
97  event_loop_.send_event(this, new T(std::forward<Args>(args)...), true);
98  }
99 
100  template<typename T>
101  void send_event(T* evt) {
102  event_loop_.send_event(this, evt, true);
103  }
104 
105 
107  template<typename T>
108  void send_persistent_event(T* evt) {
109  event_loop_.send_event(this, evt, false);
110  }
111 
129  timer_id add_timer(monotonic_clock const &deadline, duration const& interval = {});
130 
147  timer_id add_timer(duration const& interval, bool one_shot);
148 
153  void stop_timer(timer_id id);
154 
162  timer_id stop_add_timer(timer_id id, monotonic_clock const& deadline, duration const& interval = {});
163 
171  timer_id stop_add_timer(timer_id id, duration const& interval, bool one_shot);
172 
173  void filter_events(std::function<bool(event_base& ev)> const& filter) {
174  event_loop_.filter_events([&](event_handler*& h, event_base& ev) {
175  if (h != this) {
176  return false;
177  }
178  return filter(ev);
179  });
180  }
181 
182  void resend_current_event() {
183  event_loop_.resend_current_event();
184  }
185 
186  void remove_events(event_source const* const source);
187 
188  event_loop & event_loop_;
189 private:
190  friend class event_loop;
191  bool removing_{};
192 
193  void remove_from_parent();
194  event_handler* parent_{};
195  event_handler* next_{};
196  event_handler* child_{};
197 };
198 
213 template<typename T, typename F>
214 bool dispatch(event_base const& ev, F&& f)
215 {
216  bool const same = same_type<T>(ev);
217  if (same) {
218  T const* e = static_cast<T const*>(&ev);
219  std::apply(std::forward<F>(f), e->v_);
220  }
221  return same;
222 }
223 
239 template<typename T, typename H, typename F>
240 bool dispatch(event_base const& ev, H* h, F&& f)
241 {
242  bool const same = same_type<T>(ev);
243  if (same) {
244  T const* e = static_cast<T const*>(&ev);
245  apply(h, std::forward<F>(f), e->v_);
246  }
247  return same;
248 }
249 
268 template<typename T, typename ... Ts, typename H, typename F, typename ... Fs>
269 bool dispatch(event_base const& ev, H* h, F&& f, Fs&& ... fs)
270 {
271  if (dispatch<T>(ev, h, std::forward<F>(f))) {
272  return true;
273  }
274 
275  return dispatch<Ts...>(ev, h, std::forward<Fs>(fs)...);
276 }
277 
278 }
279 
280 #endif
auto apply(Obj &&obj, F &&f, Tuple &&args) -> decltype(apply_(std::forward< Obj >(obj), std::forward< F >(f), std::forward< Tuple >(args), Seq()))
Apply tuple to pointer to member.
Definition: apply.hpp:48
Definition: event_handler.hpp:60
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition: event_handler.hpp:214
event_handler_option
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
void send_persistent_event(T *evt)
Be careful with lifetime.
Definition: event_handler.hpp:108
A simple threaded event loop for the typesafe event system.
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:33
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:402
The namespace used by libfilezilla.
Definition: apply.hpp:17
The duration class represents a time interval in milliseconds.
Definition: time.hpp:290
Common base class for all events.
Definition: event.hpp:22
void send_event(Args &&...args)
Sends the passed event asynchronously to the handler.
Definition: event_handler.hpp:96