EventManager.cpp 2.9 KB

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