EventManager.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2023-02-08
  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. namespace ls::std::event
  19. {
  20. class LS_STD_DYNAMIC_GOAL EventManager : public ls::std::core::Class, public ls::std::core::interface_type::IEventSubscriber
  21. {
  22. public:
  23. explicit EventManager();
  24. ~EventManager() override;
  25. // implementation
  26. void subscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener) override;
  27. void unsubscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener) override;
  28. // additional functionality
  29. bool addEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler); // nodiscard is optional here
  30. void fire(ls::std::event::Event _event);
  31. [[nodiscard]] bool hasEventHandler(const ls::std::core::type::event_id &_id);
  32. bool removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler); // nodiscard is optional here
  33. private:
  34. ::std::map<ls::std::core::type::event_id, ::std::shared_ptr<ls::std::event::EventHandler>> eventHandlers{};
  35. [[nodiscard]] bool _hasEventHandler(const ls::std::core::type::event_id &_id);
  36. [[nodiscard]] bool _removeEventHandler(const ::std::shared_ptr<ls::std::event::EventHandler> &_eventHandler);
  37. };
  38. }
  39. #endif