1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2024-05-16
- * Changed: 2024-05-17
- *
- * */
- #include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
- #include <ls-std/event/EventListener.hpp>
- #include <ls-std/event/EventManager.hpp>
- 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<Class> &_manager)
- {
- shared_ptr<EventManager> manager = dynamic_pointer_cast<EventManager>(_manager);
- if (this->id == 0)
- {
- this->setId(manager->requestListenerId());
- }
- }
- bool EventListener::_subscribe(const Event &_event, const event_action &_action)
- {
- bool subscribed{};
- if (shared_ptr<EventManager> manager = dynamic_pointer_cast<EventManager>(_event.getManager()); !manager->holdsListenerForEvent(this->id, _event))
- {
- manager->subscribeListenerForEvent(shared_from_this(), _event, _action);
- subscribed = manager->holdsListenerForEvent(this->id, _event);
- }
- return subscribed;
- }
|