EventManager.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #ifndef LS_STD_EVENT_MANAGER_HPP
  10. #define LS_STD_EVENT_MANAGER_HPP
  11. #include <ls_std/base/Class.hpp>
  12. #include <map>
  13. #include <ls_std/event/EventTypes.hpp>
  14. #include <memory>
  15. #include "EventHandler.hpp"
  16. #include "IEventSubscriber.hpp"
  17. namespace ls_std
  18. {
  19. class EventManager : public ls_std::Class, public ls_std::IEventSubscriber
  20. {
  21. public:
  22. explicit EventManager();
  23. ~EventManager() override = default;
  24. // implementation
  25. void subscribe(const ls_std::event_id &_id, const std::shared_ptr<IListener> &_listener) override;
  26. void unsubscribe(const ls_std::event_id &_id, const std::shared_ptr<IListener> &_listener) override;
  27. // additional functionality
  28. void addEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler);
  29. void fire(ls_std::Event _event);
  30. void removeEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler);
  31. private:
  32. std::map<ls_std::event_id, std::shared_ptr<ls_std::EventHandler>> eventHandlers{};
  33. bool _hasEventHandler(const ls_std::event_id &_id);
  34. void _removeEventHandler(const std::shared_ptr<ls_std::EventHandler> &_eventHandler);
  35. };
  36. }
  37. #endif