TimerDelegate.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /***************************************************************************
  2. * E-Com Technology Ltd.
  3. *
  4. * ECOMPACS DICOM Utility Libraries
  5. ***************************************************************************/
  6. #pragma once
  7. #include "Delegate.hpp"
  8. #include "MultiDelegate.hpp"
  9. #include "MultiNotifyDelegate.hpp"
  10. class TimerDelegateArgs : public DelegateArgs
  11. {
  12. public:
  13. DWORD m_Para;
  14. };
  15. class DelegateHandlerTimer : public DelegateHandler
  16. {
  17. public:
  18. inline DelegateHandlerTimer (ULONG Interval, DWORD para)
  19. {
  20. m_Interval = Interval;
  21. m_Para = para;
  22. static const delay = 15 * 1000; // 15 seconds
  23. if (Interval > delay)
  24. m_msCount = Interval - delay; // Run it after 15 seconds
  25. else
  26. m_msCount = 0; // Run it later
  27. }
  28. inline DelegateHandlerTimer (DDateTime NextAlarm, DWORD para)
  29. {
  30. m_NextAlarm = NextAlarm ;
  31. m_Para = para;
  32. }
  33. public:
  34. ULONG m_Interval;
  35. ULONG m_msCount;
  36. DDateTime m_NextAlarm;
  37. DWORD m_Para;
  38. };
  39. class StaticDelegateHandlerTimer : public DelegateHandlerTimer
  40. {
  41. public:
  42. typedef void (* fnDelegate) (const void *, DelegateArgs *);
  43. inline StaticDelegateHandlerTimer (fnDelegate delegate, ULONG interval, DWORD para)
  44. : DelegateHandlerTimer (interval, para)
  45. {
  46. m_fnDelegate = delegate;
  47. }
  48. inline StaticDelegateHandlerTimer (fnDelegate delegate, DDateTime NextAlarm, DWORD para)
  49. : DelegateHandlerTimer (NextAlarm, para)
  50. {
  51. m_fnDelegate = delegate;
  52. }
  53. inline virtual void Invoke (const void * sender, DelegateArgs *)
  54. {
  55. try
  56. {
  57. TimerDelegateArgs arg;
  58. arg.m_Para = m_Para;
  59. m_fnDelegate (sender, &arg);
  60. }
  61. catch (...)
  62. {
  63. #ifdef _DEBUG
  64. Beep (1000, 1000);
  65. OnDelegateInvokeFailed (this);
  66. #endif
  67. }
  68. }
  69. inline virtual int IsEqual (const DelegateHandler & handler) const
  70. {
  71. const StaticDelegateHandlerTimer * staticHandler = static_cast <const StaticDelegateHandlerTimer *> (& handler);
  72. if (staticHandler != 0)
  73. {
  74. return (
  75. staticHandler->m_fnDelegate == m_fnDelegate &&
  76. staticHandler->m_Para == m_Para
  77. );
  78. }
  79. return false;
  80. }
  81. inline virtual bool IsEmpty (void) const
  82. {
  83. return (m_fnDelegate == NULL);
  84. }
  85. inline virtual DelegateHandlerTimer * Clone (void) const
  86. {
  87. StaticDelegateHandlerTimer * p =
  88. new StaticDelegateHandlerTimer (m_fnDelegate, 0, 0);
  89. p->m_Interval = m_Interval;
  90. p->m_msCount = m_msCount;
  91. p->m_NextAlarm = m_NextAlarm;
  92. p->m_Para = m_Para;
  93. return p;
  94. }
  95. protected:
  96. fnDelegate m_fnDelegate;
  97. };
  98. template <typename class T>
  99. class ClassDelegateHandlerTimer : public DelegateHandlerTimer
  100. {
  101. public:
  102. typedef void (T::*classDelegate) (const void *, DelegateArgs *);
  103. inline ClassDelegateHandlerTimer (T * pthis, classDelegate delegate, ULONG interval, DWORD para)
  104. : DelegateHandlerTimer (interval, para)
  105. {
  106. m_pthis = pthis;
  107. m_classDelegate = delegate;
  108. }
  109. inline ClassDelegateHandlerTimer (T * pthis, classDelegate delegate, DDateTime NextAlarm, DWORD para)
  110. : DelegateHandlerTimer (NextAlarm, para)
  111. {
  112. m_pthis = pthis;
  113. m_classDelegate = delegate;
  114. }
  115. inline virtual void Invoke (const void * sender, DelegateArgs *)
  116. {
  117. try
  118. {
  119. TimerDelegateArgs arg;
  120. arg.m_Para = m_Para;
  121. (* m_pthis.*m_classDelegate) (sender, & arg);
  122. }
  123. catch (...)
  124. {
  125. #ifdef _DEBUG
  126. Beep (1000, 1000);
  127. OnDelegateInvokeFailed (this);
  128. #endif
  129. }
  130. }
  131. inline virtual int IsEqual (const DelegateHandler & handler) const
  132. {
  133. const ClassDelegateHandlerTimer * classHandler = static_cast <const ClassDelegateHandlerTimer *> (& handler);
  134. if (classHandler != 0)
  135. {
  136. return (
  137. classHandler->m_classDelegate == m_classDelegate &&
  138. classHandler->m_pthis == m_pthis &&
  139. classHandler->m_Para == m_Para
  140. );
  141. }
  142. return false;
  143. }
  144. inline virtual bool IsEmpty (void) const
  145. {
  146. return (m_classDelegate == NULL);
  147. }
  148. inline virtual DelegateHandlerTimer * Clone (void) const
  149. {
  150. class ClassDelegateHandlerTimer <T> * p =
  151. new ClassDelegateHandlerTimer <T> (m_pthis, m_classDelegate, 0, 0);
  152. p->m_Interval = m_Interval;
  153. p->m_msCount = m_msCount;
  154. p->m_NextAlarm = m_NextAlarm;
  155. p->m_Para = m_Para;
  156. return p;
  157. }
  158. //protected:
  159. public:
  160. classDelegate m_classDelegate;
  161. T * m_pthis;
  162. };
  163. #define NewTimer(theClass, instance, memberFunc, interval, para) \
  164. new ClassDelegateHandlerTimer <theClass> (instance, (ClassDelegateHandlerTimer <theClass>::classDelegate) (theClass::memberFunc), interval, para)
  165. #define NewTimerStatic(staticFunc, interval, para) \
  166. new StaticDelegateHandlerTimer ((StaticDelegateHandlerTimer::fnDelegate)staticFunc, interval, para)