libfilezilla
Loading...
Searching...
No Matches
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
6#include <tuple>
7
11namespace fz {
12
56
58{
59 child_event_handler
60};
61
62class FZ_PUBLIC_SYMBOL event_handler
63{
64public:
65 event_handler() = delete;
66
67 explicit event_handler(event_loop& loop);
68 virtual ~event_handler();
69
70 event_handler(event_handler const& h);
71 event_handler(event_handler & h, event_handler_option opt);
72
73 event_handler& operator=(event_handler const&) = delete;
74
86
93 virtual void operator()(event_base const&) = 0;
94
101 template<typename T, typename... Args>
102 void send_event(Args&&... args) {
103 event_loop_.send_event(this, new T(std::forward<Args>(args)...), true);
104 }
105
106 template<typename T>
107 void send_event(T* evt) {
108 event_loop_.send_event(this, evt, true);
109 }
110
111
113 template<typename T>
115 event_loop_.send_event(this, evt, false);
116 }
117
133 timer_id add_timer(monotonic_clock const &deadline, duration const& interval = {});
134
149 timer_id add_timer(duration const& interval, bool one_shot);
150
155 void stop_timer(timer_id id);
156
164 timer_id stop_add_timer(timer_id id, monotonic_clock const& deadline, duration const& interval = {});
165
173 timer_id stop_add_timer(timer_id id, duration const& interval, bool one_shot);
174
175 void filter_events(std::function<bool(event_base& ev)> const& filter) {
176 event_loop_.filter_events([&](event_handler*& h, event_base& ev) {
177 if (h != this) {
178 return false;
179 }
180 return filter(ev);
181 });
182 }
183
190 event_loop_.resend_current_event();
191 }
192
193 void remove_events(event_source const* const source);
194
195 template<typename T>
196 void remove_events() {
197 remove_events_of_type(T::type());
198 }
199
200 event_loop & event_loop_;
201
202private:
203 void remove_events_of_type(size_t t);
204
205 friend class event_loop;
206 bool removing_{};
207
208 void remove_from_parent();
209 event_handler* parent_{};
210 event_handler* next_{};
211 event_handler* child_{};
212};
213
228template<typename T, typename F>
229bool dispatch(event_base const& ev, F&& f)
230{
231 bool const same = same_type<T>(ev);
232 if (same) {
233 T const* e = static_cast<T const*>(&ev);
234 std::apply(std::forward<F>(f), e->v_);
235 }
236 return same;
237}
238
254template<typename T, typename H, typename F>
255bool dispatch(event_base const& ev, H* h, F&& f)
256{
257 bool const same = same_type<T>(ev);
258 if (same) {
259 T const* e = static_cast<T const*>(&ev);
260 apply(h, std::forward<F>(f), e->v_);
261 }
262 return same;
263}
264
283template<typename T, typename ... Ts, typename H, typename F, typename ... Fs>
284bool dispatch(event_base const& ev, H* h, F&& f, Fs&& ... fs)
285{
286 if (dispatch<T>(ev, h, std::forward<F>(f))) {
287 return true;
288 }
289
290 return dispatch<Ts...>(ev, h, std::forward<Fs>(fs)...);
291}
292
293}
294
295#endif
The duration class represents a time interval in milliseconds.
Definition time.hpp:291
Common base class for all events.
Definition event.hpp:23
timer_id add_timer(duration const &interval, bool one_shot)
Adds a timer, returns the timer id.
void stop_timer(timer_id id)
void remove_handler()
Deactivates handler, removes all pending events and stops all timers for this handler.
timer_id stop_add_timer(timer_id id, duration const &interval, bool one_shot)
virtual void operator()(event_base const &)=0
Called by the event loop in the worker thread with the event to process.
timer_id add_timer(monotonic_clock const &deadline, duration const &interval={})
Adds a timer, returns the timer id.
void send_event(Args &&... args)
Sends the passed event asynchronously to the handler.
Definition event_handler.hpp:102
void resend_current_event()
Definition event_handler.hpp:189
void send_persistent_event(T *evt)
Be careful with lifetime.
Definition event_handler.hpp:114
timer_id stop_add_timer(timer_id id, monotonic_clock const &deadline, duration const &interval={})
Definition event.hpp:101
A monotonic clock (aka steady clock) is independent from walltime.
Definition time.hpp:403
A simple threaded event loop for the typesafe event system.
The namespace used by libfilezilla.
Definition apply.hpp:17
event_handler_option
Simple handler for asynchronous event processing.
Definition event_handler.hpp:58
bool same_type(event_base const &ev)
Definition event.hpp:154
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition event_handler.hpp:229
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