Narrator.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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-23
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/event/Narrator.hpp>
  12. using ls::std::core::Class;
  13. using ls::std::core::NullPointerArgumentEvaluator;
  14. using ls::std::core::interface_type::IListener;
  15. using ls::std::event::Narrator;
  16. using std::find;
  17. using std::list;
  18. using std::shared_ptr;
  19. Narrator::Narrator() : Class("Narrator")
  20. {}
  21. Narrator::~Narrator() noexcept = default;
  22. bool Narrator::addListener(const shared_ptr<IListener> &_listener)
  23. {
  24. bool wasAdded{};
  25. NullPointerArgumentEvaluator{_listener, "listener reference for add attempt is null!"}.evaluate();
  26. if (!this->_hasListener(_listener))
  27. {
  28. this->listeners.push_back(_listener);
  29. wasAdded = true;
  30. }
  31. return wasAdded;
  32. }
  33. void Narrator::clear()
  34. {
  35. this->listeners.clear();
  36. }
  37. list<shared_ptr<IListener>> Narrator::getListeners()
  38. {
  39. return this->listeners;
  40. }
  41. bool Narrator::removeListener(const shared_ptr<IListener> &_listener)
  42. {
  43. bool wasRemoved{};
  44. NullPointerArgumentEvaluator{_listener, "listener reference for remove attempt is null!"}.evaluate();
  45. if (this->_hasListener(_listener))
  46. {
  47. this->listeners.remove(_listener);
  48. wasRemoved = true;
  49. }
  50. return wasRemoved;
  51. }
  52. void Narrator::tell(const Class &_info)
  53. {
  54. for (const auto &listener : this->listeners)
  55. {
  56. listener->listen(_info);
  57. }
  58. }
  59. bool Narrator::_hasListener(const shared_ptr<IListener> &_listener)
  60. {
  61. return find(this->listeners.begin(), this->listeners.end(), _listener) != this->listeners.end();
  62. }