61 lines
1.2 KiB
C++
Executable File
61 lines
1.2 KiB
C++
Executable File
#pragma once
|
|
|
|
#include <functional>
|
|
#include <hack/patterns/singleton.hpp>
|
|
#include <hack/logger/logger.hpp>
|
|
#include <hack/security/uuid.hpp>
|
|
#include <hack/exception/exception.hpp>
|
|
|
|
#include "event.hpp"
|
|
|
|
namespace VE
|
|
{
|
|
struct event_manager : public hack::patterns::singleton<event_manager>
|
|
{
|
|
void emit(event e)
|
|
{
|
|
std::size_t i = 0;
|
|
try
|
|
{
|
|
for(;i < m_funcs.size();++i) m_funcs[i].m_func(e);
|
|
}
|
|
catch(...)
|
|
{
|
|
hack::error()("call function is error...");
|
|
hack::exception ex;
|
|
ex.set_data(e);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
struct EventHandler
|
|
{
|
|
std::function<void(event)> m_func;
|
|
void* m_obj;
|
|
};
|
|
|
|
std::vector<EventHandler> m_funcs;
|
|
|
|
template<typename T>
|
|
void connect(T* obj, void (T::*method)(event))
|
|
{
|
|
m_funcs.push_back({
|
|
std::bind(method, obj, std::placeholders::_1),
|
|
static_cast<void*>(obj)
|
|
});
|
|
}
|
|
|
|
template<typename T>
|
|
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.m_obj == obj;
|
|
}),
|
|
m_funcs.end()
|
|
);
|
|
}
|
|
};
|
|
}
|