Panda3D
|
00001 #ifndef __TIME_OUT_H__ 00002 #define __TIME_OUT_H__ 00003 00004 /////////////////////////////////////// 00005 // 00006 // think of this class as a time based alarm.. 00007 // 00008 // would be nice to have a template implementation of this class .. could avoud some storage and some math .. 00009 // 00010 // I would do this but not sure how to represent the duration in the template ?? 00011 // 00012 ///////////////////////////////////////////////////////////////////////////////////////////// 00013 class Time_Out 00014 { 00015 public: 00016 Time_Out() 00017 { 00018 } 00019 00020 Time_Out(const Time_Span & dur) : _alarm_time(Time_Clock::GetCurrentTime() + dur) , _duration(dur) 00021 { 00022 } 00023 /* 00024 Time_Out(const Time_Clock & tm, const Time_Span & dur) : _alarm_time(tm + dur) , _duration(dur) 00025 { 00026 } 00027 */ 00028 void ResetAll(const Time_Clock &tm, const Time_Span &sp); 00029 void ReStart(); 00030 void ResetTime(const Time_Clock & tm); 00031 void SetTimeOutSec(int sec); 00032 00033 bool Expired(const Time_Clock &tm, bool reset = false); 00034 bool Expired(bool reset = false); 00035 00036 Time_Span Remaining(const Time_Clock & tm) const; 00037 Time_Span Remaining() const; 00038 00039 void ForceToExpired() 00040 { 00041 _alarm_time.ToCurrentTime(); 00042 } 00043 00044 bool operator() (bool reset= false) 00045 { 00046 return Expired(reset); 00047 } 00048 bool operator() (const Time_Clock &tm, bool reset = false) 00049 { 00050 return Expired(tm, reset); 00051 } 00052 00053 Time_Clock GetAlarm(void) 00054 { 00055 return _alarm_time; 00056 } 00057 00058 Time_Span Duration() const { return _duration; }; 00059 00060 void NextInStep(Time_Clock &curtime) 00061 { 00062 _alarm_time += _duration; 00063 if(_alarm_time <=curtime) // if we fall way behind.. just ratchet it up ... 00064 _alarm_time = curtime+_duration; 00065 } 00066 private: 00067 Time_Clock _alarm_time; 00068 Time_Span _duration; 00069 }; 00070 ////////////////////////////////////////////////////////////// 00071 // Function name : Time_Out::ReStart 00072 // Description : 00073 // Return type : void 00074 // Argument : const Time_Clock &tm 00075 // Argument : const Time_Span &sp 00076 ////////////////////////////////////////////////////////////// 00077 inline void Time_Out::ResetAll(const Time_Clock &tm, const Time_Span &sp) 00078 { 00079 _duration = sp; 00080 _alarm_time = tm + _duration; 00081 } 00082 ////////////////////////////////////////////////////////////// 00083 // Function name : Time_Span::ReStart 00084 // Description : 00085 // Return type : void 00086 // Argument : const Time_Clock &tm 00087 ////////////////////////////////////////////////////////////// 00088 inline void Time_Out::SetTimeOutSec(int sec) 00089 { 00090 _duration.Set(0, 0, 0, sec, 0); 00091 ReStart(); 00092 } 00093 ////////////////////////////////////////////////////////////// 00094 // Function name : Time_Span::ReStart 00095 // Description : 00096 // Return type : void 00097 // Argument : void 00098 ////////////////////////////////////////////////////////////// 00099 inline void Time_Out::ReStart() 00100 { 00101 _alarm_time = Time_Clock::GetCurrentTime() + _duration; 00102 } 00103 ////////////////////////////////////////////////////////////// 00104 // Function name : ResetTime 00105 // Description : 00106 // Return type : void 00107 // Argument : const Time_Clock & tm 00108 ////////////////////////////////////////////////////////////// 00109 inline void Time_Out::ResetTime(const Time_Clock & tm) 00110 { 00111 _alarm_time = tm + _duration; 00112 00113 } 00114 ////////////////////////////////////////////////////////////// 00115 // Function name : Time_Span::Expired 00116 // Description : 00117 // Return type : bool 00118 // Argument : const Time_Clock &tm 00119 ////////////////////////////////////////////////////////////// 00120 inline bool Time_Out::Expired(const Time_Clock &tm, bool reset) 00121 { 00122 bool answer = (_alarm_time <= tm) ; 00123 if (answer && reset) 00124 ResetTime(tm); 00125 return answer; 00126 } 00127 ////////////////////////////////////////////////////////////// 00128 // Function name : Time_Span::Expired 00129 // Description : 00130 // Return type : bool 00131 // Argument : void 00132 ////////////////////////////////////////////////////////////// 00133 inline bool Time_Out::Expired(bool reset) 00134 { 00135 return Expired(Time_Clock::GetCurrentTime(), reset); 00136 } 00137 ////////////////////////////////////////////////////////////// 00138 // Function name : Time_Span::Remaining 00139 // Description : 00140 // Return type : Time_Span 00141 // Argument : const Time_Clock & tm 00142 ////////////////////////////////////////////////////////////// 00143 inline Time_Span Time_Out::Remaining(const Time_Clock & tm) const 00144 { 00145 return _alarm_time - tm; 00146 } 00147 ////////////////////////////////////////////////////////////// 00148 // Function name : Time_Span::Remaining 00149 // Description : 00150 // Return type : Time_Span 00151 // Argument : void 00152 ////////////////////////////////////////////////////////////// 00153 inline Time_Span Time_Out::Remaining() const 00154 { 00155 return Remaining(Time_Clock::GetCurrentTime()); 00156 } 00157 00158 #endif //__TIME_OUT_H__