123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2024-05-16
- * Changed: 2024-05-30
- *
- * */
- #include <ls-std/core/exception/EventNotHandledException.hpp>
- #include <ls-std/event/EventListener.hpp>
- #include <ls-std/event/EventManager.hpp>
- #include <utility>
- using ls::std::core::Class;
- using ls::std::core::EventNotHandledException;
- using ls::std::event::Event;
- using ls::std::event::EventListener;
- using ls::std::event::EventManager;
- using ls::std::event::type::event_action;
- using ls::std::event::type::event_listeners;
- using ls::std::event::type::listener_id;
- using ::std::make_pair;
- using ::std::move;
- using ::std::shared_ptr;
- EventManager::EventManager() : Class("EventManager")
- {}
- EventManager::~EventManager() noexcept = default;
- listener_id EventManager::getNextProvisionId() const
- {
- return this->provisionId;
- }
- bool EventManager::holdsListenerForEvent(listener_id _id, const Event &_event)
- {
- bool holdsListener{};
- if (this->_observesEvent(_event))
- {
- for (const auto &[listener, eventAction] : this->inventory[_event.getClassName()])
- {
- holdsListener = listener->getId() == _id;
- if (holdsListener)
- {
- break;
- }
- }
- }
- return holdsListener;
- }
- void EventManager::invoke(const Event &_event)
- {
- if (!this->_observesEvent(_event))
- {
- throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
- }
- else
- {
- const auto &listeners = this->inventory[_event.getClassName()];
- EventManager::_notifyListeners(listeners);
- }
- }
- listener_id EventManager::requestListenerId()
- {
- listener_id providedId = this->provisionId;
- ++this->provisionId;
- return providedId;
- }
- void EventManager::subscribeListenerForEvent(shared_ptr<EventListener> _listener, const Event &_event, event_action _action)
- {
- if (!this->_observesEvent(_event))
- {
- this->inventory[_event.getClassName()] = {};
- }
- auto inventoryEntry = make_pair<shared_ptr<EventListener>, event_action>(::move(_listener), ::move(_action));
- this->inventory[_event.getClassName()].push_back(inventoryEntry);
- }
- void EventManager::unsubscribeListenerForEvent(const shared_ptr<EventListener> &_listener, const Event &_event)
- {
- if (this->_observesEvent(_event))
- {
- for (auto iterator = this->inventory[_event.getClassName()].begin(); iterator != this->inventory[_event.getClassName()].end(); iterator++)
- {
- if (iterator->first->getId() == _listener->getId())
- {
- this->inventory[_event.getClassName()].erase(iterator);
- break;
- }
- }
- }
- }
- void EventManager::_notifyListeners(const event_listeners &_listeners)
- {
- for (const auto &[listener, eventAction] : _listeners)
- {
- eventAction();
- }
- }
- bool EventManager::_observesEvent(const Event &_event) const
- {
- return this->inventory.find(_event.getClassName()) != this->inventory.end();
- }
|