/** * @file EventLoop.h * @author apio (cloudapio.eu) * @brief Base class for event-driven applications. * * @copyright Copyright (c) 2023, the Luna authors. * */ #pragma once #include #include #include namespace os { /** * @brief Event loop implementation. */ class EventLoop { public: /** * @brief Construct a new event loop, which will automatically register itself as the current global event loop. */ EventLoop(); ~EventLoop(); EventLoop(EventLoop&&) = delete; EventLoop(const EventLoop&) = delete; /** * @brief Fetch the current global event loop. * * @return EventLoop& The current global event loop. */ static EventLoop& the(); /** * @brief Register a new event listener on a file descriptor. * * @param fd The file descriptor to listen on. * @param listener The callback function to invoke when events occur on the file descriptor. The first parameter * is the file descriptor registered, and the second argument is the type of event (POLLIN or POLLHUP) * @return Result Whether the operation succeeded. */ Result register_fd_listener(int fd, void (*listener)(int, int)); /** * @brief Run the event loop until it is asked to quit. * * @return int The status passed to the quit() method. */ int run(); /** * @brief Ask the event loop to quit. * * @param status The status value to return from run(). */ void quit(int status = 0); private: HashMap m_fd_listeners; Vector m_pollfds; bool m_should_quit { false }; int m_quit_status { 0 }; }; }