EventManager.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <ls_std/event/EventManager.hpp>
  10. ls_std::EventManager::EventManager() : ls_std::Class("EventManager")
  11. {}
  12. void ls_std::EventManager::subscribe(const ls_std::event_id &_id, const std::shared_ptr<IListener> &_listener)
  13. {
  14. if (this->_hasEventHandler(_id))
  15. {
  16. this->eventHandlers.at(_id)->addListener(_listener);
  17. }
  18. }
  19. void ls_std::EventManager::unsubscribe(const ls_std::event_id &_id, const std::shared_ptr<IListener> &_listener)
  20. {
  21. if (this->_hasEventHandler(_id))
  22. {
  23. this->eventHandlers.at(_id)->removeListener(_listener);
  24. }
  25. }
  26. void ls_std::EventManager::addEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler)
  27. {
  28. if (!this->_hasEventHandler(_eventHandler->getId()))
  29. {
  30. std::pair<ls_std::event_id, std::shared_ptr<ls_std::EventHandler>> element = std::make_pair(_eventHandler->getId(), _eventHandler);
  31. this->eventHandlers.insert(element);
  32. }
  33. }
  34. void ls_std::EventManager::fire(ls_std::Event _event)
  35. {
  36. if (this->_hasEventHandler(_event.getId()))
  37. {
  38. this->eventHandlers.at(_event.getId())->tell(_event);
  39. }
  40. }
  41. void ls_std::EventManager::removeEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler)
  42. {
  43. this->_removeEventHandler(_eventHandler);
  44. }
  45. bool ls_std::EventManager::_hasEventHandler(const ls_std::event_id &_id)
  46. {
  47. return this->eventHandlers.find(_id) != this->eventHandlers.end();
  48. }
  49. void ls_std::EventManager::_removeEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler)
  50. {
  51. this->eventHandlers.erase(_eventHandler->getId());
  52. }