33 #include <unordered_set> 38 #include "Utilities/Profiler/Profiler.h" 39 #include "Utilities/Memory/Memory.h" 40 #include "Utilities/String/String.h" 41 #include "Utilities/STL/MxVector.h" 48 #define MAKE_EVENT_BASE(name) struct name { inline virtual uint32_t GetEventType() const = 0; virtual ~name() = default; } 53 #define MAKE_EVENT(class_name) \ 54 template<typename T> friend class EventDispatcher;\ 55 public: inline virtual uint32_t GetEventType() const override { return eventType; } private:\ 56 constexpr static uint32_t eventType = STRING_ID(#class_name) 62 template<
typename EventBase>
65 using CallbackBaseFunction = std::function<void(EventBase&)>;
66 using NamedCallback = std::pair<MxString, CallbackBaseFunction>;
67 using CallbackList = MxVector<NamedCallback>;
68 using EventList = MxVector<UniqueRef<EventBase>>;
69 using EventTypeIndex = uint32_t;
78 std::unordered_map<EventTypeIndex, CallbackList> callbacks;
83 std::unordered_map<EventTypeIndex, CallbackList> toAddCache;
88 MxVector<MxString> toRemoveCache;
94 inline void ProcessEvent(EventBase& event)
96 auto& eventCallbacks = this->callbacks[
event.GetEventType()];
97 for (
const auto& [name, callback] : eventCallbacks)
99 if (std::find(this->toRemoveCache.begin(), this->toRemoveCache.end(), name) == this->toRemoveCache.end())
111 template<
typename EventType>
112 inline void AddCallbackImpl(MxString name, CallbackBaseFunction&& func)
114 this->toAddCache[EventType::eventType].emplace_back(std::move(name), std::move(func));
122 inline void RemoveEventByName(CallbackList& callbacks,
const MxString& name)
124 auto it = std::remove_if(callbacks.begin(), callbacks.end(), [&name](
const auto& p)
126 return p.first == name;
128 callbacks.erase(it, callbacks.end());
136 for (
auto it = this->toRemoveCache.begin(); it != this->toRemoveCache.end(); it++)
138 for (
auto& [event, callbacks] : this->callbacks)
140 RemoveEventByName(callbacks, *it);
143 this->toRemoveCache.clear();
145 for (
auto it = this->toAddCache.begin(); it != this->toAddCache.end(); it++)
147 auto& [event, funcs] = *it;
148 for (
auto& func : funcs)
150 callbacks[event].push_back(std::move(func));
153 for (
auto& list : this->toAddCache) list.second.clear();
162 template<
typename EventType>
165 this->
template AddCallbackImpl<EventType>(name, [func = std::move(func)](EventBase& e)
167 if (e.GetEventType() == EventType::eventType)
168 func(static_cast<EventType&>(e));
178 template<
typename FunctionType>
181 this->
AddEventListener(name, std::function(std::forward<FunctionType>(func)));
190 this->toRemoveCache.push_back(name);
192 for (
auto& [event, callbacks] : this->toAddCache)
194 RemoveEventByName(callbacks, name);
202 template<
typename Event>
206 this->ProcessEvent(event);
215 this->events.push_back(std::move(event));
225 for (
size_t i = 0; i < this->events.size(); i++)
227 MAKE_SCOPE_PROFILER(
typeid(*this->events[i]).name());
228 this->ProcessEvent(*this->events[i]);
230 this->events.clear();
void FlushEvents()
Definition: EventDispatcher.h:134
void AddEventListener(const MxString &name, std::function< void(EventType &)> func)
Definition: EventDispatcher.h:163
void AddEventListener(const MxString &name, FunctionType &&func)
Definition: EventDispatcher.h:179
void InvokeAll()
Definition: EventDispatcher.h:221
void Invoke(Event &event)
Definition: EventDispatcher.h:203
void AddEvent(UniqueRef< EventBase > event)
Definition: EventDispatcher.h:213
Definition: EventDispatcher.h:63
Definition: Application.cpp:49
void RemoveEventListener(const MxString &name)
Definition: EventDispatcher.h:188