EventManager.cpp 1.7 KB

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