fix event
This commit is contained in:
@@ -6,14 +6,14 @@ namespace example
|
|||||||
{
|
{
|
||||||
void ui_layer::on_attach()
|
void ui_layer::on_attach()
|
||||||
{
|
{
|
||||||
m_key = VE::CONNECT(this);
|
VE::CONNECT(this);
|
||||||
|
|
||||||
hack::log()("on_attach");
|
hack::log()("on_attach");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_layer::on_detach()
|
void ui_layer::on_detach()
|
||||||
{
|
{
|
||||||
VE::DISCONNECT(m_key);
|
VE::DISCONNECT(this);
|
||||||
hack::log()("on_detach");
|
hack::log()("on_detach");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ namespace example
|
|||||||
{
|
{
|
||||||
void ui_layer_double::on_attach()
|
void ui_layer_double::on_attach()
|
||||||
{
|
{
|
||||||
m_key = VE::CONNECT(this);
|
VE::CONNECT(this);
|
||||||
|
|
||||||
hack::log()("on_attach");
|
hack::log()("on_attach");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_layer_double::on_detach()
|
void ui_layer_double::on_detach()
|
||||||
{
|
{
|
||||||
VE::DISCONNECT(m_key);
|
VE::DISCONNECT(this);
|
||||||
hack::log()("on_detach");
|
hack::log()("on_detach");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,14 +5,15 @@
|
|||||||
namespace VE
|
namespace VE
|
||||||
{
|
{
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::string CONNECT(T* obj)
|
void CONNECT(T* obj)
|
||||||
{
|
{
|
||||||
return event_manager::instance().connect(obj, &T::on_event);
|
event_manager::instance().connect(obj, &T::on_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void DISCONNECT(std::string key)
|
template <typename T>
|
||||||
|
void DISCONNECT(T* obj)
|
||||||
{
|
{
|
||||||
event_manager::instance().disconnect(key);
|
event_manager::instance().disconnect(obj, &T::on_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TYPE, typename DATA>
|
template <typename TYPE, typename DATA>
|
||||||
|
|||||||
@@ -1,39 +1,37 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <hack/patterns/singleton.hpp>
|
#include <hack/patterns/singleton.hpp>
|
||||||
#include <hack/logger/logger.hpp>
|
#include <hack/logger/logger.hpp>
|
||||||
#include <hack/security/uuid.hpp>
|
#include <hack/security/uuid.hpp>
|
||||||
|
|
||||||
#include "event.hpp"
|
#include "event.hpp"
|
||||||
#include "utils/using.hpp"
|
|
||||||
|
|
||||||
namespace VE
|
namespace VE
|
||||||
{
|
{
|
||||||
struct event_manager : public hack::patterns::singleton<event_manager>
|
struct event_manager : public hack::patterns::singleton<event_manager>
|
||||||
{
|
{
|
||||||
std::map<std::string, event_fn<event>> m_funcs;
|
// std::map<std::string, event_fn<event>> m_funcs;
|
||||||
|
//
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
std::string connect(T* obj, void (T::*method)(event))
|
// std::string connect(T* obj, void (T::*method)(event))
|
||||||
{
|
// {
|
||||||
std::string key = hack::security::generate_uuid();
|
// std::string key = hack::security::generate_uuid();
|
||||||
m_funcs[key] = std::bind(method, obj, std::placeholders::_1);
|
// m_funcs[key] = std::bind(method, obj, std::placeholders::_1);
|
||||||
return key;
|
// return key;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
void disconnect(std::string key)
|
// void disconnect(std::string key)
|
||||||
{
|
// {
|
||||||
auto it = m_funcs.find(key);
|
// auto it = m_funcs.find(key);
|
||||||
m_funcs.erase(it);
|
// m_funcs.erase(it);
|
||||||
hack::log()("delete", m_funcs.size());
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
void emit(event e)
|
void emit(event e)
|
||||||
{
|
{
|
||||||
hack::log()("emit", m_funcs.size(), e.m_id);
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
for(auto [key, f] : m_funcs) m_funcs[key](e);
|
for(auto& f : m_funcs) f.m_func(e);
|
||||||
}
|
}
|
||||||
catch(const std::exception& ex)
|
catch(const std::exception& ex)
|
||||||
{
|
{
|
||||||
@@ -44,8 +42,36 @@ namespace VE
|
|||||||
{
|
{
|
||||||
hack::error()("ooops...");
|
hack::error()("ooops...");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hack::log()("emit end", m_funcs.size(), e.m_id);
|
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),
|
||||||
|
reinterpret_cast<void*>(&method)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include "event/event.hpp"
|
#include "event/event.hpp"
|
||||||
#include "utils/using.hpp"
|
#include "utils/using.hpp"
|
||||||
|
|
||||||
@@ -24,7 +23,6 @@ namespace VE
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
event_fn<event> execute;
|
event_fn<event> execute;
|
||||||
std::string m_key;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user