EventManager.cpp 2.9 KB

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