1
0
mirror of synced 2025-01-11 22:02:17 +01:00
ImHex/lib/libimhex/source/api/event_manager.cpp
2024-12-14 19:15:49 +01:00

50 lines
1.3 KiB
C++

#include <hex/api/event_manager.hpp>
namespace hex {
std::multimap<void *, EventManager::EventList::iterator>& EventManager::getTokenStore() {
static std::multimap<void *, EventManager::EventList::iterator> tokenStore;
return tokenStore;
}
EventManager::EventList& EventManager::getEvents() {
static EventManager::EventList events;
return events;
}
std::recursive_mutex& EventManager::getEventMutex() {
static std::recursive_mutex mutex;
return mutex;
}
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);
}
}
}