Narrator.cpp 1.6 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: 2022-05-19
  7. *
  8. * */
  9. #include <ls_std/core/utils/STLUtils.hpp>
  10. #include <ls_std/event/Narrator.hpp>
  11. #include <ls_std/core/exception/IllegalArgumentException.hpp>
  12. ls::std::event::Narrator::Narrator() : ls::std::core::Class("Narrator")
  13. {}
  14. bool ls::std::event::Narrator::addListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  15. {
  16. bool wasAdded{};
  17. if (_listener == nullptr)
  18. {
  19. throw ls::std::core::IllegalArgumentException{};
  20. }
  21. else
  22. {
  23. if (!ls::std::core::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::event::Narrator::clear()
  32. {
  33. this->listeners.clear();
  34. }
  35. std::list<std::shared_ptr<ls::std::core::interface_type::IListener>> ls::std::event::Narrator::getListeners()
  36. {
  37. return this->listeners;
  38. }
  39. bool ls::std::event::Narrator::removeListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  40. {
  41. bool wasRemoved{};
  42. if (_listener == nullptr)
  43. {
  44. throw ls::std::core::IllegalArgumentException{};
  45. }
  46. else
  47. {
  48. if (ls::std::core::STLUtils::contains(this->listeners, _listener))
  49. {
  50. this->listeners.remove(_listener);
  51. wasRemoved = true;
  52. }
  53. }
  54. return wasRemoved;
  55. }
  56. void ls::std::event::Narrator::tell(const ls::std::core::Class &_info)
  57. {
  58. for (const auto &listener : this->listeners)
  59. {
  60. listener->listen(_info);
  61. }
  62. }