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
9namespace fz {
10
54class FZ_PUBLIC_SYMBOL event_handler
55{
56public:
57 event_handler() = delete;
58
59 explicit event_handler(event_loop& loop);
60 virtual ~event_handler();
61
63 event_handler& operator=(event_handler const&) = delete;
64
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)...));
90 }
91
92 template<typename T>
93 void send_event(T* evt) {
94 event_loop_.send_event(this, evt);
95 }
96
113 timer_id add_timer(duration const& interval, bool one_shot);
114
119 void stop_timer(timer_id id);
120
121 event_loop & event_loop_;
122private:
123 friend class event_loop;
124 bool removing_{};
125};
126
141template<typename T, typename F>
142bool dispatch(event_base const& ev, F&& f)
143{
144 bool const same = same_type<T>(ev);
145 if (same) {
146 T const* e = static_cast<T const*>(&ev);
147 std::apply(std::forward<F>(f), e->v_);
148 }
149 return same;
150}
151
167template<typename T, typename H, typename F>
168bool dispatch(event_base const& ev, H* h, F&& f)
169{
170 bool const same = same_type<T>(ev);
171 if (same) {
172 T const* e = static_cast<T const*>(&ev);
173 apply(h, std::forward<F>(f), e->v_);
174 }
175 return same;
176}
177
196template<typename T, typename ... Ts, typename H, typename F, typename ... Fs>
197bool dispatch(event_base const& ev, H* h, F&& f, Fs&& ... fs)
198{
199 if (dispatch<T>(ev, h, std::forward<F>(f))) {
200 return true;
201 }
202
203 return dispatch<Ts...>(ev, h, std::forward<Fs>(fs)...);
204}
205
206}
207
208#endif
The duration class represents a time interval in milliseconds.
Definition: time.hpp:286
Common base class for all events.
Definition: event.hpp:23
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
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.
virtual void operator()(event_base const &)=0
Called by the event loop in the worker thread with the event to process.
void send_event(Args &&... args)
Sends the passed event asynchronously to the handler.
Definition: event_handler.hpp:88
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:34
A simple threaded event loop for the typesafe event system.
The namespace used by libfilezilla.
Definition: apply.hpp:17
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
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition: event_handler.hpp:142