EventManager.cpp 3.3 KB

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