From 992f18b94ba7e395369d8dbb7e1d722f0ba6d80b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 14 Dec 2024 19:15:49 +0100 Subject: [PATCH] impr: Optimize build times a bit --- lib/external/pattern_language | 2 +- .../include/hex/api/event_manager.hpp | 26 +++++-------------- .../include/hex/api/localization_manager.hpp | 2 +- lib/libimhex/include/hex/helpers/fmt.hpp | 2 +- lib/libimhex/source/api/event_manager.cpp | 26 +++++++++++++++++++ 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index d387be8ad..1057425e1 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit d387be8ad6b678b516f5cd0b848019a2ff498f1b +Subproject commit 1057425e1b945ab61b45bcfe4a2018ef1b785f75 diff --git a/lib/libimhex/include/hex/api/event_manager.hpp b/lib/libimhex/include/hex/api/event_manager.hpp index bb6474d4e..1fc3c253b 100644 --- a/lib/libimhex/include/hex/api/event_manager.hpp +++ b/lib/libimhex/include/hex/api/event_manager.hpp @@ -130,15 +130,9 @@ namespace hex { static void subscribe(void *token, typename E::Callback function) { std::scoped_lock lock(getEventMutex()); - if (getTokenStore().contains(token)) { - auto&& [begin, end] = getTokenStore().equal_range(token); - const auto eventRegistered = std::any_of(begin, end, [&](auto &item) { - return item.second->first == E::Id; - }); - if (eventRegistered) { - log::fatal("The token '{}' has already registered the same event ('{}')", token, wolv::type::getTypeName()); - return; - } + if (isAlreadyRegistered(token, E::Id)) { + log::fatal("The token '{}' has already registered the same event ('{}')", token, wolv::type::getTypeName()); + return; } getTokenStore().insert({ token, subscribe(function) }); @@ -163,16 +157,7 @@ namespace hex { static void unsubscribe(void *token) noexcept { std::scoped_lock lock(getEventMutex()); - auto &tokenStore = getTokenStore(); - auto iter = std::find_if(tokenStore.begin(), tokenStore.end(), [&](auto &item) { - return item.first == token && item.second->first == E::Id; - }); - - if (iter != tokenStore.end()) { - getEvents().erase(iter->second); - tokenStore.erase(iter); - } - + unsubscribe(token, E::Id); } /** @@ -210,6 +195,9 @@ namespace hex { static std::multimap& getTokenStore(); static EventList& getEvents(); static std::recursive_mutex& getEventMutex(); + + static bool isAlreadyRegistered(void *token, impl::EventId id); + static void unsubscribe(void *token, impl::EventId id); }; /* Default Events */ diff --git a/lib/libimhex/include/hex/api/localization_manager.hpp b/lib/libimhex/include/hex/api/localization_manager.hpp index eea1c9c2a..448c183f8 100644 --- a/lib/libimhex/include/hex/api/localization_manager.hpp +++ b/lib/libimhex/include/hex/api/localization_manager.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include namespace hex { diff --git a/lib/libimhex/include/hex/helpers/fmt.hpp b/lib/libimhex/include/hex/helpers/fmt.hpp index f78d9340c..314b3561e 100644 --- a/lib/libimhex/include/hex/helpers/fmt.hpp +++ b/lib/libimhex/include/hex/helpers/fmt.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include namespace hex { diff --git a/lib/libimhex/source/api/event_manager.cpp b/lib/libimhex/source/api/event_manager.cpp index aae430ce8..af37f36fc 100644 --- a/lib/libimhex/source/api/event_manager.cpp +++ b/lib/libimhex/source/api/event_manager.cpp @@ -21,4 +21,30 @@ namespace hex { } + bool EventManager::isAlreadyRegistered(void *token, impl::EventId id) { + if (getTokenStore().contains(token)) { + auto&& [begin, end] = getTokenStore().equal_range(token); + + return std::any_of(begin, end, [&](auto &item) { + return item.second->first == id; + }); + } + + return false; + } + + void EventManager::unsubscribe(void *token, impl::EventId id) { + auto &tokenStore = getTokenStore(); + auto iter = std::find_if(tokenStore.begin(), tokenStore.end(), [&](auto &item) { + return item.first == token && item.second->first == id; + }); + + if (iter != tokenStore.end()) { + getEvents().erase(iter->second); + tokenStore.erase(iter); + } + } + + + } \ No newline at end of file