This commit is contained in:
2023-11-17 17:28:41 -06:00
parent 41c26dd103
commit 0c46c328fa

View File

@ -7,14 +7,34 @@
#include "dawnlibs.hpp"
namespace Dawn {
template<typename T>
template<typename ...A>
class Event {
private:
int32_t nextId = 0;
std::unordered_map<int32, std::function<void(A...)>> listeners;
public:
Event() {}
virtual ~Event() {}
Event() {
virtual void operator()(T &t) = 0;
}
void emit(A ...args) {
auto copy = listeners;
for(auto &pair : copy) {
pair.second(args...);
}
}
std::function<void()> listen(const std::function<void(A...)> listener) {
int32_t id = nextId++;
listeners[id] = listener;
return [this, id]() {
listeners.erase(id);
};
}
virtual ~Event() {
listeners.clear();
}
}
}