EventManager.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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-08
  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. ls::std::event::EventManager::EventManager() : ls::std::core::Class("EventManager")
  15. {}
  16. ls::std::event::EventManager::~EventManager() = default;
  17. void ls::std::event::EventManager::subscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  18. {
  19. ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be subscribed!"}.evaluate();
  20. ls::std::core::NullPointerArgumentEvaluator{_listener, "listener reference for subscribe attempt is null!"}.evaluate();
  21. if (this->_hasEventHandler(_id))
  22. {
  23. this->eventHandlers.at(_id)->addListener(_listener);
  24. }
  25. else
  26. {
  27. throw ls::std::core::EventNotSubscribedException{"id: " + _id};
  28. }
  29. }
  30. void ls::std::event::EventManager::unsubscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  31. {
  32. ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be unsubscribed!"}.evaluate();
  33. ls::std::core::NullPointerArgumentEvaluator{_listener, "listener reference for unsubscribe attempt is null!"}.evaluate();
  34. if (this->_hasEventHandler(_id))
  35. {
  36. this->eventHandlers.at(_id)->removeListener(_listener);
  37. }
  38. }
  39. bool ls::std::event::EventManager::addEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  40. {
  41. bool wasAdded{};
  42. ls::std::core::NullPointerArgumentEvaluator{_eventHandler, "event handler reference for add attempt is null!"}.evaluate();
  43. if (!this->_hasEventHandler(_eventHandler->getId()))
  44. {
  45. ::std::pair<ls::std::core::type::event_id, ::std::shared_ptr<ls::std::event::EventHandler>> element = ::std::make_pair(_eventHandler->getId(), _eventHandler);
  46. wasAdded = this->eventHandlers.insert(element).second;
  47. }
  48. return wasAdded;
  49. }
  50. void ls::std::event::EventManager::fire(ls::std::event::Event _event)
  51. {
  52. if (this->_hasEventHandler(_event.getId()))
  53. {
  54. this->eventHandlers.at(_event.getId())->tell(_event);
  55. }
  56. else
  57. {
  58. throw ls::std::core::EventNotHandledException{"id: " + _event.getId()};
  59. }
  60. }
  61. bool ls::std::event::EventManager::hasEventHandler(const ls::std::core::type::event_id &_id)
  62. {
  63. ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be passed!"}.evaluate();
  64. return this->_hasEventHandler(_id);
  65. }
  66. bool ls::std::event::EventManager::removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  67. {
  68. ls::std::core::NullPointerArgumentEvaluator{_eventHandler, "event handler reference for remove attempt is null!"}.evaluate();
  69. return this->_removeEventHandler(_eventHandler);
  70. }
  71. bool ls::std::event::EventManager::_hasEventHandler(const ls::std::core::type::event_id &_id)
  72. {
  73. return this->eventHandlers.find(_id) != this->eventHandlers.end();
  74. }
  75. bool ls::std::event::EventManager::_removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  76. {
  77. return this->eventHandlers.erase(_eventHandler->getId()) == 1;
  78. }