To Be C++
shaker's Blog
生当作人杰,死亦为鬼雄,至今思项羽,不肯过江东。
首页
新随笔
联系
聚合
管理
随笔-39 评论-92 文章-0 trackbacks-0
用C++摆弄了一个事件模型
参考了CPPblog上一位同学的文章
原文在这里
基本是照搬了代码 但是不知道是作者没有在VC71中测试 还是发出来的代码还是有问题的 我在VC71中编译还是有点点小小的曲折
下面是我自己根据VC71修改的!
1
class
CEvent;
2
3
class
CFunImpl
4
{
5
public
:
6
CFunImpl()
{}
7
virtual
~
CFunImpl()
{}
8
virtual
void
operator
()(CEvent
&
e)
=
0
;
9
virtual
bool
operator
==
(
const
CFunImpl
&
fun)
=
0
;
10
virtual
CFunImpl
*
Clone()
=
0
;
11
}
;
12
13
template
<
typename ClassName
>
14
class
CMemberFunc :
public
CFunImpl
15
{
16
public
:
17
18
typedef CMemberFunc
<
ClassName
>
this_type;
19
typedef
void
(ClassName::
*
fEventHandler) ( CEvent
&
e );
20
21
CMemberFunc( ClassName
&
obj, fEventHandler impl ) : m_Object(obj), m_pImpl(impl)
{}
22
23
void
operator
()( CEvent
&
e )
24
{
25
if
( m_pImpl
!=
NULL ) (m_Object.
*
(m_pImpl))( e );
26
}
27
28
CFunImpl
*
Clone()
29
{
30
return
new
this_type(m_Object, m_pImpl);
31
}
32
33
bool
operator
==
(
const
CFunImpl
&
fun)
34
{
35
if
( typeid(
*
this
)
==
typeid(fun) )
36
{
37
const
this_type
&
rFun
=
dynamic_cast
<
const
this_type
&
>
(fun);
38
39
return
(
&
m_Object
==
&
rFun.m_Object
&&
m_pImpl
==
rFun.m_pImpl);
40
}
41
42
return
false
;
43
}
44
45
virtual
~
CMemberFunc()
46
{
47
}
48
protected
:
49
ClassName
&
m_Object;
50
fEventHandler m_pImpl;
51
}
;
52
53
class
CStaticFunc :
public
CFunImpl
54
{
55
public
:
56
typedef
void
(
*
fEventHandler) ( CEvent
&
e );
57
58
CStaticFunc( fEventHandler impl ) : m_pImpl(impl)
{}
59
60
void
operator
()( CEvent
&
e )
61
{
62
if
( m_pImpl
!=
NULL ) m_pImpl( e );
63
}
64
65
CFunImpl
*
Clone()
66
{
67
return
new
CStaticFunc(m_pImpl);
68
}
69
70
bool
operator
==
(
const
CFunImpl
&
fun)
71
{
72
if
( typeid(
*
this
)
==
typeid(fun) )
73
{
74
const
CStaticFunc
&
rFun
=
dynamic_cast
<
const
CStaticFunc
&
>
(fun);
75
76
return
(m_pImpl
==
rFun.m_pImpl);
77
}
78
79
return
false
;
80
}
81
82
virtual
~
CStaticFunc()
83
{
84
}
85
protected
:
86
fEventHandler m_pImpl;
87
}
;
88
89
class
CEventHandler
90
{
91
private
:
92
void
Clear()
{
if
(m_pImpl)
{delete m_pImpl ;m_pImpl
=
NULL ;}
}
93
CFunImpl
*
m_pImpl;
94
public
:
95
~
CEventHandler()
96
{
97
Clear();
98
}
99
template
<
typename ClassName
>
100
CEventHandler( ClassName
&
obj,
void
(ClassName::
*
impl)(CEvent
&
) ) : m_pImpl(
new
CMemberFunc
<
ClassName
>
(obj,impl) )
{}
101
CEventHandler(
void
(
*
impl)(CEvent
&
) ) : m_pImpl(
new
CStaticFunc(impl) )
{}
102
CEventHandler(
const
CEventHandler
&
fun ) : m_pImpl( NULL )
{
*
this
=
fun; }
103
void
operator
() ( CEvent
&
e )
104
{
105
(
*
m_pImpl)(e);
106
}
107
CEventHandler
&
operator
=
(
const
CEventHandler
&
fun )
108
{
109
Clear();
110
if
(fun.m_pImpl) m_pImpl
=
fun.m_pImpl
->
Clone();
111
return
*
this
;
112
}
113
bool
operator
==
(
const
CEventHandler
&
handler )
114
{
115
if
( m_pImpl
==
NULL
||
handler.m_pImpl
==
NULL )
return
true
;
116
if
( typeid(
*
m_pImpl)
==
typeid(
*
(handler.m_pImpl)) )
117
{
118
return
(
*
m_pImpl)
==
(
*
(handler.m_pImpl));
119
}
120
return
false
;
121
}
122
}
;
123
124
class
CEvent
125
{
126
private
:
127
std::list
<
CEventHandler
>
m_Funcs;
128
void
Register( CEventHandler handle )
129
{
130
m_Funcs.push_back(handle);
131
}
132
void
UnRegister(
const
CEventHandler
&
handler )
133
{
134
m_Funcs.remove(handler);
135
}
136
void
*
lpData;
137
public
:
138
void
*
GetPointer()
{
return
lpData; }
139
CEvent
&
operator
<<
(
const
CEventHandler
&
handler )
140
{
141
Register ( handler );
142
return
*
this
;
143
}
144
145
CEvent
&
operator
>>
(
const
CEventHandler
&
handler )
146
{
147
UnRegister ( handler );
148
return
*
this
;
149
}
150
151
void
operator
( )(
void
*
pData
=
NULL )
152
{
153
lpData
=
pData;
154
for
(std::list
<
CEventHandler
>
::iterator pos
=
m_Funcs.begin(); pos
!=
m_Funcs.end();
++
pos )
155
(
*
pos)(
*
this
);
156
}
157
}
;
158
编译的时候要打开RTTI!
posted on 2006-09-04 12:27
shaker(太子)
阅读(130)
评论(0)
编辑
收藏
引用
所属分类:
C++
标题
姓名
主页
验证码
*
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
Windows下无法安装VS2005SP1的解决办法
BCGSoft announces that BCGControlBar Pro for MFC version 10.0 is available!
BCGControlBar Library Professional Edition v9.56
[ZT]C++ Boost Thread 编程指南
C++Builder2007 安装办法
[图书]使用wxWidgets进行跨平台程序开发
发一个mir2的内挂代码
使用boost::signals连接类成员函数
write a simple os with asm&c
以前写的一个网络流封包类
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
点击这里给我发QQ消息
<
2006年9月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
留言簿
(16)
给我留言
查看公开留言
查看私人留言
随笔分类
(32)
C++(23)
Delphi(1)
Utility(8)
随笔档案
(39)
2008年8月 (1)
2008年6月 (1)
2008年3月 (1)
2008年2月 (1)
2007年10月 (2)
2007年9月 (1)
2007年8月 (2)
2007年7月 (1)
2007年5月 (2)
2007年4月 (2)
2007年2月 (2)
2006年12月 (7)
2006年11月 (2)
2006年10月 (2)
2006年9月 (12)
精彩blog
#ant
boost源码剖析
CppExplore
系统设计系列文章
Learning boost
懒人日志
笑笑小生的博客
沐枫小筑
微妙的平衡
有一些HGE的技巧
链接
C++博客
就是这里啦
Code Project
大名鼎鼎的网站 全e文
CSDN.net
感觉是个大杂烩 不专精 不过但凡国内的程序员大多也会去那看看的
VC知识库
VC资料网站 收集的还算多的 不过还需要向CodeProject学习
游戏外挂研究院
算是曾今国内众多外挂高手的根据地吧 不过人心散了... 难现往日辉煌
搜索
积分与排名
积分 - 35148
排名 - 78
最新评论
1. re: [工具]VSS2005 下载
Thank you!
--RitaYoung
2. re: [工具]VSS2005 下载[未登录]
谢谢您拉3Q
--RON
3. re: [工具]VSS2005 下载
太太谢,好人呀!!!
--gengyupin
4. re: [工具]VSS2005 下载
太赞了,谢谢!
--秒大刀
5. re: BCGControlBar Library Professional Edition v9.56[未登录]
谢谢
没破解我回头找你
--我
阅读排行榜
1. C++ Builder 2007 破解文件(4002)
2. [工具]VSS2005 下载(2414)
3. C++Builder2007 安装办法(2300)
4. 用boost::regex来做词法分析(1752)
5. BCGControlBar Library Professional Edition v9.56(1615)
6. 花一晚上琢磨了个Skin换肤代码,请各位指教!(1489)
7. 经典的XP主题Luna Element全集(1415)
8. 用处不大却不是没用的一个类!(1373)
9. 2007年5月 Windows XP Pro SP2 最新正版验证破解文件(1347)
10. 发一个mir2的内挂代码(1322)
评论排行榜
1. write a simple os with asm&c(23)
2. BCGControlBar Library Professional Edition v9.56(10)
3. Visual Assist X v10.3.1534 build 2006.09.02 cracked dll's by Av0id(8)
4. 传奇2TMD(7)
5. [工具]VSS2005 下载(6)
6. C++ Builder 2007 破解文件(6)
7. 更新了下TMD!几张截图(5)
8. 发一个mir2的内挂代码(4)
9. MSDN的Bug!(3)
10. 2007年5月 Windows XP Pro SP2 最新正版验证破解文件(3)