EventListener.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2024-05-16
  6. * Changed: 2024-05-31
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
  10. #include <ls-std/event/EventListener.hpp>
  11. #include <ls-std/event/EventManager.hpp>
  12. using ls::std::core::Class;
  13. using ls::std::core::NullPointerEvaluator;
  14. using ls::std::event::Event;
  15. using ls::std::event::EventListener;
  16. using ls::std::event::EventManager;
  17. using ls::std::event::EventParameter;
  18. using ls::std::event::type::event_action;
  19. using ls::std::event::type::listener_id;
  20. using ::std::dynamic_pointer_cast;
  21. using ::std::shared_ptr;
  22. EventListener::EventListener() : Class("EventListener")
  23. {}
  24. EventListener::~EventListener() noexcept = default;
  25. listener_id EventListener::getId() const
  26. {
  27. return this->id;
  28. }
  29. void EventListener::setId(const listener_id _id)
  30. {
  31. this->id = _id;
  32. }
  33. void EventListener::setParameter(const EventParameter &_parameter)
  34. {
  35. this->parameter = _parameter;
  36. }
  37. bool EventListener::subscribe(const Event &_event, const event_action &_action)
  38. {
  39. NullPointerEvaluator(_event.getManager(), "no event manager is provided for " + _event.getClassName()).evaluate();
  40. this->_requestListenerId(_event.getManager());
  41. return this->_subscribe(_event, _action);
  42. }
  43. bool EventListener::unsubscribe(const Event &_event)
  44. {
  45. NullPointerEvaluator(_event.getManager(), "no event manager is provided for " + _event.getClassName()).evaluate();
  46. return this->_unsubscribe(_event);
  47. }
  48. void EventListener::_requestListenerId(const shared_ptr<Class> &_manager)
  49. {
  50. const shared_ptr<EventManager> manager = dynamic_pointer_cast<EventManager>(_manager);
  51. if (this->id == 0)
  52. {
  53. this->setId(manager->requestListenerId());
  54. }
  55. }
  56. bool EventListener::_subscribe(const Event &_event, const event_action &_action)
  57. {
  58. bool subscribed{};
  59. if (const shared_ptr<EventManager> manager = dynamic_pointer_cast<EventManager>(_event.getManager()); !manager->holdsListenerForEvent(this->id, _event))
  60. {
  61. manager->subscribeListenerForEvent(shared_from_this(), _event, _action);
  62. subscribed = manager->holdsListenerForEvent(this->id, _event);
  63. }
  64. return subscribed;
  65. }
  66. bool EventListener::_unsubscribe(const Event &_event)
  67. {
  68. bool unsubscribed{};
  69. if (const shared_ptr<EventManager> manager = dynamic_pointer_cast<EventManager>(_event.getManager()); manager->holdsListenerForEvent(this->id, _event))
  70. {
  71. manager->unsubscribeListenerForEvent(shared_from_this(), _event);
  72. unsubscribed = !manager->holdsListenerForEvent(this->id, _event);
  73. }
  74. return unsubscribed;
  75. }