EventManager.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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() || _listener == nullptr)
  19. {
  20. throw ls::std::core::IllegalArgumentException{};
  21. }
  22. if (this->_hasEventHandler(_id))
  23. {
  24. this->eventHandlers.at(_id)->addListener(_listener);
  25. }
  26. else
  27. {
  28. throw ls::std::core::EventNotSubscribedException{"id: " + _id};
  29. }
  30. }
  31. 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)
  32. {
  33. if (_id.empty() || _listener == nullptr)
  34. {
  35. throw ls::std::core::IllegalArgumentException{};
  36. }
  37. if (this->_hasEventHandler(_id))
  38. {
  39. this->eventHandlers.at(_id)->removeListener(_listener);
  40. }
  41. }
  42. bool ls::std::event::EventManager::addEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler)
  43. {
  44. bool wasAdded{};
  45. if (_eventHandler == nullptr)
  46. {
  47. throw ls::std::core::IllegalArgumentException{};
  48. }
  49. if (!this->_hasEventHandler(_eventHandler->getId()))
  50. {
  51. ::std::pair<ls::std::core::type::event_id, ::std::shared_ptr<ls::std::event::EventHandler>> element = ::std::make_pair(_eventHandler->getId(), _eventHandler);
  52. wasAdded = this->eventHandlers.insert(element).second;
  53. }
  54. return wasAdded;
  55. }
  56. void ls::std::event::EventManager::fire(ls::std::event::Event _event)
  57. {
  58. if (this->_hasEventHandler(_event.getId()))
  59. {
  60. this->eventHandlers.at(_event.getId())->tell(_event);
  61. }
  62. else
  63. {
  64. throw ls::std::core::EventNotHandledException{"id: " + _event.getId()};
  65. }
  66. }
  67. bool ls::std::event::EventManager::hasEventHandler(const ls::std::core::type::event_id &_id)
  68. {
  69. if (_id.empty())
  70. {
  71. throw ls::std::core::IllegalArgumentException{};
  72. }
  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{};
  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. }