![]() |
libfilezilla
|
00001 #ifndef LIBFILEZILLA_EVENT_HANDLER 00002 #define LIBFILEZILLA_EVENT_HANDLER 00003 00004 #include "event_loop.hpp" 00005 00009 namespace fz { 00010 00054 class FZ_PUBLIC_SYMBOL event_handler 00055 { 00056 public: 00057 event_handler() = delete; 00058 00059 explicit event_handler(event_loop& loop); 00060 virtual ~event_handler(); 00061 00062 event_handler(event_handler const&) = delete; 00063 event_handler& operator=(event_handler const&) = delete; 00064 00073 void remove_handler(); 00074 00081 virtual void operator()(event_base const&) = 0; 00082 00083 /* \brief Sends the passed event asynchronously to the handler. 00084 * 00085 * Can be called from any thread. 00086 * 00087 * All events are processed in the order they are sent, excluding possible races between threads. 00088 */ 00089 template<typename T, typename... Args> 00090 void send_event(Args&&... args) { 00091 event_loop_.send_event(this, new T(std::forward<Args>(args)...)); 00092 } 00093 00094 template<typename T> 00095 void send_event(T* evt) { 00096 event_loop_.send_event(this, evt); 00097 } 00098 00115 timer_id add_timer(duration const& interval, bool one_shot); 00116 00121 void stop_timer(timer_id id); 00122 00123 event_loop & event_loop_; 00124 private: 00125 friend class event_loop; 00126 bool removing_{}; 00127 }; 00128 00143 template<typename T, typename F> 00144 bool dispatch(event_base const& ev, F&& f) 00145 { 00146 bool const same = same_type<T>(ev); 00147 if (same) { 00148 T const* e = static_cast<T const*>(&ev); 00149 apply(std::forward<F>(f), e->v_); 00150 } 00151 return same; 00152 } 00153 00169 template<typename T, typename H, typename F> 00170 bool dispatch(event_base const& ev, H* h, F&& f) 00171 { 00172 bool const same = same_type<T>(ev); 00173 if (same) { 00174 T const* e = static_cast<T const*>(&ev); 00175 apply(h, std::forward<F>(f), e->v_); 00176 } 00177 return same; 00178 } 00179 00198 template<typename T, typename ... Ts, typename H, typename F, typename ... Fs> 00199 bool dispatch(event_base const& ev, H* h, F&& f, Fs&& ... fs) 00200 { 00201 if (dispatch<T>(ev, h, std::forward<F>(f))) { 00202 return true; 00203 } 00204 00205 return dispatch<Ts...>(ev, h, std::forward<Fs>(fs)...); 00206 } 00207 00208 } 00209 00210 #endif