diff --git a/src/event_manager.cc b/src/event_manager.cc index 12b23f7e..410b85b5 100644 --- a/src/event_manager.cc +++ b/src/event_manager.cc @@ -15,7 +15,7 @@ void EventManager::watch(int fd, EventHandler handler) throw runtime_error("fd already watched"); m_events.push_back(pollfd{ fd, POLLIN | POLLPRI, 0 }); - m_handlers.push_back(std::move(handler)); + m_handlers.emplace(fd, std::move(handler)); } void EventManager::unwatch(int fd) @@ -25,7 +25,7 @@ void EventManager::unwatch(int fd) if (m_events[i].fd == fd) { m_events.erase(m_events.begin() + i); - m_handlers.erase(m_handlers.begin() + i); + m_handlers.erase(fd); return; } } @@ -39,7 +39,7 @@ void EventManager::handle_next_events() { if ((res > 0 and m_events[i].revents) or contains(m_forced, m_events[i].fd)) - m_handlers[i](m_events[i].fd); + m_handlers[m_events[i].fd](m_events[i].fd); } m_forced.clear(); } diff --git a/src/event_manager.hh b/src/event_manager.hh index 1e8d5a87..9f21fa06 100644 --- a/src/event_manager.hh +++ b/src/event_manager.hh @@ -2,6 +2,7 @@ #define event_manager_hh_INCLUDED #include +#include #include "utils.hh" @@ -24,7 +25,7 @@ public: private: std::vector m_events; - std::vector m_handlers; + std::unordered_map m_handlers; std::vector m_forced; };