事件是面向组件开发的必要特性之一,但C++不直接支持事件,没关系,我自己实现了一个,感觉很好用,分享给大家!
最开始打算用函数指针模拟事件,但由于C++中成员函数指针不能和void*相互强转,而且typedef中不能含有模板,所以才不得已以接口继承实现。这样效果也不错 :)
一. 先看看事件接口定义和实现
#ifndefIEVENT_H
#defineIEVENT_H
/*
以下各基础设施是在C++中事件机制的完整实现,事件是面向组件开发的必要特性之一。
创作者:sky
时间:2005.06.22
修订时间:2005.06.22
*/
#include"../Collection/SafeArrayList.h"
template<classSenderType,classParaType>classEventPublisher;
classNullType
{
};
//IEventHandler是事件处理句柄,预定事件的类从此接口继承以实现事件处理函数
template<classSenderType,classParaType>
interfaceIEventHandler
{
public:
virtual~IEventHandler(){}
private:
virtualvoidHandleEvent(SenderTypesender,ParaTypepara)=0;
friendclassEventPublisher<SenderType,ParaType>;
};
//IEvent事件预定方通过此接口预定事件
template<classSenderType,classParaType>
interfaceIEvent
{
public:
virtual~IEvent(){}
virtualvoidRegister(IEventHandler<SenderType,ParaType>*handler)=0;
virtualvoidUnRegister(IEventHandler<SenderType,ParaType>*handler)=0;
};
//IEventActivator事件发布方通过此接口触发事件
template<classSenderType,classParaType>
interfaceIEventActivator
{
public:
virtual~IEventActivator(){}
virtualvoidInvoke(SenderTypesender,ParaTypepara)=0;
virtualintHandlerCount()=0;
virtualIEventHandler<SenderType,ParaType>*GetHandler(intindex)=0;
};
//IEventPublisher事件发布方发布事件相当于就是发布一个IEventPublisher派生类的对象
//不过仅仅将该对象的IEvent接口发布即可。
template<classSenderType,classParaType>
interfaceIEventPublisher:publicIEvent<SenderType,ParaType>,publicIEventActivator<SenderType,ParaType>
{
};
//EventPublisher是IEventPublisher的默认实现
template<classSenderType,classParaType>
classEventPublisher:publicIEventPublisher<SenderType,ParaType>
{
private:
SafeArrayList<IEventHandler<SenderType,ParaType>>handerList;
IEventHandler<SenderType,ParaType>*innerHandler;
public:
voidRegister(IEventHandler<SenderType,ParaType>*handler)
{
this->handerList.Add(handler);
}
voidUnRegister(IEventHandler<SenderType,ParaType>*handler)
{
this->handerList.Remove(handler);
}
voidInvoke(SenderTypesender,ParaTypepara)
{
intcount=this->handerList.Count();
for(inti=0;i<count;i++)
{
IEventHandler<SenderType,ParaType>*handler=this->handerList.GetElement(i);
handler->HandleEvent(sender,para);
}
}
intHandlerCount()
{
returnthis->handerList.Count();
}
IEventHandler<SenderType,ParaType>*GetHandler(intindex)
{
returnthis->handerList.GetElement(index);
}
};
#endif
上面的实现是浅显易懂的,关键是要注意IEventPublisher的双重身份-- 事件发布方最好发布IEvent指针给外部,而该指针实际指向的是一个EventPublisher对象,这是为了避免外部直接调用IEventActivator接口的方法。
二. 一个定时器类Timer,演示如何发布事件。想必大家都知道定时器的用途了哦,这个Timer就像C#中的Timer类一样。
#ifndefTIMER_H
#defineTIMER_H
/*
Timer定时器,每经过一段指定时间就触发事件
创作者:sky
时间:2005.06.22
修订时间:2005.06.22
*/
#include"IEvent.h"
#include"Thread.h"
voidTimerThreadStart(void*para);
classTimer
{
private:
intspanInMillSecs;
volatileboolisStop;
volatilebooltimerThreadDone;
public:
friendvoidTimerThreadStart(void*para);
IEvent<Timer*,NullType>*TimerTicked;
Timer(intspan_InMillSecs)
{
this->isStop=true;
this->timerThreadDone=true;
this->spanInMillSecs=span_InMillSecs;
this->TimerTicked=newEventPublisher<Timer*,NullType>;
}
~Timer()
{
this->Stop();
deletethis->TimerTicked;
}
voidStart()
{
if(!this->isStop)
{
return;
}
this->isStop=false;
Threadthread;
thread.Start(TimerThreadStart,this);
//unsignedintdwThreadId;
//HANDLEhThread=(HANDLE)_beginthreadex(NULL,0,(unsignedint(_stdcall*)(void*))&TimerThreadStart,this,0,&dwThreadId);
}
voidStop()
{
if(this->isStop)
{
return;
}
this->isStop=true;
//等待工作线程退出
while(!this->timerThreadDone)
{
Sleep(200);
}
}
private:
voidWorkerThread()
{
this->timerThreadDone=false;
while(!this->isStop)
{
Sleep(this->spanInMillSecs);
if(this->isStop)
{
break;
}
NullTypenullObj;
((EventPublisher<Timer*,NullType>*)this->TimerTicked)->Invoke(this,nullObj);
}
this->timerThreadDone=true;
}
};
voidTimerThreadStart(void*para)
{
Timer*timer=(Timer*)para;
timer->WorkerThread();
}
#endif
上面的示例清晰地说明了如何发布一个事件,如何在适当的时候触发事件,接下来看看如何预定事件。
三. 预定事件例子
classTimerEventExample:publicIEventHandler<Timer*,NullType>,CriticalSection
{
private:
Timer*timer;
public:
TimerEventExample(intcheckSpan)
{
this->timer=newTimer(checkSpan);
this->timer->TimerTicked->Register(this);
}
~TimerEventExample()
{
deletethis->timer;
}
private:
//处理定时事件
voidHandleEvent(Timer*sender,NullTypepara)
{
cout<<"Timeticked!"<<endl;
}
};
到这里,已经将C++中的事件机制的实现及使用讲清楚了。C#提供了很多便利的基础设施来支持组件开发,而在C++中,这些基础设施需要自己动手来构建,在拥有了这些设施之后,相信使用C++进行组件开发会轻松一点。
突然想起来,这里是.NET专区,这篇文章放在这里是否合适?犹豫了一下,还是决定留下来。也许可以对那些有C++基础的.NET开发者有所帮助,也算是对事件的内部机理的另一种展现吧。
分享到:
相关推荐
C++反射机制实现,根据类名字符串创建该类的实例;根据类的属性字符串来设置属性;
实现的事件驱动机制,绑定对象与事件,通过成员方法响应事件,采用c++的方法实现,不依赖于任何平台
事件模型是被广泛使用的好东西,但是C++标准库里没有现成的,其他实现又复杂或者不优雅,比如需要使用宏。现在VC11可以用在XP下了,那么痛快的拿起C++11提供的先进设施组合出一个轻便的实现吧。 为了达到简洁的...
C++消息机制
C++插件机制的实现,VS2013下实现。
C++ 模拟 C# 事件 函数指针 尾随参数
C++Event机制的简单实现
C++反射机制
C++实现反射机制
介绍了C++编程语言中的虚函数及其在进行面向对象程序设计中重要性,并且详细阐述了它在编译器底层虚函数的实现机制.它通过一个vptr和vtable在运行时进行动态绑定,从而能够根据对象类型的不同调用不同的 虚函数;并...
(1)它让声明和实现进行解耦,在项目开发时,如果很多文件引用了该头文件,如果头文件改变了一些实现,那么在编译时,所有引用的文件也得重新编译一次,增加了c++程序的编译时间, 如果把实现用一个指针进行声明,...
一个二叉树数据结构的实现代码。这个是一个软件过程中涉及到的作业。
泛型的基本概念 函数模板和类模板的实现 C++标准模板库
C++异常机制的实现方式和开销分析 一篇深度分析C++异常实现的文章(语言级别分析,没有深入过多的汇编和提供级别)
C++面向对象特性实现机制的初步分析 <br/>1准备知识 1.1 程序对内存的使用方法 1.2 C++ Class内存格局 1.3 编译期和执行期 <br/>2封装 2.1 封装的目的和意义 2.2 封装的实现机制 ...
C++虚拟与多态学习,帮你更深刻的理解虚拟与多态,更深刻的理解C++。技术更上一层楼