#ifndef IEVENT_H
#define IEVENT_H
/*
浠ヤ笅鍚勫熀紜璁炬柦鏄湪C++涓簨浠舵満鍒剁殑瀹屾暣瀹炵幇錛屼簨浠舵槸闈㈠悜緇勪歡寮鍙戠殑蹇呰鐗規т箣涓銆?BR>
鍒?nbsp;浣?nbsp;鑰咃細sky
鏃?nbsp; 闂達細2005.06.22
淇鏃墮棿錛?005.06.22
*/
#include "../Collection/SafeArrayList.h"
template<class SenderType ,class ParaType> class EventPublisher ;
class NullType
{
};
// IEventHandler 鏄簨浠跺鐞嗗彞鏌勶紝棰勫畾浜嬩歡鐨勭被浠庢鎺ュ彛緇ф壙浠ュ疄鐜頒簨浠跺鐞嗗嚱鏁?/SPAN>
template<class SenderType ,class ParaType>
interface IEventHandler
{
public:
virtual ~IEventHandler(){}
private:
virtual void HandleEvent(SenderType sender ,ParaType para) = 0 ;
friend class EventPublisher<SenderType ,ParaType> ;
};
// IEvent 浜嬩歡棰勫畾鏂歸氳繃姝ゆ帴鍙i瀹氫簨浠?/SPAN>
template<class SenderType ,class ParaType>
interface IEvent
{
public:
virtual ~IEvent(){}
virtual void Register (IEventHandler<SenderType ,ParaType>* handler) = 0 ;
virtual void UnRegister(IEventHandler<SenderType ,ParaType>* handler) = 0 ;
};
// IEventActivator 浜嬩歡鍙戝竷鏂歸氳繃姝ゆ帴鍙hЕ鍙戜簨浠?/SPAN>
template<class SenderType ,class ParaType>
interface IEventActivator
{
public:
virtual ~IEventActivator(){}
virtual void Invoke(SenderType sender ,ParaType para) = 0;
virtual int HandlerCount() = 0;
virtual IEventHandler<SenderType ,ParaType>* GetHandler(int index) = 0;
};
// IEventPublisher 浜嬩歡鍙戝竷鏂瑰彂甯冧簨浠剁浉褰撲簬灝辨槸鍙戝竷涓涓狪EventPublisher媧劇敓綾葷殑瀵硅薄
// 涓嶈繃浠呬粎灝嗚瀵硅薄鐨処Event鎺ュ彛鍙戝竷鍗沖彲銆?/SPAN>
template<class SenderType ,class ParaType>
interface IEventPublisher : public IEvent<SenderType ,ParaType> ,public IEventActivator<SenderType ,ParaType>
{
};
// EventPublisher鏄疘EventPublisher鐨勯粯璁ゅ疄鐜?/SPAN>
template<class SenderType ,class ParaType>
class EventPublisher :public IEventPublisher<SenderType ,ParaType>
{
private:
SafeArrayList< IEventHandler<SenderType ,ParaType> > handerList ;
IEventHandler<SenderType ,ParaType>* innerHandler ;
public:
void Register(IEventHandler<SenderType ,ParaType>* handler)
{
this->handerList.Add(handler) ;
}
void UnRegister(IEventHandler<SenderType ,ParaType>* handler)
{
this->handerList.Remove(handler) ;
}
void Invoke(SenderType sender ,ParaType para)
{
int count = this->handerList.Count() ;
for(int i=0 ;i<count ;i++)
{
IEventHandler<SenderType ,ParaType>* handler = this->handerList.GetElement(i) ;
handler->HandleEvent(sender ,para) ;
}
}
int HandlerCount()
{
return this->handerList.Count() ;
}
IEventHandler<SenderType ,ParaType>* GetHandler(int index)
{
return this->handerList.GetElement(index) ;
}
};
#endif
#ifndef TIMER_H
#define TIMER_H
/*
Timer 瀹氭椂鍣?姣忕粡榪囦竴孌墊寚瀹氭椂闂村氨瑙﹀彂浜嬩歡
鍒?nbsp;浣?nbsp;鑰咃細sky
鏃?nbsp; 闂達細2005.06.22
淇鏃墮棿錛?005.06.22
*/
#include "IEvent.h"
#include "Thread.h"
void TimerThreadStart(void* para) ;
class Timer
{
private:
int spanInMillSecs ;
volatile bool isStop ;
volatile bool timerThreadDone ;
public:
friend void TimerThreadStart(void* para) ;
IEvent<Timer* ,NullType>* TimerTicked ;
Timer(int span_InMillSecs)
{
this->isStop = true ;
this->timerThreadDone = true ;
this->spanInMillSecs = span_InMillSecs ;
this->TimerTicked = new EventPublisher<Timer* ,NullType> ;
}
~Timer()
{
this->Stop() ;
delete this->TimerTicked ;
}
void Start()
{
if(! this->isStop)
{
return ;
}
this->isStop = false ;
Thread thread ;
thread.Start(TimerThreadStart ,this) ;
//unsigned int dwThreadId ;
//HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0 , (unsigned int (_stdcall*)(void*))&TimerThreadStart , this, 0, &dwThreadId);
}
void Stop()
{
if( this->isStop)
{
return ;
}
this->isStop = true ;
//絳夊緟宸ヤ綔綰跨▼閫鍑?/SPAN>
while(! this->timerThreadDone)
{
Sleep(200) ;
}
}
private:
void WorkerThread()
{
this->timerThreadDone = false ;
while(! this->isStop)
{
Sleep(this->spanInMillSecs) ;
if(this->isStop)
{
break ;
}
NullType nullObj ;
((EventPublisher<Timer* ,NullType>*)this->TimerTicked)->Invoke(this ,nullObj) ;
}
this->timerThreadDone = true ;
}
};
void TimerThreadStart(void* para)
{
Timer* timer = (Timer*)para ;
timer->WorkerThread() ;
}
#endif
class TimerEventExample :public IEventHandler<Timer* ,NullType> ,CriticalSection
{
private:
Timer* timer ;
public:
TimerEventExample(int checkSpan)
{
this->timer = new Timer(checkSpan) ;
this->timer->TimerTicked->Register(this) ;
}
~TimerEventExample()
{
delete this->timer ;
}
private:
//澶勭悊瀹氭椂浜嬩歡
void HandleEvent(Timer* sender ,NullType para)
{
cout<<"Time ticked !"<<endl ;
}
};