EventManager.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-30
  7. *
  8. * */
  9. #include <ls-std/core/exception/EventNotHandledException.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::EventNotHandledException;
  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::event_listeners;
  20. using ls::std::event::type::listener_id;
  21. using ::std::make_pair;
  22. using ::std::move;
  23. using ::std::shared_ptr;
  24. EventManager::EventManager() : Class("EventManager")
  25. {}
  26. EventManager::~EventManager() noexcept = default;
  27. listener_id EventManager::getNextProvisionId() const
  28. {
  29. return this->provisionId;
  30. }
  31. bool EventManager::holdsListenerForEvent(listener_id _id, const Event &_event)
  32. {
  33. bool holdsListener{};
  34. if (this->_observesEvent(_event))
  35. {
  36. for (const auto &[listener, eventAction] : this->inventory[_event.getClassName()])
  37. {
  38. holdsListener = listener->getId() == _id;
  39. if (holdsListener)
  40. {
  41. break;
  42. }
  43. }
  44. }
  45. return holdsListener;
  46. }
  47. void EventManager::invoke(const Event &_event)
  48. {
  49. if (!this->_observesEvent(_event))
  50. {
  51. throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
  52. }
  53. const auto &listeners = this->inventory[_event.getClassName()];
  54. EventManager::_notifyListeners(listeners);
  55. }
  56. void EventManager::invoke(const Event &_event, const EventParameter &_parameter)
  57. {
  58. if (!this->_observesEvent(_event))
  59. {
  60. throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
  61. }
  62. const auto &listeners = this->inventory[_event.getClassName()];
  63. EventManager::_giveListenersParameter(listeners, _parameter);
  64. EventManager::_notifyListeners(listeners);
  65. }
  66. listener_id EventManager::requestListenerId()
  67. {
  68. listener_id providedId = this->provisionId;
  69. ++this->provisionId;
  70. return providedId;
  71. }
  72. void EventManager::subscribeListenerForEvent(shared_ptr<EventListener> _listener, const Event &_event, event_action _action)
  73. {
  74. if (!this->_observesEvent(_event))
  75. {
  76. this->inventory[_event.getClassName()] = {};
  77. }
  78. auto inventoryEntry = make_pair<shared_ptr<EventListener>, event_action>(::move(_listener), ::move(_action));
  79. this->inventory[_event.getClassName()].push_back(inventoryEntry);
  80. }
  81. void EventManager::unsubscribeListenerForEvent(const shared_ptr<EventListener> &_listener, const Event &_event)
  82. {
  83. if (this->_observesEvent(_event))
  84. {
  85. for (auto iterator = this->inventory[_event.getClassName()].begin(); iterator != this->inventory[_event.getClassName()].end(); iterator++)
  86. {
  87. if (iterator->first->getId() == _listener->getId())
  88. {
  89. this->inventory[_event.getClassName()].erase(iterator);
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. void EventManager::_giveListenersParameter(const event_listeners &_listeners, const EventParameter &_parameter)
  96. {
  97. for (const auto &[listener, action] : _listeners)
  98. {
  99. listener->setParameter(_parameter);
  100. }
  101. }
  102. void EventManager::_notifyListeners(const event_listeners &_listeners)
  103. {
  104. for (const auto &[listener, eventAction] : _listeners)
  105. {
  106. eventAction();
  107. }
  108. }
  109. bool EventManager::_observesEvent(const Event &_event) const
  110. {
  111. return this->inventory.find(_event.getClassName()) != this->inventory.end();
  112. }