libfilezilla
event_loop.hpp
Go to the documentation of this file.
00001 #ifndef LIBFILEZILLA_EVENT_LOOP_HEADER
00002 #define LIBFILEZILLA_EVENT_LOOP_HEADER
00003 
00004 #include "apply.hpp"
00005 #include "event.hpp"
00006 #include "mutex.hpp"
00007 #include "time.hpp"
00008 #include "thread.hpp"
00009 
00010 #include <deque>
00011 #include <functional>
00012 #include <vector>
00013 
00018 namespace fz {
00019 
00020 class event_handler;
00021 
00030 class FZ_PUBLIC_SYMBOL event_loop final : private thread
00031 {
00032 public:
00033     typedef std::deque<std::pair<event_handler*, event_base*>> Events;
00034 
00036     event_loop();
00037 
00039     virtual ~event_loop();
00040 
00041     event_loop(event_loop const&) = delete;
00042     event_loop& operator=(event_loop const&) = delete;
00043 
00055     void filter_events(std::function<bool (Events::value_type&)> const& filter);
00056 
00061     void stop();
00062 
00063 private:
00064     friend class event_handler;
00065 
00066     void FZ_PRIVATE_SYMBOL remove_handler(event_handler* handler);
00067 
00068     timer_id FZ_PRIVATE_SYMBOL add_timer(event_handler* handler, duration const& interval, bool one_shot);
00069     void FZ_PRIVATE_SYMBOL stop_timer(timer_id id);
00070 
00071     void send_event(event_handler* handler, event_base* evt);
00072 
00073     // Process the next (if any) event. Returns true if an event has been processed
00074     bool FZ_PRIVATE_SYMBOL process_event(scoped_lock & l);
00075 
00076     // Process timers. Returns true if a timer has been triggered
00077     bool FZ_PRIVATE_SYMBOL process_timers(scoped_lock & l, monotonic_clock& now);
00078 
00079     virtual void FZ_PRIVATE_SYMBOL entry();
00080 
00081     struct timer_data final
00082     {
00083         event_handler* handler_{};
00084         timer_id id_{};
00085         monotonic_clock deadline_;
00086         duration interval_{};
00087     };
00088 
00089     typedef std::vector<timer_data> Timers;
00090 
00091     Events pending_events_;
00092     Timers timers_;
00093 
00094     mutex sync_;
00095     condition cond_;
00096 
00097     bool quit_{};
00098 
00099     event_handler * active_handler_{};
00100 
00101 
00102     monotonic_clock deadline_;
00103 
00104     timer_id next_timer_id_{};
00105 };
00106 
00107 }
00108 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines