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 
54 class FZ_PUBLIC_SYMBOL event_handler
55 {
56 public:
57  event_handler() = delete;
58 
59  explicit event_handler(event_loop& loop);
60  virtual ~event_handler();
61 
62  event_handler(event_handler const& h);
63  event_handler& operator=(event_handler const&) = delete;
64 
71  void remove_handler();
72 
79  virtual void operator()(event_base const&) = 0;
80 
87  template<typename T, typename... Args>
88  void send_event(Args&&... args) {
89  event_loop_.send_event(this, new T(std::forward<Args>(args)...), true);
90  }
91 
92  template<typename T>
93  void send_event(T* evt) {
94  event_loop_.send_event(this, evt, true);
95  }
96 
97 
99  template<typename T>
100  void send_persistent_event(T* evt) {
101  event_loop_.send_event(this, evt, false);
102  }
103 
121  timer_id add_timer(monotonic_clock const &deadline, duration const& interval = {});
122 
139  timer_id add_timer(duration const& interval, bool one_shot);
140 
145  void stop_timer(timer_id id);
146 
154  timer_id stop_add_timer(timer_id id, monotonic_clock const& deadline, duration const& interval = {});
155 
163  timer_id stop_add_timer(timer_id id, duration const& interval, bool one_shot);
164 
165  void filter_events(std::function<bool(event_base& ev)> const& filter) {
166  event_loop_.filter_events([&](event_handler*& h, event_base& ev) {
167  if (h != this) {
168  return false;
169  }
170  return filter(ev);
171  });
172  }
173 
174  void resend_current_event() {
175  event_loop_.resend_current_event();
176  }
177 
178  event_loop & event_loop_;
179 private:
180  friend class event_loop;
181  bool removing_{};
182 };
183 
198 template<typename T, typename F>
199 bool dispatch(event_base const& ev, F&& f)
200 {
201  bool const same = same_type<T>(ev);
202  if (same) {
203  T const* e = static_cast<T const*>(&ev);
204  std::apply(std::forward<F>(f), e->v_);
205  }
206  return same;
207 }
208 
224 template<typename T, typename H, typename F>
225 bool dispatch(event_base const& ev, H* h, F&& f)
226 {
227  bool const same = same_type<T>(ev);
228  if (same) {
229  T const* e = static_cast<T const*>(&ev);
230  apply(h, std::forward<F>(f), e->v_);
231  }
232  return same;
233 }
234 
253 template<typename T, typename ... Ts, typename H, typename F, typename ... Fs>
254 bool dispatch(event_base const& ev, H* h, F&& f, Fs&& ... fs)
255 {
256  if (dispatch<T>(ev, h, std::forward<F>(f))) {
257  return true;
258  }
259 
260  return dispatch<Ts...>(ev, h, std::forward<Fs>(fs)...);
261 }
262 
263 }
264 
265 #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
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:54
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition: event_handler.hpp:199
void send_persistent_event(T *evt)
Be careful with lifetime.
Definition: event_handler.hpp:100
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:88