EventManager.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-17
  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. #include <utility>
  13. using ls::std::core::Class;
  14. using ls::std::core::EventNotHandledException;
  15. using ls::std::event::Event;
  16. using ls::std::event::EventListener;
  17. using ls::std::event::EventManager;
  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) const
  48. {
  49. if (!this->_observesEvent(_event))
  50. {
  51. throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
  52. }
  53. else
  54. {
  55. for (const auto &[eventName, listeners] : this->inventory)
  56. {
  57. EventManager::_notifyListeners(listeners);
  58. }
  59. }
  60. }
  61. listener_id EventManager::requestListenerId()
  62. {
  63. listener_id providedId = this->provisionId;
  64. ++this->provisionId;
  65. return providedId;
  66. }
  67. void EventManager::subscribeListenerForEvent(shared_ptr<EventListener> _listener, const Event &_event, event_action _action)
  68. {
  69. if (!this->_observesEvent(_event))
  70. {
  71. this->inventory[_event.getClassName()] = {};
  72. }
  73. auto inventoryEntry = make_pair<shared_ptr<EventListener>, event_action>(::move(_listener), ::move(_action));
  74. this->inventory[_event.getClassName()].push_back(inventoryEntry);
  75. }
  76. void EventManager::unsubscribeListenerForEvent(const shared_ptr<EventListener> &_listener, const Event &_event)
  77. {
  78. if (this->_observesEvent(_event))
  79. {
  80. for (auto iterator = this->inventory[_event.getClassName()].begin(); iterator != this->inventory[_event.getClassName()].end(); iterator++)
  81. {
  82. if (iterator->first->getId() == _listener->getId())
  83. {
  84. this->inventory[_event.getClassName()].erase(iterator);
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. void EventManager::_notifyListeners(const event_listeners &_listeners)
  91. {
  92. for (const auto &[listener, eventAction] : _listeners)
  93. {
  94. eventAction();
  95. }
  96. }
  97. bool EventManager::_observesEvent(const Event &_event) const
  98. {
  99. return this->inventory.find(_event.getClassName()) != this->inventory.end();
  100. }