Alex Bikfalvi
SimStream Documentation
Timer.h
00001 #pragma once 00002 00003 #include "SimHandler.h" 00004 #include "TimerBase.h" 00005 #include "EventTimer.h" 00006 00007 template<class T> class CTimer : public CTimerBase 00008 { 00009 protected: 00010 CSimHandler* sim; 00011 T* object; 00012 void (T::*handler)(CTimerInfo* info); 00013 CEventTimer* evt; 00014 00015 public: 00016 CTimer( 00017 CSimHandler* sim, 00018 T* object, 00019 void (T::*handler)(CTimerInfo* info) = NULL 00020 ); 00021 00022 virtual ~CTimer(); 00023 00024 inline bool IsSet() { return this->evt != NULL; } 00025 00026 void SetAt(__time time, CTimerInfo* info = NULL); 00027 void SetAt(__time time, void (T::*handler)(CTimerInfo* info), CTimerInfo* info = NULL); 00028 void SetAfter(__time interval, CTimerInfo* info = NULL); 00029 void SetAfter(__time interval, void (T::*handler)(CTimerInfo* info), CTimerInfo* info = NULL); 00030 00031 void ResetAt(__time time); 00032 void ResetAfter(__time interval); 00033 00034 void Cancel(); 00035 00036 virtual void Finish(CSimEvent* evt); 00037 }; 00038 00039 template<class T> CTimer<T>::CTimer( 00040 CSimHandler* sim, 00041 T* object, 00042 void (T::*handler)(CTimerInfo* info) 00043 ) 00044 { 00045 assert(sim); 00046 assert(object); 00047 00048 this->sim = sim; 00049 this->object = object; 00050 this->handler = handler; 00051 this->evt = NULL; 00052 } 00053 00054 template<class T> CTimer<T>::~CTimer() 00055 { 00056 // Cancel any pending events 00057 if(this->evt) this->sim->CancelEvent(this->evt->Time(), this->evt); 00058 } 00059 00060 template<class T> void CTimer<T>::SetAt(__time time, CTimerInfo* info) 00061 { 00062 assert(NULL == this->evt); 00063 assert(NULL != this->handler); 00064 00065 // Schedule next event 00066 CEventTimer* evt = new CEventTimer(this, time, info); 00067 00068 this->sim->ScheduleEventAt(time, evt); 00069 00070 this->evt = evt; 00071 } 00072 00073 template<class T> void CTimer<T>::SetAt(__time time, void (T::*handler)(CTimerInfo* info), CTimerInfo* info) 00074 { 00075 assert(NULL == this->evt); 00076 assert(NULL != handler); 00077 00078 // Set the handler 00079 this->handler = handler; 00080 00081 // Schedule next event 00082 CEventTimer* evt = new CEventTimer(this, time, info); 00083 00084 this->sim->ScheduleEventAt(time, evt); 00085 00086 this->evt = evt; 00087 } 00088 00089 template<class T> void CTimer<T>::SetAfter(__time interval, CTimerInfo* info) 00090 { 00091 assert(NULL == this->evt); 00092 assert(NULL != this->handler); 00093 00094 // Schedule next event 00095 CEventTimer* evt = new CEventTimer(this, this->sim->Time() + interval, info); 00096 00097 this->sim->ScheduleEventAt(this->sim->Time() + interval, evt); 00098 00099 this->evt = evt; 00100 } 00101 00102 template<class T> void CTimer<T>::SetAfter(__time interval, void (T::*handler)(CTimerInfo* info), CTimerInfo* info) 00103 { 00104 assert(NULL == this->evt); 00105 assert(NULL != handler); 00106 00107 // Set the handler 00108 this->handler = handler; 00109 00110 // Schedule next event 00111 CEventTimer* evt = new CEventTimer(this, this->sim->Time() + interval, info); 00112 00113 this->sim->ScheduleEventAt(this->sim->Time() + interval, evt); 00114 00115 this->evt = evt; 00116 } 00117 00118 template<class T> void CTimer<T>::ResetAt(__time time) 00119 { 00120 assert(NULL != this->evt); 00121 00122 CTimerInfo* info = this->evt->Info(); 00123 00124 // Cancel old event 00125 this->sim->CancelEvent(this->evt->Time(), this->evt); 00126 00127 // Schedule next event 00128 CEventTimer* evt = new CEventTimer(this, time, info); 00129 00130 this->sim->ScheduleEventAt(time, evt); 00131 00132 this->evt = evt; 00133 } 00134 00135 template<class T> void CTimer<T>::ResetAfter(__time interval) 00136 { 00137 assert(NULL != this->evt); 00138 00139 CTimerInfo* info = this->evt->Info(); 00140 00141 // Cancel old event 00142 this->sim->CancelEvent(this->evt->Time(), this->evt); 00143 00144 // Schedule next event 00145 CEventTimer* evt = new CEventTimer(this, this->sim->Time() + interval, info); 00146 00147 this->sim->ScheduleEventAt(this->sim->Time() + interval, evt); 00148 00149 this->evt = evt; 00150 } 00151 00152 template<class T> void CTimer<T>::Cancel() 00153 { 00154 // Cancel timer 00155 assert(this->evt); 00156 00157 this->sim->CancelEvent(this->evt->Time(), this->evt); 00158 00159 this->evt = NULL; 00160 } 00161 00162 template<class T> void CTimer<T>::Finish(CSimEvent* evt) 00163 { 00164 assert(this->evt == evt); 00165 00166 // Copy the info 00167 CTimerInfo* info = this->evt->Info(); 00168 00169 // Set event to null 00170 this->evt = NULL; 00171 00172 // Call event handler 00173 (this->object->*this->handler)(info); 00174 }
Last updated: February 8, 2011