EventManager.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2023-01-06
  7. *
  8. * */
  9. #include <ls_std/event/EventManager.hpp>
  10. #include <ls_std/core/exception/IllegalArgumentException.hpp>
  11. #include <iostream>
  12. #include <list>
  13. ls::std::event::EventManager::EventManager() : ls::std::core::Class("EventManager")
  14. {}
  15. void ls::std::event::EventManager::subscribe(const ls::std::event::type::event_id &_id,
  16. const ::std::function<void(ls::std::event::Event)>& _listener)
  17. {
  18. if (_id.empty() || _listener == nullptr)
  19. {
  20. throw ls::std::core::IllegalArgumentException{};
  21. }
  22. ::std::list<::std::function<void(Event)>> listeners = this->eventHandler.at(_id);
  23. if (!listeners.empty()) //null check needed?
  24. {
  25. listeners.push_back(_listener);
  26. }
  27. }
  28. void ls::std::event::EventManager::unsubscribe(const ls::std::event::type::event_id &_id,
  29. const ::std::function<void(ls::std::event::Event)> &_listener)
  30. {
  31. if (_id.empty() || _listener == nullptr)
  32. {
  33. throw ls::std::core::IllegalArgumentException{};
  34. }
  35. ::std::list<::std::function<void(Event)>> listeners = this->eventHandler.at(_id);
  36. //listeners.remove(_listener);
  37. }
  38. void ls::std::event::EventManager::fire(ls::std::event::Event _event)
  39. {
  40. ::std::list<::std::function<void(Event)>> listeners = this->eventHandler.at(_event.getId());
  41. if (!listeners.empty())
  42. {
  43. for (::std::function<void(Event)> &listener : listeners)
  44. {
  45. listener(_event);
  46. }
  47. }
  48. }