To Be C++
shaker's Blog
生當(dāng)作人杰,死亦為鬼雄,至今思項(xiàng)羽,不肯過江東。
首頁
新隨筆
聯(lián)系
聚合
管理
隨筆-60 評(píng)論-111 文章-0 trackbacks-0
用C++擺弄了一個(gè)事件模型
參考了CPPblog上一位同學(xué)的文章
原文在這里
基本是照搬了代碼 但是不知道是作者沒有在VC71中測(cè)試 還是發(fā)出來的代碼還是有問題的 我在VC71中編譯還是有點(diǎn)點(diǎn)小小的曲折
下面是我自己根據(jù)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
編譯的時(shí)候要打開RTTI!
posted on 2006-09-04 12:27
shaker(太子)
閱讀(528)
評(píng)論(0)
編輯
收藏
引用
所屬分類:
C++
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
Relocate SVN
又是一個(gè)APIHOOK
[zt] Windows APC機(jī)制
[zt]談?wù)剬?duì)APC的一點(diǎn)理解
NtProtectVirtualMemory
【轉(zhuǎn)帖】Windows網(wǎng)絡(luò)體系結(jié)構(gòu)總結(jié)
Pro OGRE 3D Programming 中文翻譯版本0.2.0
Boost的狀態(tài)機(jī)庫教程 補(bǔ)充
Boost的狀態(tài)機(jī)庫教程(3)
Boost的狀態(tài)機(jī)庫教程(2)
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
給我發(fā)QQ消息
我的微博
libghttp
win7sp1
<
2010年8月
>
日
一
二
三
四
五
六
25
26
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
31
1
2
3
4
留言簿
(25)
給我留言
查看公開留言
查看私人留言
隨筆分類
(52)
C++(37)
Delphi(2)
Utility(10)
Windows Kernel(3)
隨筆檔案
(60)
2014年4月 (1)
2011年12月 (1)
2011年9月 (1)
2011年7月 (1)
2011年5月 (2)
2011年3月 (1)
2011年2月 (3)
2011年1月 (1)
2010年8月 (6)
2010年3月 (1)
2009年11月 (1)
2009年6月 (1)
2009年5月 (1)
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
系統(tǒng)設(shè)計(jì)系列文章
Learning boost
飯中淹的避難所
懶人日志
笑笑小生的博客
沐楓小筑
微妙的平衡
有一些HGE的技巧
鏈接
C++博客
就是這里啦
Code Project
大名鼎鼎的網(wǎng)站 全e文
CSDN.net
感覺是個(gè)大雜燴 不專精 不過但凡國內(nèi)的程序員大多也會(huì)去那看看的
VC知識(shí)庫
VC資料網(wǎng)站 收集的還算多的 不過還需要向CodeProject學(xué)習(xí)
游戲外掛研究院
算是曾今國內(nèi)眾多外掛高手的根據(jù)地吧 不過人心散了... 難現(xiàn)往日輝煌
搜索
積分與排名
積分 - 137983
排名 - 188
最新評(píng)論
1.?re: [ZT]C++ Boost Thread 編程指南
謝謝你的分享。
--ALPg
2.?re: [工具]VSS2005 下載
@周超
用迅雷試試
--shaker
3.?re: [工具]VSS2005 下載
。。。
--。
4.?re: [工具]VSS2005 下載
gdfg
--fd
5.?re: [工具]VSS2005 下載
下不了啊。404錯(cuò)誤
--周超
閱讀排行榜
1.?[ZT]C++ Boost Thread 編程指南 (20556)
2.?[工具]VSS2005 下載(9330)
3.?C++ Builder 2007 破解文件(6757)
4.?NtProtectVirtualMemory(6636)
5.?[轉(zhuǎn)]RGB與YUV轉(zhuǎn)換(5991)
6.?Boost的狀態(tài)機(jī)庫教程(1) (5775)
7.?C++Builder2007 安裝辦法(4680)
8.?經(jīng)典的XP主題Luna Element全集(3767)
9.?BCGControlBar Library Professional Edition v9.56(3479)
10.?發(fā)一個(gè)mir2的內(nèi)掛代碼(3258)
評(píng)論排行榜
1.?write a simple os with asm&c(23)
2.?[工具]VSS2005 下載(13)
3.?BCGControlBar Library Professional Edition v9.56(10)
4.?Visual Assist X v10.3.1534 build 2006.09.02 cracked dll's by Av0id(8)
5.?傳奇2TMD(8)
6.?C++ Builder 2007 破解文件(7)
7.?發(fā)一個(gè)mir2的內(nèi)掛代碼(5)
8.?更新了下TMD!幾張截圖(5)
9.?MSDN的Bug!(3)
10.?2007年5月 Windows XP Pro SP2 最新正版驗(yàn)證破解文件(3)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 shaker(太子)
精品欧美一区二区三区久久久
|
久久大香香蕉国产
|
精品久久久久中文字幕一区
|
久久人人爽人爽人人爽av
|
色综合久久天天综合
|
一本一道久久精品综合
|
欧美无乱码久久久免费午夜一区二区三区中文字幕
|
久久久久久久久无码精品亚洲日韩
|
国产成人久久激情91
|
伊人精品久久久久7777
|
狠狠色丁香久久综合五月
|
欧洲国产伦久久久久久久
|
国产成人久久激情91
|
青青热久久国产久精品
|
久久国产精品久久国产精品
|
人妻少妇精品久久
|
亚洲国产精品久久
|
久久综合狠狠综合久久
|
欧美精品一区二区久久
|
99久久久精品免费观看国产
|
久久只有这精品99
|
99热都是精品久久久久久
|
久久久久99精品成人片直播
|
四虎国产精品成人免费久久
|
九九久久精品无码专区
|
久久综合中文字幕
|
久久国产精品久久
|
久久噜噜电影你懂的
|
久久国产亚洲精品无码
|
人妻久久久一区二区三区
|
久久久久久久波多野结衣高潮
|
久久青青草原精品国产不卡
|
久久婷婷国产麻豆91天堂
|
精品999久久久久久中文字幕
|
久久精品国产亚洲AV高清热
|
亚洲色大成网站WWW久久九九
|
97视频久久久
|
精品少妇人妻av无码久久
|
久久发布国产伦子伦精品
|
狠狠色丁香久久综合五月
|
91精品国产91热久久久久福利
|