14 template<
typename EventType>
15 using Handler = std::function<void(
const EventType & e)>;
33 template<
typename EventType>
41 m_handlerType(m_handler.target_type().name())
49 if (e.
getType() == EventType::getStaticType())
51 m_handler(
static_cast<const EventType &
>(e));
55 std::string getType()
const override {
return m_handlerType; }
57 Handler<EventType> m_handler;
58 const std::string m_handlerType;
70 template<
typename EventType>
73 std::unique_lock lock(m_mutex);
75 auto wrapper = std::make_unique<HandlerWrapper<EventType>>(handler);
76 m_subscribers[EventType::getStaticType()].push_back(std::move(wrapper));
79 template<
typename EventType>
82 std::unique_lock lock(m_mutex);
84 const Type type = EventType::getStaticType();
85 const auto it = m_subscribers.find(type);
86 if (it != m_subscribers.end())
88 auto & subscribers = it->second;
89 const auto it = std::find_if(subscribers.begin(), subscribers.end(),
90 [&handler](
const std::unique_ptr<HandlerWrapperInterface> & wrapper)
92 return wrapper->getType() == handler.target_type().name();
95 if (it != subscribers.end())
97 subscribers.erase(it);
104 std::shared_lock lock(m_mutex);
107 const auto it = m_subscribers.find(type);
108 if (it != m_subscribers.end())
110 for (
auto & subscriber : it->second)
119 std::unordered_map<Type, std::vector<std::unique_ptr<HandlerWrapperInterface>>> m_subscribers;
120 std::shared_mutex m_mutex;
Definition: AbstractEvent.hpp:11
virtual Type getType() const noexcept=0
Definition: EventManager.hpp:18
void exec(const AbstractEvent &e)
Definition: EventManager.hpp:22
virtual std::string getType() const =0
Definition: EventManager.hpp:35
HandlerWrapper(const Handler< EventType > &handler)
Definition: EventManager.hpp:39
Definition: EventManager.hpp:63
void triggerEvent(const AbstractEvent &e)
Definition: EventManager.hpp:102
virtual ~Manager()=default
void unsubscribe(const Handler< EventType > &handler)
Definition: EventManager.hpp:80
void subscribe(const Handler< EventType > &handler)
Definition: EventManager.hpp:71
Definition: AbstractEvent.cpp:4
std::type_index Type
Definition: AbstractEvent.hpp:8
std::function< void(const EventType &e)> Handler
Definition: EventManager.hpp:15