EventManager.hpp 2.4 KB

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