1
0
mirror of synced 2024-11-15 03:27:40 +01:00
ImHex/plugins/libimhex/source/api/event.cpp

30 lines
1.0 KiB
C++
Raw Normal View History

#include <hex/api/event.hpp>
#include <hex/helpers/shared_data.hpp>
namespace hex {
2021-01-21 10:53:12 +01:00
std::vector<std::any> EventManager::post(Events eventType, const std::any &userData) {
std::vector<std::any> results;
for (auto &handler : SharedData::eventHandlers)
if (eventType == handler.eventType)
2021-01-21 10:53:12 +01:00
results.push_back(handler.callback(userData));
return results;
}
2021-01-21 10:53:12 +01:00
void EventManager::subscribe(Events eventType, void *owner, std::function<std::any(const std::any&)> callback) {
for (auto &handler : SharedData::eventHandlers)
if (eventType == handler.eventType && owner == handler.owner)
return;
SharedData::eventHandlers.push_back(EventHandler { owner, eventType, callback });
}
void EventManager::unsubscribe(Events eventType, void *sender) {
std::erase_if(SharedData::eventHandlers, [&eventType, &sender](EventHandler handler) {
return eventType == handler.eventType && sender == handler.owner;
});
}
}