8#include <unordered_map>
45 template<
typename Config = EventSystemConfig::Full>
48 template<
typename Event>
50 return entt::type_hash<Event>::value();
55 virtual void handle(
const void* event) = 0;
58 template<
typename Event>
60 std::function<void(
const Event&)>
handler;
64 void handle(
const void* event)
override {
65 handler(*
static_cast<const Event*
>(event));
70 std::unique_ptr<void, void(*)(
void*)>
data;
73 template<
typename Event>
75 :
data(new Event(
std::forward<Event>(event)), [](void* ptr) {
delete static_cast<Event*
>(ptr); })
77 auto* evt =
static_cast<const Event*
>(
data.get());
87 template<
typename Event>
98 std::conditional_t<Config::EventAsData,
99 std::unordered_map<entt::id_type, std::unordered_map<SubscribeId, std::unique_ptr<IEventHandler>>>,
100 std::unordered_map<entt::id_type, std::unordered_map<SubscribeId, std::unique_ptr<IEventIdentifierHandler>>>>
m_handlers;
102 std::conditional_t<Config::EnableQueue,
103 std::unordered_map<entt::id_type, std::vector<std::function<void()>>>,
109 template<
typename Event>
111 requires (Config::EnableTrigger && Config::EventAsData)
117 for (
const auto& [
id, handler] : it->second) {
118 handler->handle(&event);
123 template<
typename Event>
125 requires (Config::EnableTrigger && !Config::EventAsData)
131 for (
const auto& [
id, handler] : it->second) {
137 template<
typename Event>
139 requires (Config::EnableQueue && Config::EventAsData)
143 auto triggerLambda = [
this, evt = std::forward<Event>(event)]()
mutable {
147 m_eventQueue[eventTypeId].emplace_back(std::move(triggerLambda));
150 template<
typename Event>
152 requires (Config::EnableQueue && !Config::EventAsData)
156 auto triggerLambda = [
this]() {
160 m_eventQueue[eventTypeId].emplace_back(std::move(triggerLambda));
164 template<
typename Event>
166 requires Config::EnableQueue
172 auto& queue = it->second;
173 for (
auto& triggerFunc : queue) {
181 requires Config::EnableQueue
184 for (
auto& triggerFunc : queue) {
191 template<
typename Event,
typename Func>
193 requires Config::EventAsData
199 auto handler = std::make_unique<EventHandler<Event>>(
200 std::function<void(const Event&)>(std::forward<Func>(func))
203 m_handlers[eventTypeId][subscribeId] = std::move(handler);
207 template<
typename Event,
typename Func>
209 requires (!Config::EventAsData)
215 auto handler = std::make_unique<EventIdentifierHandler<Event>>(
216 std::function<void()>(std::forward<Func>(func))
219 m_handlers[eventTypeId][subscribeId] = std::move(handler);
225 template<
typename Event>
231 it->second.erase(subscribeId);
232 if (it->second.empty()) {
238 template<
typename Event>
246 auto it = handlers.find(subscribeId);
247 if (it != handlers.end()) {
249 if (handlers.empty()) {
257 template<
typename Config = EventSystemConfig::Full>
void enqueue(Event &&event)
void unsubscribe(SubscribeId subscribeId)
void trigger(const Event &event)
SubscribeId m_nextSubscribeId
SubscribeId subscribe(Func &&func)
SubscribeId subscribe(Func &&func)
static constexpr auto getEventTypeId()
std::conditional_t< Config::EnableQueue, std::unordered_map< entt::id_type, std::vector< std::function< void()> > >, std::monostate > m_eventQueue
std::conditional_t< Config::EventAsData, std::unordered_map< entt::id_type, std::unordered_map< SubscribeId, std::unique_ptr< IEventHandler > > >, std::unordered_map< entt::id_type, std::unordered_map< SubscribeId, std::unique_ptr< IEventIdentifierHandler > > > > m_handlers
void unsubscribe(SubscribeId subscribeId)
IEventSystem< Config > EventDispatcher
static constexpr bool EnableQueue
static constexpr bool EventAsData
static constexpr bool EnableTrigger
static constexpr bool EventAsData
static constexpr bool EnableTrigger
static constexpr bool EnableQueue
static constexpr bool EnableTrigger
static constexpr bool EnableQueue
static constexpr bool EventAsData
static constexpr bool EnableQueue
static constexpr bool EnableTrigger
static constexpr bool EventAsData
static constexpr bool EnableTrigger
static constexpr bool EnableQueue
static constexpr bool EventAsData
std::function< void(const Event &)> handler
EventHandler(std::function< void(const Event &)> h)
void handle(const void *event) override
std::function< void()> handler
EventIdentifierHandler(std::function< void()> h)
virtual ~IEventHandler()=default
virtual void handle(const void *event)=0
virtual ~IEventIdentifierHandler()=default
QueuedEvent(Event &&event)
std::unique_ptr< void, void(*)(void *)> data
std::function< void(const void *)> trigger_func