diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2013-01-10 18:54:40 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2013-01-10 19:10:42 +0100 |
| commit | 79d28e68dc872fd729932c907c6d79710789d579 (patch) | |
| tree | 46976b4a6dd4643d297c7ff468805526eb5288c6 /src/event_manager.hh | |
| parent | bba7152063e68801ee9a9d08c47b31216a9ce1fd (diff) | |
Refactor EventManager
Watching a file descriptor is now done using a FDWatcher object
Diffstat (limited to 'src/event_manager.hh')
| -rw-r--r-- | src/event_manager.hh | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/src/event_manager.hh b/src/event_manager.hh index fc1e94da..08c9bfca 100644 --- a/src/event_manager.hh +++ b/src/event_manager.hh @@ -1,15 +1,24 @@ #ifndef event_manager_hh_INCLUDED #define event_manager_hh_INCLUDED -#include <poll.h> -#include <unordered_map> - #include "utils.hh" namespace Kakoune { -using EventHandler = std::function<void (int fd)>; +class FDWatcher +{ +public: + using Callback = std::function<void (FDWatcher& watcher)>; + FDWatcher(int fd, Callback callback); + ~FDWatcher(); + + int fd() const { return m_fd; } + void run() { m_callback(*this); } +private: + int m_fd; + Callback m_callback; +}; // The EventManager provides an interface to file descriptor // based event handling. @@ -20,26 +29,21 @@ class EventManager : public Singleton<EventManager> { public: EventManager(); - // Watch the given file descriptor, when data becomes - // ready, handler will be called with fd as parameter. - // It is an error to register multiple handlers on the - // same file descriptor. - void watch(int fd, EventHandler handler); - - // stop watching fd - void unwatch(int fd); + ~EventManager(); void handle_next_events(); - // force the handler associated with fd to be executed + // force the watchers associated with fd to be executed // on next handle_next_events call. void force_signal(int fd); private: - std::vector<pollfd> m_events; - std::vector<std::unique_ptr<EventHandler>> m_handlers; - std::vector<std::unique_ptr<EventHandler>> m_handlers_trash; - std::vector<int> m_forced; + friend class FDWatcher; + void register_fd_watcher(FDWatcher* watcher); + void unregister_fd_watcher(FDWatcher* watcher); + + std::vector<FDWatcher*> m_fd_watchers; + std::vector<int> m_forced_fd; }; } |
