Narrator.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2021-05-27
  7. *
  8. * */
  9. #include <ls_std/utils/STLUtils.hpp>
  10. #include <ls_std/logic/Narrator.hpp>
  11. #include <ls_std/exception/IllegalArgumentException.hpp>
  12. ls_std::Narrator::Narrator() : ls_std::Class("Narrator")
  13. {}
  14. bool ls_std::Narrator::addListener(const std::shared_ptr<ls_std::IListener> &_listener)
  15. {
  16. bool wasAdded{};
  17. if (_listener == nullptr)
  18. {
  19. throw ls_std::IllegalArgumentException{};
  20. }
  21. else
  22. {
  23. if (!ls_std::STLUtils::contains(this->listeners, _listener))
  24. {
  25. this->listeners.push_back(_listener);
  26. wasAdded = true;
  27. }
  28. }
  29. return wasAdded;
  30. }
  31. void ls_std::Narrator::clear()
  32. {
  33. this->listeners.clear();
  34. }
  35. std::list<std::shared_ptr<ls_std::IListener>> ls_std::Narrator::getListeners()
  36. {
  37. return this->listeners;
  38. }
  39. bool ls_std::Narrator::removeListener(const std::shared_ptr<ls_std::IListener> &_listener)
  40. {
  41. bool wasRemoved{};
  42. if (_listener == nullptr)
  43. {
  44. throw ls_std::IllegalArgumentException{};
  45. }
  46. else
  47. {
  48. if (ls_std::STLUtils::contains(this->listeners, _listener))
  49. {
  50. this->listeners.remove(_listener);
  51. wasRemoved = true;
  52. }
  53. }
  54. return wasRemoved;
  55. }
  56. void ls_std::Narrator::tell(const ls_std::Class &_info)
  57. {
  58. for (const auto &listener : this->listeners)
  59. {
  60. listener->listen(_info);
  61. }
  62. }