1
0
mirror of synced 2024-11-28 09:30:51 +01:00

impr: Drastically optimize event handler

This commit is contained in:
WerWolv 2024-06-27 17:10:01 +02:00
parent ced3af3935
commit b1b54a5fe7

View File

@ -20,12 +20,12 @@
constexpr static auto ShouldLog = (should_log); \ constexpr static auto ShouldLog = (should_log); \
explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \
\ \
static EventManager::EventList::iterator subscribe(Event::Callback function) { return EventManager::subscribe<event_name>(function); } \ static EventManager::EventList::iterator subscribe(Event::Callback function) { return EventManager::subscribe<event_name>(std::move(function)); } \
static void subscribe(void *token, Event::Callback function) { EventManager::subscribe<event_name>(token, function); } \ static void subscribe(void *token, Event::Callback function) { EventManager::subscribe<event_name>(token, std::move(function)); } \
static void unsubscribe(const EventManager::EventList::iterator &token) noexcept { EventManager::unsubscribe(token); } \ static void unsubscribe(const EventManager::EventList::iterator &token) noexcept { EventManager::unsubscribe(token); } \
static void unsubscribe(void *token) noexcept { EventManager::unsubscribe<event_name>(token); } \ static void unsubscribe(void *token) noexcept { EventManager::unsubscribe<event_name>(token); } \
static void post(auto &&...args) { EventManager::post<event_name>(std::forward<decltype(args)>(args)...); } \ static void post(auto &&...args) { EventManager::post<event_name>(std::forward<decltype(args)>(args)...); } \
}; }
#define EVENT_DEF(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, true, __VA_ARGS__) #define EVENT_DEF(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, true, __VA_ARGS__)
#define EVENT_DEF_NO_LOG(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, false, __VA_ARGS__) #define EVENT_DEF_NO_LOG(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, false, __VA_ARGS__)
@ -58,6 +58,10 @@ namespace hex {
return m_hash == other.m_hash; return m_hash == other.m_hash;
} }
constexpr auto operator<=>(const EventId &other) const {
return m_hash <=> other.m_hash;
}
private: private:
u32 m_hash; u32 m_hash;
}; };
@ -99,7 +103,7 @@ namespace hex {
*/ */
class EventManager { class EventManager {
public: public:
using EventList = std::list<std::pair<impl::EventId, std::unique_ptr<impl::EventBase>>>; using EventList = std::multimap<impl::EventId, std::unique_ptr<impl::EventBase>>;
/** /**
* @brief Subscribes to an event * @brief Subscribes to an event
@ -112,7 +116,7 @@ namespace hex {
std::scoped_lock lock(getEventMutex()); std::scoped_lock lock(getEventMutex());
auto &events = getEvents(); auto &events = getEvents();
return events.insert(events.end(), std::make_pair(E::Id, std::make_unique<E>(function))); return events.insert({ E::Id, std::make_unique<E>(function) });
} }
/** /**
@ -164,7 +168,7 @@ namespace hex {
}); });
if (iter != tokenStore.end()) { if (iter != tokenStore.end()) {
getEvents().remove(*iter->second); getEvents().erase(iter->second);
tokenStore.erase(iter); tokenStore.erase(iter);
} }
@ -179,11 +183,11 @@ namespace hex {
static void post(auto && ...args) { static void post(auto && ...args) {
std::scoped_lock lock(getEventMutex()); std::scoped_lock lock(getEventMutex());
for (const auto &[id, event] : getEvents()) { auto [begin, end] = getEvents().equal_range(E::Id);
if (id == E::Id) { for (auto it = begin; it != end; ++it) {
const auto &[id, event] = *it;
(*static_cast<E *const>(event.get())).template call<E>(std::forward<decltype(args)>(args)...); (*static_cast<E *const>(event.get())).template call<E>(std::forward<decltype(args)>(args)...);
} }
}
#if defined (DEBUG) #if defined (DEBUG)
if constexpr (E::ShouldLog) if constexpr (E::ShouldLog)
@ -276,7 +280,7 @@ namespace hex {
EVENT_DEF_NO_LOG(EventFrameBegin); EVENT_DEF_NO_LOG(EventFrameBegin);
EVENT_DEF_NO_LOG(EventFrameEnd); EVENT_DEF_NO_LOG(EventFrameEnd);
EVENT_DEF_NO_LOG(EventSetTaskBarIconState, u32, u32, u32); EVENT_DEF_NO_LOG(EventSetTaskBarIconState, u32, u32, u32);
EVENT_DEF_NO_LOG(EventImGuiElementRendered, ImGuiID, const std::array<float, 4>&) EVENT_DEF_NO_LOG(EventImGuiElementRendered, ImGuiID, const std::array<float, 4>&);
EVENT_DEF(RequestAddInitTask, std::string, bool, std::function<bool()>); EVENT_DEF(RequestAddInitTask, std::string, bool, std::function<bool()>);
EVENT_DEF(RequestAddExitTask, std::string, std::function<bool()>); EVENT_DEF(RequestAddExitTask, std::string, std::function<bool()>);