Narrator.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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-07
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <ls-std/core/exception/IllegalArgumentException.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. if (_listener == nullptr)
  19. {
  20. throw ls::std::core::IllegalArgumentException{"_listener is null"};
  21. }
  22. else
  23. {
  24. if (!this->_hasListener(_listener))
  25. {
  26. this->listeners.push_back(_listener);
  27. wasAdded = true;
  28. }
  29. }
  30. return wasAdded;
  31. }
  32. void ls::std::event::Narrator::clear()
  33. {
  34. this->listeners.clear();
  35. }
  36. ::std::list<::std::shared_ptr<ls::std::core::interface_type::IListener>> ls::std::event::Narrator::getListeners()
  37. {
  38. return this->listeners;
  39. }
  40. bool ls::std::event::Narrator::removeListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  41. {
  42. bool wasRemoved{};
  43. if (_listener == nullptr)
  44. {
  45. throw ls::std::core::IllegalArgumentException{"_listener is null"};
  46. }
  47. else
  48. {
  49. if (this->_hasListener(_listener))
  50. {
  51. this->listeners.remove(_listener);
  52. wasRemoved = true;
  53. }
  54. }
  55. return wasRemoved;
  56. }
  57. void ls::std::event::Narrator::tell(const ls::std::core::Class &_info)
  58. {
  59. for (const auto &listener : this->listeners)
  60. {
  61. listener->listen(_info);
  62. }
  63. }
  64. bool ls::std::event::Narrator::_hasListener(const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
  65. {
  66. return ::std::find(this->listeners.begin(), this->listeners.end(), _listener) != this->listeners.end();
  67. }