Fixed OS EventHandler compilation issue

This commit is contained in:
Ryzerth
2021-05-03 02:08:49 +02:00
parent 6e4f502454
commit c6c15a446b
17 changed files with 121 additions and 63 deletions

44
core/src/utils/event.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <vector>
#include <spdlog/spdlog.h>
template <class T>
struct EventHandler {
EventHandler() {}
EventHandler(void (*handler)(T, void*), void* ctx) {
this->handler = handler;
this->ctx = ctx;
}
void (*handler)(T, void*);
void* ctx;
};
template <class T>
class Event {
public:
Event() {}
~Event() {}
void emit(T value) {
for (auto const& handler : handlers) {
handler.handler(value, handler.ctx);
}
}
void bindHandler(const EventHandler<T>& handler) {
handlers.push_back(handler);
}
void unbindHandler(const EventHandler<T>& handler) {
if (handlers.find(handler) == handlers.end()) {
spdlog::error("Tried to remove a non-existant event handler");
return;
}
handlers.erase(std::remove(handlers.begin(), handlers.end(), handler), handlers.end());
}
private:
std::vector<EventHandler<T>> handlers;
};