add new implementation event
This commit is contained in:
@@ -1,80 +1,132 @@
|
||||
#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"
|
||||
#include "utils/using.hpp"
|
||||
|
||||
namespace VE
|
||||
{
|
||||
struct event_manager : public hack::patterns::singleton<event_manager>
|
||||
{
|
||||
template<typename T>
|
||||
std::string connect(T* obj, void (T::*method)(event))
|
||||
{
|
||||
std::string key = hack::security::generate_uuid();
|
||||
m_funcs[key] = std::bind(method, obj, std::placeholders::_1);
|
||||
return key;
|
||||
}
|
||||
|
||||
void disconnect(std::string key)
|
||||
{
|
||||
auto it = m_funcs.find(key);
|
||||
if (it != m_funcs.end())
|
||||
m_funcs.erase(it);
|
||||
hack::log()(m_funcs.size());
|
||||
}
|
||||
|
||||
void emit(event e)
|
||||
{
|
||||
if (m_funcs.size() == 0)
|
||||
{
|
||||
hack::warn()("funs is empty");
|
||||
return;
|
||||
}
|
||||
else if (!e.m_msg.empty())
|
||||
{
|
||||
hack::warn()("EMIT MSG:", e.m_msg, m_funcs.size());
|
||||
}
|
||||
|
||||
std::size_t i = 0;
|
||||
try
|
||||
{
|
||||
for(;i < m_funcs.size();++i) m_funcs[i].m_func(e);
|
||||
for(auto [key, f] : m_funcs) m_funcs[key](e);
|
||||
}
|
||||
catch(std::exception& ext)
|
||||
catch(const std::exception& ex)
|
||||
{
|
||||
hack::error()("call function is error:", ext.what());
|
||||
hack::exception ex;
|
||||
ex.system_error(ext);
|
||||
ex.set_data(e);
|
||||
throw ex;
|
||||
hack::error()(ex.what());
|
||||
hack::log()("size funcs", m_funcs.size());
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
hack::error()("call function is error:", "oopps...");
|
||||
hack::exception ex;
|
||||
ex.set_data(e);
|
||||
throw ex;
|
||||
hack::error()("ooops...");
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
});
|
||||
hack::warn()("connect (size)", m_funcs.size());
|
||||
}
|
||||
|
||||
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()
|
||||
);
|
||||
hack::warn()("disconnect (size)", m_funcs.size());
|
||||
}
|
||||
std::map<std::string, event_fn<event>> m_funcs;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// #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)
|
||||
// {
|
||||
// if (m_funcs.size() == 0)
|
||||
// {
|
||||
// hack::warn()("funs is empty");
|
||||
// return;
|
||||
// }
|
||||
// else if (!e.m_msg.empty())
|
||||
// {
|
||||
// hack::warn()("EMIT MSG:", e.m_msg, m_funcs.size());
|
||||
// }
|
||||
//
|
||||
// std::size_t i = 0;
|
||||
// try
|
||||
// {
|
||||
// for(;i < m_funcs.size();++i) m_funcs[i].m_func(e);
|
||||
// }
|
||||
// catch(std::exception& ext)
|
||||
// {
|
||||
// hack::error()("call function is error:", ext.what());
|
||||
// hack::exception ex;
|
||||
// ex.system_error(ext);
|
||||
// ex.set_data(e);
|
||||
// throw ex;
|
||||
// }
|
||||
// catch(...)
|
||||
// {
|
||||
// hack::error()("call function is error:", "oopps...");
|
||||
// hack::exception ex;
|
||||
// ex.set_data(e);
|
||||
// throw ex;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// 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)
|
||||
// });
|
||||
// hack::warn()("connect (size)", m_funcs.size());
|
||||
// }
|
||||
//
|
||||
// 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()
|
||||
// );
|
||||
// hack::warn()("disconnect (size)", m_funcs.size());
|
||||
// }
|
||||
//
|
||||
// struct EventHandler
|
||||
// {
|
||||
// std::function<void(event)> m_func;
|
||||
// void* m_obj;
|
||||
// };
|
||||
//
|
||||
// std::vector<EventHandler> m_funcs;
|
||||
//
|
||||
// };
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user