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