123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2024-05-16
- * Changed: 2024-05-31
- *
- * */
- #include <ls-std/core/exception/EventNotHandledException.hpp>
- #include <ls-std/event/EventListener.hpp>
- #include <ls-std/event/EventManager.hpp>
- 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::EventParameter;
- 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(const 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");
- }
- const auto &listeners = this->inventory[_event.getClassName()];
- _notifyListeners(listeners);
- }
- void EventManager::invoke(const Event &_event, const EventParameter &_parameter)
- {
- if (!this->_observesEvent(_event))
- {
- throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
- }
- const auto &listeners = this->inventory[_event.getClassName()];
- _giveListenersParameter(listeners, _parameter);
- _notifyListeners(listeners);
- }
- listener_id EventManager::requestListenerId()
- {
- const 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()] = {};
- }
- const 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::_giveListenersParameter(const event_listeners &_listeners, const EventParameter &_parameter)
- {
- for (const auto &[listener, action] : _listeners)
- {
- listener->setParameter(_parameter);
- }
- }
- 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();
- }
|