#ifndef event_manager_hh_INCLUDED #define event_manager_hh_INCLUDED #include "utils.hh" #include #include namespace Kakoune { class FDWatcher { public: using Callback = std::function; FDWatcher(int fd, Callback callback); ~FDWatcher(); int fd() const { return m_fd; } void run() { m_callback(*this); } private: FDWatcher(const FDWatcher&) = delete; int m_fd; Callback m_callback; }; using Clock = std::chrono::steady_clock; using TimePoint = Clock::time_point; class Timer { public: using Callback = std::function; Timer(TimePoint date, Callback callback); ~Timer(); TimePoint next_date() const { return m_date; } void set_next_date(TimePoint date) { m_date = date; } void run(); private: TimePoint m_date; Callback m_callback; }; // The EventManager provides an interface to file descriptor // based event handling. // // The program main loop should call handle_next_events() // until it's time to quit. class EventManager : public Singleton { public: EventManager(); ~EventManager(); void handle_next_events(); // force the watchers associated with fd to be executed // on next handle_next_events call. void force_signal(int fd); private: friend class FDWatcher; friend class Timer; std::unordered_set m_fd_watchers; std::unordered_set m_timers; std::vector m_forced_fd; TimePoint m_last; }; } #endif // event_manager_hh_INCLUDED