增加事件委托
This commit is contained in:
parent
968e4cf085
commit
4e064b34e4
|
@ -0,0 +1,58 @@
|
||||||
|
#include "Type.h"
|
||||||
|
#include "DateTime.h"
|
||||||
|
#include "SString.h"
|
||||||
|
|
||||||
|
#include "EventHandler.h"
|
||||||
|
|
||||||
|
/************************************************ EventHandler ************************************************/
|
||||||
|
EventHandler::EventHandler()
|
||||||
|
{
|
||||||
|
Method = nullptr;
|
||||||
|
Target = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
EventHandler& EventHandler::operator=(const Func& func)
|
||||||
|
{
|
||||||
|
Method = (void*)func;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventHandler::Add(Func func)
|
||||||
|
{
|
||||||
|
Method = (void*)func;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventHandler::operator()()
|
||||||
|
{
|
||||||
|
if(!Method) return;
|
||||||
|
|
||||||
|
if(Target)
|
||||||
|
((Action)Method)(Target);
|
||||||
|
else
|
||||||
|
((Func)Method)();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*void EventHandler::Invoke(void* arg)
|
||||||
|
{
|
||||||
|
if(!Method) return;
|
||||||
|
|
||||||
|
auto func = (Func)Method;
|
||||||
|
|
||||||
|
if(Target)
|
||||||
|
func(Target, arg);
|
||||||
|
else
|
||||||
|
((Action)Method)(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventHandler::Invoke(void* arg1, void* arg2)
|
||||||
|
{
|
||||||
|
if(!Method) return;
|
||||||
|
|
||||||
|
auto func = (Func)Method;
|
||||||
|
|
||||||
|
if(Target)
|
||||||
|
func(Target, arg1, arg2);
|
||||||
|
else
|
||||||
|
func(arg1, arg2);
|
||||||
|
}*/
|
|
@ -0,0 +1,86 @@
|
||||||
|
#ifndef __EventHandler_H__
|
||||||
|
#define __EventHandler_H__
|
||||||
|
|
||||||
|
// 没有参数和返回值的委托
|
||||||
|
typedef void (*Func)(void);
|
||||||
|
// 一个参数没有返回值的委托,一般param参数用作目标对象,调用者用静态函数包装成员函数
|
||||||
|
typedef void (*Action)(void* param);
|
||||||
|
|
||||||
|
// 事件处理器
|
||||||
|
class EventHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void* Method; // 函数指针
|
||||||
|
void* Target; // 参数
|
||||||
|
|
||||||
|
EventHandler();
|
||||||
|
|
||||||
|
EventHandler& operator=(const Func& func);
|
||||||
|
template<typename T>
|
||||||
|
EventHandler& operator=(void(T::*func)()) { Method = (void*)&func; return *this; }
|
||||||
|
template<typename T, typename TArg>
|
||||||
|
EventHandler& operator=(void(T::*func)(TArg)) { Method = (void*)&func; return *this; }
|
||||||
|
template<typename T, typename TArg, typename TArg2>
|
||||||
|
EventHandler& operator=(void(T::*func)(TArg, TArg2)) { Method = (void*)&func; return *this; }
|
||||||
|
|
||||||
|
void Add(Func func);
|
||||||
|
template<typename T>
|
||||||
|
void Add(void(T::*func)(), T* target) { Method = (void*)&func; Target = target; }
|
||||||
|
template<typename T, typename TArg>
|
||||||
|
void Add(void(T::*func)(TArg), T* target) { Method = (void*)&func; Target = target; }
|
||||||
|
template<typename T, typename TArg, typename TArg2>
|
||||||
|
void Add(void(T::*func)(TArg, TArg2), T* target) { Method = (void*)&func; Target = target; }
|
||||||
|
|
||||||
|
void operator()();
|
||||||
|
template<typename T>
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
if(Method)
|
||||||
|
{
|
||||||
|
auto obj = (T*)Target;
|
||||||
|
typedef void(T::*TAction)();
|
||||||
|
auto act = *(TAction*)Method;
|
||||||
|
|
||||||
|
(obj->*act)();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T, typename TArg>
|
||||||
|
void operator()(TArg arg)
|
||||||
|
{
|
||||||
|
if(Method)
|
||||||
|
{
|
||||||
|
auto obj = (T*)Target;
|
||||||
|
typedef void(T::*TAction)(TArg);
|
||||||
|
auto act = *(TAction*)Method;
|
||||||
|
|
||||||
|
(obj->*act)(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T, typename TArg, typename TArg2>
|
||||||
|
void operator()(TArg arg, TArg arg2)
|
||||||
|
{
|
||||||
|
if(Method)
|
||||||
|
{
|
||||||
|
auto obj = (T*)Target;
|
||||||
|
typedef void(T::*TAction)(TArg, TArg2);
|
||||||
|
auto act = *(TAction*)Method;
|
||||||
|
|
||||||
|
(obj->*act)(arg, arg2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*void operator+= (const Func& func) { Add(func); }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void operator+= (void(T::*func)()) { Method = (void*)&func; }
|
||||||
|
|
||||||
|
template<typename T, typename TArg>
|
||||||
|
void operator+= (void(T::*func)(TArg)) { Method = (void*)&func; }
|
||||||
|
|
||||||
|
template<typename T, typename TArg, typename TArg2>
|
||||||
|
void operator+= (void(T::*func)(TArg, TArg2)) { Method = (void*)&func; }*/
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue