![]() |
libfilezilla
|
Simple handler for asynchronous event processing. More...
#include <event_handler.hpp>
Public Member Functions | |
event_handler (event_loop &loop) | |
event_handler (event_handler const &) | |
event_handler & | operator= (event_handler const &) |
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. | |
template<typename T , typename... Args> | |
void | send_event (Args &&...args) |
template<typename T > | |
void | send_event (T *evt) |
timer_id | add_timer (duration const &interval, bool one_shot) |
Adds a timer, returns the timer id. | |
void | stop_timer (timer_id id) |
Public Attributes | |
event_loop & | event_loop_ |
Friends | |
class | event_loop |
Simple handler for asynchronous event processing.
Usage example:
struct foo_event_type{}; // Any uniquely named type that's not implicitly convertible typedef fz::simple_event<foo_event_type, int, std::string> foo_event; struct bar_event_type{}; typedef fz::simple_event<bar_event_type> bar_event; class my_handler final : public fz::event_handler { public: my_handler(fz::event_loop& loop) : fz::event_handler(loop) {} virtual ~my_handler() { // It is imperative to call remove_handler remove_handler(); } void foo(int v, std::string const& s) { std::cout << "foo called with:" << s << v; } void bar() { std::cout << "bar called"; } virtual void operator()(event_base const& ev) { // Tip: Put in order of decreasing frequency fz::dispatch<foo_event, bar_event>(ev, this, &my_handler::foo, &my_handler::bar); } }; fz::event_loop loop; my_handler h(loop); h.SendEvent<foo_event>(42, "Don't Panic");
timer_id add_timer | ( | duration const & | interval, |
bool | one_shot | ||
) |
Adds a timer, returns the timer id.
Once the interval expires, you get a timer event from the event loop.
One-shot timers are deleted automatically
For periodic timers, the next event is scheduled right before the callback is called. If multiple intervals expire before the timer fires, e.g. under heavy load, only one event is sent.
If multiple different timers have expired, the order in which the callbacks are executed is unspecified, there is no fairness guarantee.
Timers take precedence over other queued events.
virtual void operator() | ( | event_base const & | ) | [pure virtual] |
Called by the event loop in the worker thread with the event to process.
Override in your derived class.
Consider using dispatch inside your function.
void remove_handler | ( | ) |
Deactivates handler, removes all pending events and stops all timers for this handler.
When function returns, handler is not in its callback anymore.
This may deadlock if a handler removes itself inside its own callback.
void stop_timer | ( | timer_id | id | ) |
Stops the given timer.
One-shot timers that have fired stop automatically and do not need to be stopped.