EventManager.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2024-05-16
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #ifndef LS_STD_EVENT_MANAGER_HPP
  11. #define LS_STD_EVENT_MANAGER_HPP
  12. #include "Event.hpp"
  13. #include "EventListener.hpp"
  14. #include "EventParameter.hpp"
  15. #include <list>
  16. #include <ls-std/core/Class.hpp>
  17. #include <ls-std/event/type/EventTypes.hpp>
  18. #include <ls-std/os/dynamic-goal.hpp>
  19. #include <memory>
  20. #include <unordered_map>
  21. namespace ls::standard::event::type
  22. {
  23. using event_listeners = ::std::list<::std::pair<::std::shared_ptr<ls::standard::event::EventListener>, ls::standard::event::type::event_action>>;
  24. using event_observability_inventory = ::std::unordered_map<::std::string, ls::standard::event::type::event_listeners>;
  25. }
  26. /*
  27. * @doc: class(name: 'EventManager', package: 'event')
  28. * @doc: event.EventManager.description('This class can invoke all events known by subscribed listeners.')
  29. * */
  30. namespace ls::standard::event
  31. {
  32. class LS_STD_DYNAMIC_GOAL EventManager : public ls::standard::core::Class
  33. {
  34. public:
  35. explicit EventManager();
  36. ~EventManager() noexcept override;
  37. [[nodiscard]] ls::standard::event::type::listener_id getNextProvisionId() const;
  38. [[nodiscard]] bool holdsListenerForEvent(ls::standard::event::type::listener_id _id, const ls::standard::event::Event &_event);
  39. void invoke(const ls::standard::event::Event &_event);
  40. void invoke(const ls::standard::event::Event &_event, const ls::standard::event::EventParameter &_parameter);
  41. [[nodiscard]] ls::standard::event::type::listener_id requestListenerId();
  42. void subscribeListenerForEvent(::std::shared_ptr<ls::standard::event::EventListener> _listener, const ls::standard::event::Event &_event, ls::standard::event::type::event_action _action);
  43. void unsubscribeListenerForEvent(const ::std::shared_ptr<ls::standard::event::EventListener> &_listener, const ls::standard::event::Event &_event);
  44. private:
  45. ls::standard::event::type::event_observability_inventory inventory{};
  46. ls::standard::event::type::listener_id provisionId = 1;
  47. static void _giveListenersParameter(const ls::standard::event::type::event_listeners &_listeners, const ls::standard::event::EventParameter &_parameter);
  48. static void _notifyListeners(const ls::standard::event::type::event_listeners &_listeners);
  49. bool _observesEvent(const ls::standard::event::Event &_event) const;
  50. };
  51. }
  52. #endif