EventManager.cpp 2.6 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: 2021-05-27
  7. *
  8. * */
  9. #include <ls_std/event/EventManager.hpp>
  10. #include <ls_std/exception/EventNotSubscribedException.hpp>
  11. #include <ls_std/exception/EventNotHandledException.hpp>
  12. #include <ls_std/exception/IllegalArgumentException.hpp>
  13. ls_std::EventManager::EventManager() : ls_std::Class("EventManager")
  14. {}
  15. void ls_std::EventManager::subscribe(const ls_std::event_id &_id, const std::shared_ptr<ls_std::IListener> &_listener)
  16. {
  17. if (_id.empty() || _listener == nullptr)
  18. {
  19. throw ls_std::IllegalArgumentException{};
  20. }
  21. if (this->_hasEventHandler(_id))
  22. {
  23. this->eventHandlers.at(_id)->addListener(_listener);
  24. }
  25. else
  26. {
  27. throw ls_std::EventNotSubscribedException{};
  28. }
  29. }
  30. void ls_std::EventManager::unsubscribe(const ls_std::event_id &_id, const std::shared_ptr<ls_std::IListener> &_listener)
  31. {
  32. if (_id.empty() || _listener == nullptr)
  33. {
  34. throw ls_std::IllegalArgumentException{};
  35. }
  36. if (this->_hasEventHandler(_id))
  37. {
  38. this->eventHandlers.at(_id)->removeListener(_listener);
  39. }
  40. }
  41. bool ls_std::EventManager::addEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler)
  42. {
  43. bool wasAdded{};
  44. if (_eventHandler == nullptr)
  45. {
  46. throw ls_std::IllegalArgumentException{};
  47. }
  48. if (!this->_hasEventHandler(_eventHandler->getId()))
  49. {
  50. std::pair<ls_std::event_id, std::shared_ptr<ls_std::EventHandler>> element = std::make_pair(_eventHandler->getId(), _eventHandler);
  51. wasAdded = this->eventHandlers.insert(element).second;
  52. }
  53. return wasAdded;
  54. }
  55. void ls_std::EventManager::fire(ls_std::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::EventNotHandledException{};
  64. }
  65. }
  66. bool ls_std::EventManager::hasEventHandler(const ls_std::event_id &_id)
  67. {
  68. if (_id.empty())
  69. {
  70. throw ls_std::IllegalArgumentException{};
  71. }
  72. return this->_hasEventHandler(_id);
  73. }
  74. bool ls_std::EventManager::removeEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler)
  75. {
  76. if (_eventHandler == nullptr)
  77. {
  78. throw ls_std::IllegalArgumentException{};
  79. }
  80. return this->_removeEventHandler(_eventHandler);
  81. }
  82. bool ls_std::EventManager::_hasEventHandler(const ls_std::event_id &_id)
  83. {
  84. return this->eventHandlers.find(_id) != this->eventHandlers.end();
  85. }
  86. bool ls_std::EventManager::_removeEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler)
  87. {
  88. return this->eventHandlers.erase(_eventHandler->getId()) == 1;
  89. }