/*************************************************************************** * E-Com Technology Ltd. * * ECOMPACS DICOM Utility Libraries ***************************************************************************/ #pragma once #include "Delegate.hpp" #include "MultiDelegate.hpp" #include "MultiNotifyDelegate.hpp" class TimerDelegateArgs : public DelegateArgs { public: DWORD m_Para; }; class DelegateHandlerTimer : public DelegateHandler { public: inline DelegateHandlerTimer (ULONG Interval, DWORD para) { m_Interval = Interval; m_Para = para; static const delay = 15 * 1000; // 15 seconds if (Interval > delay) m_msCount = Interval - delay; // Run it after 15 seconds else m_msCount = 0; // Run it later } inline DelegateHandlerTimer (DDateTime NextAlarm, DWORD para) { m_NextAlarm = NextAlarm ; m_Para = para; } public: ULONG m_Interval; ULONG m_msCount; DDateTime m_NextAlarm; DWORD m_Para; }; class StaticDelegateHandlerTimer : public DelegateHandlerTimer { public: typedef void (* fnDelegate) (const void *, DelegateArgs *); inline StaticDelegateHandlerTimer (fnDelegate delegate, ULONG interval, DWORD para) : DelegateHandlerTimer (interval, para) { m_fnDelegate = delegate; } inline StaticDelegateHandlerTimer (fnDelegate delegate, DDateTime NextAlarm, DWORD para) : DelegateHandlerTimer (NextAlarm, para) { m_fnDelegate = delegate; } inline virtual void Invoke (const void * sender, DelegateArgs *) { try { TimerDelegateArgs arg; arg.m_Para = m_Para; m_fnDelegate (sender, &arg); } catch (...) { #ifdef _DEBUG Beep (1000, 1000); OnDelegateInvokeFailed (this); #endif } } inline virtual int IsEqual (const DelegateHandler & handler) const { const StaticDelegateHandlerTimer * staticHandler = static_cast (& handler); if (staticHandler != 0) { return ( staticHandler->m_fnDelegate == m_fnDelegate && staticHandler->m_Para == m_Para ); } return false; } inline virtual bool IsEmpty (void) const { return (m_fnDelegate == NULL); } inline virtual DelegateHandlerTimer * Clone (void) const { StaticDelegateHandlerTimer * p = new StaticDelegateHandlerTimer (m_fnDelegate, 0, 0); p->m_Interval = m_Interval; p->m_msCount = m_msCount; p->m_NextAlarm = m_NextAlarm; p->m_Para = m_Para; return p; } protected: fnDelegate m_fnDelegate; }; template class ClassDelegateHandlerTimer : public DelegateHandlerTimer { public: typedef void (T::*classDelegate) (const void *, DelegateArgs *); inline ClassDelegateHandlerTimer (T * pthis, classDelegate delegate, ULONG interval, DWORD para) : DelegateHandlerTimer (interval, para) { m_pthis = pthis; m_classDelegate = delegate; } inline ClassDelegateHandlerTimer (T * pthis, classDelegate delegate, DDateTime NextAlarm, DWORD para) : DelegateHandlerTimer (NextAlarm, para) { m_pthis = pthis; m_classDelegate = delegate; } inline virtual void Invoke (const void * sender, DelegateArgs *) { try { TimerDelegateArgs arg; arg.m_Para = m_Para; (* m_pthis.*m_classDelegate) (sender, & arg); } catch (...) { #ifdef _DEBUG Beep (1000, 1000); OnDelegateInvokeFailed (this); #endif } } inline virtual int IsEqual (const DelegateHandler & handler) const { const ClassDelegateHandlerTimer * classHandler = static_cast (& handler); if (classHandler != 0) { return ( classHandler->m_classDelegate == m_classDelegate && classHandler->m_pthis == m_pthis && classHandler->m_Para == m_Para ); } return false; } inline virtual bool IsEmpty (void) const { return (m_classDelegate == NULL); } inline virtual DelegateHandlerTimer * Clone (void) const { class ClassDelegateHandlerTimer * p = new ClassDelegateHandlerTimer (m_pthis, m_classDelegate, 0, 0); p->m_Interval = m_Interval; p->m_msCount = m_msCount; p->m_NextAlarm = m_NextAlarm; p->m_Para = m_Para; return p; } //protected: public: classDelegate m_classDelegate; T * m_pthis; }; #define NewTimer(theClass, instance, memberFunc, interval, para) \ new ClassDelegateHandlerTimer (instance, (ClassDelegateHandlerTimer ::classDelegate) (theClass::memberFunc), interval, para) #define NewTimerStatic(staticFunc, interval, para) \ new StaticDelegateHandlerTimer ((StaticDelegateHandlerTimer::fnDelegate)staticFunc, interval, para)