写这个程序前,参考过vczh的VL_Data_Event,也看过何咏的,写的目的:第一主要是想熟练c++的模板;第二是重复发明车轮,也拥有自己的事件类了~~。
上大学开始接触编程,一开始便学习C++,但是教科书对模板的介绍都比较少,当时连比较基础的C++都学得一塌糊涂,更不用说模板了。
现在工作任务完成,有点空闲时间看《C++templates》,看到现在终于能写点像样的C++模板了。
目前事件类的封装还没有对0--5个参数模板的特化,这里只贴出具有6个参数的事件类代码:
1#pragma once
2#include <list>
3using namespace std;
4
5//函数调用类抽象接口
6template<typename param1,typename param2,typename param3,typename param4,typename param5,typename param6>
7class CY_FunctionInterface
8{
9public:
10 virtual void* GetClassHandler()=0;
11 virtual void* GetFunHandler()=0;
12 virtual void Invoke(param1 p1,param2 p2,param3 p3,param4 p4,param5 p5,param6 p6)=0;
13};
14
15
16//全局函数调用类
17template<typename param1,typename param2,typename param3,typename param4,typename param5,typename param6>
18class CY_GolobalFunction:public CY_FunctionInterface<param1,param2,param3,param4,param5,param6>
19{
20public:
21 typedef void(*T_HANDLER)(param1 ,param2 ,param3 ,param4 ,param5 ,param6);
22protected:
23 union
24 {
25 T_HANDLER FuncHandler;
26 void* pFPointer;
27 };
28
29public:
30 CY_GolobalFunction(T_HANDLER f)
31 {
32 FuncHandler=f;
33 }
34 void *GetClassHandler()
35 {
36 return 0;
37 }
38 void *GetFunHandler()
39 {
40 return pFPointer;
41 }
42 void Invoke(param1 p1,param2 p2,param3 p3,param4 p4,param5 p5,param6 p6)
43 {
44 (*FuncHandler)(p1,p2,p3,p4,p5,p6);
45 }
46};
47
48//成员函数调用类
49template<typename Class,typename param1,typename param2,typename param3,typename param4,typename param5,typename param6>
50class CY_MemberFunction:public CY_FunctionInterface<param1,param2,param3,param4,param5,param6>
51{
52public:
53 typedef void(Class::*T_HANDLER)(param1,param2,param3,param4,param5,param6);
54protected:
55 union
56 {
57 T_HANDLER FuncHandler;
58 void * pFPointer;
59 };
60 union
61 {
62 Class *OwnClassHandler;
63 void * pCPointer;
64 };
65
66public:
67 CY_MemberFunction(Class *pClass,T_HANDLER f)
68 {
69 OwnClassHandler=pClass;
70 FuncHandler=f;
71 }
72 void *GetClassHandler()
73 {
74 return pCPointer;
75 }
76 void *GetFunHandler()
77 {
78 return pFPointer;
79 }
80 void Invoke(param1 p1,param2 p2,param3 p3,param4 p4,param5 p5,param6 p6)
81 {
82 (OwnClassHandler->*FuncHandler)(p1,p2,p3,p4,p5,p6);
83 }
84};
85
86
87//事件封装
88template<typename param1,typename param2,typename param3,typename param4,typename param5,typename param6>
89class CY_Event
90{
91protected:
92 typedef list< CY_FunctionInterface<param1,param2,param3,param4,param5,param6>*> T_FUNCTIONLIST;
93 typedef typename T_FUNCTIONLIST::iterator T_FUNCTION_ITERATOR;
94
95 T_FUNCTIONLIST FunctionList;
96
97private:
98 T_FUNCTION_ITERATOR Find(CY_FunctionInterface<param1,param2,param3,param4,param5,param6>* target)
99 {
100 void *temp1=target->GetClassHandler();
101 void *temp2=target->GetFunHandler();
102 T_FUNCTION_ITERATOR i=FunctionList.begin();
103 for (;i!=FunctionList.end();i++)
104 {
105 if (temp1==(*i)->GetClassHandler() && temp2==(*i)->GetFunHandler())
106 {
107 break;
108 }
109 }
110 return i;
111 }
112public:
113 inline void Invoke(param1 p1,param2 p2,param3 p3,param4 p4,param5 p5,param6 p6)
114 {
115 T_FUNCTION_ITERATOR i=FunctionList.begin();
116 for (;i!=FunctionList.end();i++)
117 {
118 (*i)->Invoke(p1,p2,p3,p4,p5,p6);
119 }
120 }
121
122 void Bind(typename CY_GolobalFunction<param1,param2,param3,param4,param5,param6>::T_HANDLER gFuncHandle)
123 {
124 CY_GolobalFunction<param1,param2,param3,param4,param5,param6>*addone=new CY_GolobalFunction<param1,param2,param3,param4,param5,param6>(gFuncHandle);
125 if(Find(addone)!=FunctionList.end())//已经存在了,就不需要添加
126 delete addone;//删除临时的new对象
127 else
128 FunctionList.push_back(addone);
129 }
130 void UnBind(typename CY_GolobalFunction<param1,param2,param3,param4,param5,param6>::T_HANDLER gFuncHandle)
131 {
132 CY_GolobalFunction<param1,param2,param3,param4,param5,param6>*delone=new CY_GolobalFunction<param1,param2,param3,param4,param5,param6>(gFuncHandle);
133 T_FUNCTION_ITERATOR delnode=Find(delone);
134 if (delnode!=FunctionList.end())//存在,删除节点和指针
135 {
136 delete (*delnode);
137 FunctionList.erase(delnode);
138 }
139 delete delone;//删除临时的new对象
140 }
141 template<typename Class>
142 void Bind(Class *classHandle,typename CY_MemberFunction<Class,param1,param2,param3,param4,param5,param6>::T_HANDLER mFuncHandle)
143 {
144 CY_MemberFunction<Class,param1,param2,param3,param4,param5,param6>*addone=new CY_MemberFunction<Class,param1,param2,param3,param4,param5,param6>(classHandle,mFuncHandle);
145 if (Find(addone)!=FunctionList.end())//已经存在
146 delete addone;//删除临时的new对象
147 else
148 FunctionList.push_back(addone);
149 }
150 template<typename Class>
151 void UnBind(Class *classHandle,typename CY_MemberFunction<Class,param1,param2,param3,param4,param5,param6>::T_HANDLER mFuncHandle)
152 {
153 CY_MemberFunction<Class,param1,param2,param3,param4,param5,param6>*delone=new CY_MemberFunction<Class,param1,param2,param3,param4,param5,param6>(classHandle,mFuncHandle);
154 T_FUNCTION_ITERATOR delnode=Find(delone);
155 if (delnode!=FunctionList.end())//存在,删除节点和指针
156 {
157 delete (*delnode);
158 FunctionList.erase(delnode);
159 }
160 delete delone;//删除临时的new对象
161 }
162 void operator()(param1 p1,param2 p2,param3 p3,param4 p4,param5 p5,param6 p6)
163 {
164 Invoke(p1,p2,p3,p4,p5,p6);
165 }
166 ~CY_Event()
167 {
168 T_FUNCTION_ITERATOR i=FunctionList.begin();
169 for (;i!=FunctionList.end();i++)
170 {
171 delete (*i);
172 }
173 }
174};
测试代码:
1#define _CRTDBG_MAP_ALLOC
2#include<stdlib.h>
3
4#include<crtdbg.h>
5
6#include <iostream>
7#include "Event.h"
8
9using namespace std;
10
11class MyClass
12{
13public:
14 void func(int a1,int a2,int a3,int a4,int a5,int a6)
15 {
16 cout<<"class func!!"<<endl;
17 cout<<a1<<endl;
18 cout<<a2<<endl;
19 cout<<a3<<endl;
20 cout<<a4<<endl;
21 cout<<a5<<endl;
22 cout<<a6<<endl;
23 cout<<"class func end-----!!"<<endl;
24 }
25};
26void gfunc(int a1,int a2,int a3,int a4,int a5,int a6)
27{
28 cout<<"global func!!"<<endl;
29 cout<<a1<<endl;
30 cout<<a2<<endl;
31 cout<<a3<<endl;
32 cout<<a4<<endl;
33 cout<<a5<<endl;
34 cout<<a6<<endl;
35 cout<<"global func end------!!"<<endl;
36}
37
38int main()
39{
40 MyClass class1;
41
42 CY_Event<int,int,int,int,int,int> *eve;
43
44 eve=new CY_Event<int,int,int,int,int,int>;
45
46 eve->Bind(&gfunc);
47 eve->Bind(&class1,&MyClass::func);
48
49 cout<<"invoking"<<endl;
50 eve->Invoke(1,2,3,4,5,6);
51 eve->UnBind(&class1,&MyClass::func);
52 cout<<"-------------after unBind-------------"<<endl;
53 eve->Invoke(6,5,4,3,2,1);
54
55 delete eve;
56
57 int a;
58 cin>>a;
59
60 _CrtDumpMemoryLeaks();
61}
运行结果:
posted on 2009-07-25 14:40
陈昱(CY) 阅读(1899)
评论(6) 编辑 收藏 引用 所属分类:
C++