EventManager.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2024-05-16
  6. * Changed: 2024-05-31
  7. *
  8. * */
  9. #ifndef LS_STD_EVENT_MANAGER_HPP
  10. #define LS_STD_EVENT_MANAGER_HPP
  11. #include "Event.hpp"
  12. #include "EventListener.hpp"
  13. #include "EventParameter.hpp"
  14. #include <list>
  15. #include <ls-std/core/Class.hpp>
  16. #include <ls-std/core/definition.hpp>
  17. #include <ls-std/event/type/EventTypes.hpp>
  18. #include <memory>
  19. #include <unordered_map>
  20. namespace ls::std::event::type
  21. {
  22. using event_listeners = ::std::list<::std::pair<::std::shared_ptr<EventListener>, event_action>>;
  23. using event_observability_inventory = ::std::unordered_map<::std::string, event_listeners>;
  24. }
  25. namespace ls::std::event
  26. {
  27. ls_std_class EventManager final : public core::Class
  28. {
  29. public:
  30. explicit EventManager();
  31. ~EventManager() noexcept override;
  32. [[nodiscard]] type::listener_id getNextProvisionId() const;
  33. [[nodiscard]] bool holdsListenerForEvent(type::listener_id _id, const Event &_event);
  34. void invoke(const Event &_event);
  35. void invoke(const Event &_event, const EventParameter &_parameter);
  36. [[nodiscard]] type::listener_id requestListenerId();
  37. void subscribeListenerForEvent(::std::shared_ptr<EventListener> _listener, const Event &_event, type::event_action _action);
  38. void unsubscribeListenerForEvent(const ::std::shared_ptr<EventListener> &_listener, const Event &_event);
  39. private:
  40. type::event_observability_inventory inventory{};
  41. type::listener_id provisionId = 1;
  42. static void _giveListenersParameter(const type::event_listeners &_listeners, const EventParameter &_parameter);
  43. static void _notifyListeners(const type::event_listeners &_listeners);
  44. [[nodiscard]] bool _observesEvent(const Event &_event) const;
  45. };
  46. }
  47. #endif