EventManager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/core/exception/EventNotHandledException.hpp>
  12. #include <ls-std/core/exception/EventNotSubscribedException.hpp>
  13. #include <ls-std/event/EventManager.hpp>
  14. using ls::std::core::Class;
  15. using ls::std::core::EmptyStringArgumentEvaluator;
  16. using ls::std::core::EventNotHandledException;
  17. using ls::std::core::EventNotSubscribedException;
  18. using ls::std::core::NullPointerArgumentEvaluator;
  19. using ls::std::core::interface_type::IListener;
  20. using ls::std::core::type::event_id;
  21. using ls::std::event::Event;
  22. using ls::std::event::EventHandler;
  23. using ls::std::event::EventManager;
  24. using std::make_pair;
  25. using std::pair;
  26. using std::shared_ptr;
  27. EventManager::EventManager() : Class("EventManager")
  28. {}
  29. EventManager::~EventManager() noexcept = default;
  30. void EventManager::subscribe(const event_id &_id, const shared_ptr<IListener> &_listener)
  31. {
  32. EmptyStringArgumentEvaluator{_id, "event id is empty and can not be subscribed!"}.evaluate();
  33. NullPointerArgumentEvaluator{_listener, "listener reference for subscribe attempt is null!"}.evaluate();
  34. if (this->_hasEventHandler(_id))
  35. {
  36. this->eventHandlers.at(_id)->addListener(_listener);
  37. }
  38. else
  39. {
  40. throw EventNotSubscribedException{"id: " + _id};
  41. }
  42. }
  43. void EventManager::unsubscribe(const event_id &_id, const shared_ptr<IListener> &_listener)
  44. {
  45. EmptyStringArgumentEvaluator{_id, "event id is empty and can not be unsubscribed!"}.evaluate();
  46. NullPointerArgumentEvaluator{_listener, "listener reference for unsubscribe attempt is null!"}.evaluate();
  47. if (this->_hasEventHandler(_id))
  48. {
  49. this->eventHandlers.at(_id)->removeListener(_listener);
  50. }
  51. }
  52. bool EventManager::addEventHandler(const shared_ptr<EventHandler> &_eventHandler)
  53. {
  54. bool wasAdded{};
  55. NullPointerArgumentEvaluator{_eventHandler, "event handler reference for add attempt is null!"}.evaluate();
  56. if (!this->_hasEventHandler(_eventHandler->getId()))
  57. {
  58. pair<event_id, shared_ptr<EventHandler>> element = make_pair(_eventHandler->getId(), _eventHandler);
  59. wasAdded = this->eventHandlers.insert(element).second;
  60. }
  61. return wasAdded;
  62. }
  63. void EventManager::fire(Event _event)
  64. {
  65. if (this->_hasEventHandler(_event.getId()))
  66. {
  67. this->eventHandlers.at(_event.getId())->tell(_event);
  68. }
  69. else
  70. {
  71. throw EventNotHandledException{"id: " + _event.getId()};
  72. }
  73. }
  74. bool EventManager::hasEventHandler(const event_id &_id)
  75. {
  76. EmptyStringArgumentEvaluator{_id, "event id is empty and can not be passed!"}.evaluate();
  77. return this->_hasEventHandler(_id);
  78. }
  79. bool EventManager::removeEventHandler(const shared_ptr<EventHandler> &_eventHandler)
  80. {
  81. NullPointerArgumentEvaluator{_eventHandler, "event handler reference for remove attempt is null!"}.evaluate();
  82. return this->_removeEventHandler(_eventHandler);
  83. }
  84. bool EventManager::_hasEventHandler(const event_id &_id)
  85. {
  86. return this->eventHandlers.find(_id) != this->eventHandlers.end();
  87. }
  88. bool EventManager::_removeEventHandler(const shared_ptr<EventHandler> &_eventHandler)
  89. {
  90. return this->eventHandlers.erase(_eventHandler->getId()) == 1;
  91. }