#pragma once #include #include #include #include #include "event.hpp" namespace VE { struct event_manager : public hack::patterns::singleton { void emit(event e) { try { for(auto& f : m_funcs) f.func(e); } catch(const std::exception& ex) { hack::error()(ex.what()); hack::log()("size funcs", m_funcs.size()); } catch(...) { hack::error()("ooops..."); } } struct EventHandler { std::function func; void* obj; }; std::vector m_funcs; template void connect(T* obj, void (T::*method)(event)) { m_funcs.push_back({ std::bind(method, obj, std::placeholders::_1), static_cast(obj) }); } template void disconnect(T* obj, void (T::*method)(event)) { m_funcs.erase( std::remove_if(m_funcs.begin(), m_funcs.end(), [obj](const auto& handler) { return handler.obj == obj; }), m_funcs.end() ); } }; }