From 4e064b34e4e0e3fc386355bca381b7ee3290d664 Mon Sep 17 00:00:00 2001 From: Stone Date: Fri, 17 Jun 2016 11:36:51 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=8B=E4=BB=B6=E5=A7=94?= =?UTF-8?q?=E6=89=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/EventHandler.cpp | 58 +++++++++++++++++++++++++++++ Core/EventHandler.h | 86 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 Core/EventHandler.cpp create mode 100644 Core/EventHandler.h diff --git a/Core/EventHandler.cpp b/Core/EventHandler.cpp new file mode 100644 index 00000000..903f143b --- /dev/null +++ b/Core/EventHandler.cpp @@ -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); +}*/ diff --git a/Core/EventHandler.h b/Core/EventHandler.h new file mode 100644 index 00000000..07ad6929 --- /dev/null +++ b/Core/EventHandler.h @@ -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 + EventHandler& operator=(void(T::*func)()) { Method = (void*)&func; return *this; } + template + EventHandler& operator=(void(T::*func)(TArg)) { Method = (void*)&func; return *this; } + template + EventHandler& operator=(void(T::*func)(TArg, TArg2)) { Method = (void*)&func; return *this; } + + void Add(Func func); + template + void Add(void(T::*func)(), T* target) { Method = (void*)&func; Target = target; } + template + void Add(void(T::*func)(TArg), T* target) { Method = (void*)&func; Target = target; } + template + void Add(void(T::*func)(TArg, TArg2), T* target) { Method = (void*)&func; Target = target; } + + void operator()(); + template + void operator()() + { + if(Method) + { + auto obj = (T*)Target; + typedef void(T::*TAction)(); + auto act = *(TAction*)Method; + + (obj->*act)(); + } + } + template + void operator()(TArg arg) + { + if(Method) + { + auto obj = (T*)Target; + typedef void(T::*TAction)(TArg); + auto act = *(TAction*)Method; + + (obj->*act)(arg); + } + } + template + 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 + void operator+= (void(T::*func)()) { Method = (void*)&func; } + + template + void operator+= (void(T::*func)(TArg)) { Method = (void*)&func; } + + template + void operator+= (void(T::*func)(TArg, TArg2)) { Method = (void*)&func; }*/ + +private: +}; + +#endif