ImHex/include/event.hpp

47 lines
1.2 KiB
C++
Raw Normal View History

#pragma once
#include <vector>
#include <functional>
namespace hex {
enum class Events {
2020-11-14 23:46:38 +00:00
DataChanged,
2020-11-17 12:58:50 +00:00
PatternChanged,
FileDropped
};
struct EventHandler {
void *sender;
Events eventType;
2020-11-17 12:58:50 +00:00
std::function<void(const void*)> callback;
};
class EventManager {
public:
2020-11-17 12:58:50 +00:00
void post(Events eventType, const void *userData) {
for (auto &handler : this->m_eventHandlers)
if (eventType == handler.eventType)
handler.callback(userData);
}
2020-11-17 12:58:50 +00:00
void subscribe(Events eventType, void *sender, std::function<void(const void*)> callback) {
for (auto &handler : this->m_eventHandlers)
if (eventType == handler.eventType && sender == handler.sender)
return;
this->m_eventHandlers.push_back(EventHandler { sender, eventType, callback });
}
void unsubscribe(Events eventType, void *sender) {
std::erase_if(this->m_eventHandlers, [&eventType, &sender](EventHandler handler) {
return eventType == handler.eventType && sender == handler.sender;
});
}
private:
std::vector<EventHandler> m_eventHandlers;
};
}