/* * Author: Patrick-Christopher Mattulat * Company: Lynar Studios * E-Mail: webmaster@lynarstudios.com * Created: 2024-05-16 * Changed: 2024-05-17 * * */ #include #include #include using ls::std::core::Class; using ls::std::core::NullPointerEvaluator; 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::listener_id; using ::std::dynamic_pointer_cast; using ::std::shared_ptr; EventListener::EventListener() : Class("EventListener") {} EventListener::~EventListener() noexcept = default; listener_id EventListener::getId() const { return this->id; } void EventListener::setId(listener_id _id) { this->id = _id; } bool EventListener::subscribe(const Event &_event, const event_action &_action) { NullPointerEvaluator(_event.getManager(), "no event manager is provided for " + _event.getClassName()).evaluate(); this->_requestListenerId(_event.getManager()); return this->_subscribe(_event, _action); } void EventListener::_requestListenerId(const shared_ptr &_manager) { shared_ptr manager = dynamic_pointer_cast(_manager); if (this->id == 0) { this->setId(manager->requestListenerId()); } } bool EventListener::_subscribe(const Event &_event, const event_action &_action) { bool subscribed{}; if (shared_ptr manager = dynamic_pointer_cast(_event.getManager()); !manager->holdsListenerForEvent(this->id, _event)) { manager->subscribeListenerForEvent(shared_from_this(), _event, _action); subscribed = manager->holdsListenerForEvent(this->id, _event); } return subscribed; }