EventHandler.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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-27
  7. *
  8. * */
  9. #include <ls_std/event/EventHandler.hpp>
  10. ls_std::EventHandler::EventHandler(ls_std::event_id _id) : ls_std::Class("EventHandler"),
  11. id(std::move(_id))
  12. {}
  13. ls_std::event_id ls_std::EventHandler::getId()
  14. {
  15. return this->id;
  16. }
  17. void ls_std::EventHandler::notify(const ls_std::Event &_event)
  18. {
  19. for(const auto& listener : this->listeners) {
  20. listener->listen(_event);
  21. }
  22. }
  23. void ls_std::EventHandler::subscribe(const std::shared_ptr<ls_std::IListener> &_listener)
  24. {
  25. if(!this->_hasListener(_listener)) {
  26. this->_addListener(_listener);
  27. }
  28. }
  29. void ls_std::EventHandler::unsubscribe(const std::shared_ptr<ls_std::IListener> &_listener)
  30. {
  31. this->_removeListener(_listener);
  32. }
  33. void ls_std::EventHandler::_addListener(const std::shared_ptr<ls_std::IListener> &_listener)
  34. {
  35. this->listeners.push_back(_listener);
  36. }
  37. bool ls_std::EventHandler::_hasListener(const std::shared_ptr<ls_std::IListener>& _listener)
  38. {
  39. bool exists {};
  40. for(const auto& listener : this->listeners) {
  41. if(listener == _listener) {
  42. exists = true;
  43. break;
  44. }
  45. }
  46. return exists;
  47. }
  48. void ls_std::EventHandler::_removeListener(const std::shared_ptr<ls_std::IListener> &_listener)
  49. {
  50. this->listeners.remove(_listener);
  51. this->listeners.size();
  52. }