123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- /***************************************************************************
- * 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 <const StaticDelegateHandlerTimer *> (& 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 <typename class T>
- 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 <const ClassDelegateHandlerTimer *> (& 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 <T> * p =
- new ClassDelegateHandlerTimer <T> (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 <theClass> (instance, (ClassDelegateHandlerTimer <theClass>::classDelegate) (theClass::memberFunc), interval, para)
- #define NewTimerStatic(staticFunc, interval, para) \
- new StaticDelegateHandlerTimer ((StaticDelegateHandlerTimer::fnDelegate)staticFunc, interval, para)
|