EventManager.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2024-05-24
  7. *
  8. * */
  9. #ifndef LS_STD_EVENT_MANAGER_HPP
  10. #define LS_STD_EVENT_MANAGER_HPP
  11. #include "EventHandler.hpp"
  12. #include <ls-std/core/Class.hpp>
  13. #include <ls-std/core/interface/IEventSubscriber.hpp>
  14. #include <ls-std/core/type/EventTypes.hpp>
  15. #include <ls-std/os/dynamic-goal.hpp>
  16. #include <map>
  17. #include <memory>
  18. /*
  19. * @class(name: 'EventManager', package: 'event')
  20. * */
  21. namespace ls::std::event
  22. {
  23. class LS_STD_DYNAMIC_GOAL EventManager : public ls::std::core::Class, public ls::std::core::interface_type::IEventSubscriber
  24. {
  25. public:
  26. explicit EventManager();
  27. ~EventManager() noexcept override;
  28. // implementation
  29. void subscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener) override;
  30. void unsubscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener) override;
  31. // additional functionality
  32. bool addEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler); // nodiscard is optional here
  33. void fire(const ls::std::event::Event &_event);
  34. [[nodiscard]] bool hasEventHandler(const ls::std::core::type::event_id &_id);
  35. bool removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler); // nodiscard is optional here
  36. private:
  37. ::std::map<ls::std::core::type::event_id, ::std::shared_ptr<ls::std::event::EventHandler>, ::std::less<>> eventHandlers{};
  38. [[nodiscard]] bool _hasEventHandler(const ls::std::core::type::event_id &_id);
  39. [[nodiscard]] bool _removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler);
  40. };
  41. }
  42. #endif