Narrator.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2023-02-08
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/event/Narrator.hpp>
  12. ls::std::event::Narrator::Narrator() : ls::std::core::Class("Narrator")
  13. {}
  14. ls::std::event::Narrator::~Narrator() = default;
  15. bool ls::std::event::Narrator::addListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  16. {
  17. bool wasAdded{};
  18. ls::std::core::NullPointerArgumentEvaluator{_listener, "listener reference for add attempt is null!"}.evaluate();
  19. if (!this->_hasListener(_listener))
  20. {
  21. this->listeners.push_back(_listener);
  22. wasAdded = true;
  23. }
  24. return wasAdded;
  25. }
  26. void ls::std::event::Narrator::clear()
  27. {
  28. this->listeners.clear();
  29. }
  30. ::std::list<::std::shared_ptr<ls::std::core::interface_type::IListener>> ls::std::event::Narrator::getListeners()
  31. {
  32. return this->listeners;
  33. }
  34. bool ls::std::event::Narrator::removeListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  35. {
  36. bool wasRemoved{};
  37. ls::std::core::NullPointerArgumentEvaluator{_listener, "listener reference for remove attempt is null!"}.evaluate();
  38. if (this->_hasListener(_listener))
  39. {
  40. this->listeners.remove(_listener);
  41. wasRemoved = true;
  42. }
  43. return wasRemoved;
  44. }
  45. void ls::std::event::Narrator::tell(const ls::std::core::Class &_info)
  46. {
  47. for (const auto &listener : this->listeners)
  48. {
  49. listener->listen(_info);
  50. }
  51. }
  52. bool ls::std::event::Narrator::_hasListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  53. {
  54. return ::std::find(this->listeners.begin(), this->listeners.end(), _listener) != this->listeners.end();
  55. }