/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #ifndef ANTKEEPER_EVENT_DISPATCHER_HPP #define ANTKEEPER_EVENT_DISPATCHER_HPP #include "event.hpp" #include "event-handler.hpp" #include #include #include /** * Queues events and dispatches them to event handlers. */ class event_dispatcher { public: /// Creates an event dispatcher event_dispatcher(); /// Destroys an event dispatcher ~event_dispatcher(); /** * Processes all pending subscriptions and unsubscriptions, dispatches queued events, then dispatches due scheduled events. * * @param time The current time. */ void update(double time); /** * Subscribes an event handler to event dispatches. * * @param handler Handler to subscribe. */ template void subscribe(event_handler* handler); /** * Unsubscribes an event handler from event dispatches. * * @param handler Handler to unsubscribe. */ template void unsubscribe(event_handler* handler); /** * Adds an event to the queue. * * @param event Event to queue. */ void queue(const event_base& event); /** * Schedules an event to be dispatched at a specific time. * * @param event Event to schedule. * @param time Time that the event should be dispatched. */ void schedule(const event_base& event, double time); /** * Dispatches a single event. * * @param event Event to dispatch. */ void dispatch(const event_base& event); /** * Dispatches all events in the queue. */ void flush(); /// Removes all queued and scheduled events from the queue without notifying handlers. void clear(); private: std::list> to_subscribe; std::list> to_unsubscribe; std::map> handler_map; std::list queued_events; std::multimap scheduled_events; }; template void event_dispatcher::subscribe(event_handler* handler) { to_subscribe.push_back(std::make_tuple(handler->get_handled_event_type_id(), handler)); } template void event_dispatcher::unsubscribe(event_handler* handler) { to_unsubscribe.push_back(std::make_tuple(handler->get_handled_event_type_id(), handler)); } inline void event_dispatcher::queue(const event_base& event) { queued_events.push_back(event.clone()); } inline void event_dispatcher::schedule(const event_base& event, double time) { scheduled_events.insert(std::pair(time, event.clone())); } inline void event_dispatcher::dispatch(const event_base& event) { // Get list of handlers for this type of event const std::list& handlers = handler_map[event.get_event_type_id()]; // For each handler for (auto handler = handlers.begin(); handler != handlers.end(); ++handler) { // Pass event to the handler (*handler)->route_event(event); } } #endif // ANTKEEPER_EVENT_DISPATCHER_HPP