EventManager.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/exception/EventNotHandledException.hpp>
  11. #include <ls-std/core/exception/EventNotSubscribedException.hpp>
  12. #include <ls-std/core/exception/IllegalArgumentException.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. if (_listener == nullptr)
  21. {
  22. throw ls::std::core::IllegalArgumentException{"_listener is null"};
  23. }
  24. if (this->_hasEventHandler(_id))
  25. {
  26. this->eventHandlers.at(_id)->addListener(_listener);
  27. }
  28. else
  29. {
  30. throw ls::std::core::EventNotSubscribedException{"id: " + _id};
  31. }
  32. }
  33. 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)
  34. {
  35. ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be unsubscribed!"}.evaluate();
  36. if (_listener == nullptr)
  37. {
  38. throw ls::std::core::IllegalArgumentException{"_listener is null"};
  39. }
  40. if (this->_hasEventHandler(_id))
  41. {
  42. this->eventHandlers.at(_id)->removeListener(_listener);
  43. }
  44. }
  45. bool ls::std::event::EventManager::addEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  46. {
  47. bool wasAdded{};
  48. if (_eventHandler == nullptr)
  49. {
  50. throw ls::std::core::IllegalArgumentException{"_eventHandler is null"};
  51. }
  52. if (!this->_hasEventHandler(_eventHandler->getId()))
  53. {
  54. ::std::pair<ls::std::core::type::event_id, ::std::shared_ptr<ls::std::event::EventHandler>> element = ::std::make_pair(_eventHandler->getId(), _eventHandler);
  55. wasAdded = this->eventHandlers.insert(element).second;
  56. }
  57. return wasAdded;
  58. }
  59. void ls::std::event::EventManager::fire(ls::std::event::Event _event)
  60. {
  61. if (this->_hasEventHandler(_event.getId()))
  62. {
  63. this->eventHandlers.at(_event.getId())->tell(_event);
  64. }
  65. else
  66. {
  67. throw ls::std::core::EventNotHandledException{"id: " + _event.getId()};
  68. }
  69. }
  70. bool ls::std::event::EventManager::hasEventHandler(const ls::std::core::type::event_id &_id)
  71. {
  72. ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be passed!"}.evaluate();
  73. return this->_hasEventHandler(_id);
  74. }
  75. bool ls::std::event::EventManager::removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  76. {
  77. if (_eventHandler == nullptr)
  78. {
  79. throw ls::std::core::IllegalArgumentException{"_eventHandler is null"};
  80. }
  81. return this->_removeEventHandler(_eventHandler);
  82. }
  83. bool ls::std::event::EventManager::_hasEventHandler(const ls::std::core::type::event_id &_id)
  84. {
  85. return this->eventHandlers.find(_id) != this->eventHandlers.end();
  86. }
  87. bool ls::std::event::EventManager::_removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  88. {
  89. return this->eventHandlers.erase(_eventHandler->getId()) == 1;
  90. }