#pragma once #include #include #include #include typedef int HandlerID; template class NewEvent { public: using Handler = std::function; HandlerID bind(const Handler& handler) { std::lock_guard lck(mtx); HandlerID id = genID(); handlers[id] = handler; return id; } template HandlerID bind(MHandler handler, T* ctx) { return bind([=](Args... args){ (ctx->*handler)(args...); }); } void unbind(HandlerID id) { std::lock_guard lck(mtx); if (handlers.find(id) == handlers.end()) { throw std::runtime_error("Could not unbind handler, unknown ID"); } handlers.erase(id); } void operator()(Args... args) { std::lock_guard lck(mtx); for (const auto& [desc, handler] : handlers) { handler(args...); } } private: HandlerID genID() { int id; for (id = 1; handlers.find(id) != handlers.end(); id++); return id; } std::map handlers; std::mutex mtx; };