EventManager.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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-17
  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 <list>
  14. #include <ls-std/core/Class.hpp>
  15. #include <ls-std/event/type/EventTypes.hpp>
  16. #include <ls-std/os/dynamic-goal.hpp>
  17. #include <memory>
  18. #include <unordered_map>
  19. namespace ls::std::event::type
  20. {
  21. using event_listeners = ::std::list<::std::pair<::std::shared_ptr<ls::std::event::EventListener>, ls::std::event::type::event_action>>;
  22. using event_observability_inventory = ::std::unordered_map<::std::string, ls::std::event::type::event_listeners>;
  23. }
  24. namespace ls::std::event
  25. {
  26. class LS_STD_DYNAMIC_GOAL EventManager : public ls::std::core::Class
  27. {
  28. public:
  29. explicit EventManager();
  30. ~EventManager() noexcept override;
  31. [[nodiscard]] ls::std::event::type::listener_id getNextProvisionId() const;
  32. [[nodiscard]] bool holdsListenerForEvent(ls::std::event::type::listener_id _id, const ls::std::event::Event &_event);
  33. void invoke(const ls::std::event::Event &_event) const;
  34. [[nodiscard]] ls::std::event::type::listener_id requestListenerId();
  35. void subscribeListenerForEvent(::std::shared_ptr<ls::std::event::EventListener> _listener, const ls::std::event::Event &_event, ls::std::event::type::event_action _action);
  36. void unsubscribeListenerForEvent(const ::std::shared_ptr<ls::std::event::EventListener> &_listener, const ls::std::event::Event &_event);
  37. private:
  38. ls::std::event::type::event_observability_inventory inventory{};
  39. ls::std::event::type::listener_id provisionId = 1;
  40. static void _notifyListeners(const ls::std::event::type::event_listeners &_listeners);
  41. bool _observesEvent(const ls::std::event::Event &_event) const;
  42. };
  43. }
  44. #endif