00001 #ifndef __TIME_OUT_H__
00002 #define __TIME_OUT_H__
00003
00004
00005
00006
00007
00008
00009
00010
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
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)
00064 _alarm_time = curtime+_duration;
00065 }
00066 private:
00067 Time_Clock _alarm_time;
00068 Time_Span _duration;
00069 };
00070
00071
00072
00073
00074
00075
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
00084
00085
00086
00087
00088 inline void Time_Out::SetTimeOutSec(int sec)
00089 {
00090 _duration.Set(0, 0, 0, sec, 0);
00091 ReStart();
00092 }
00093
00094
00095
00096
00097
00098
00099 inline void Time_Out::ReStart()
00100 {
00101 _alarm_time = Time_Clock::GetCurrentTime() + _duration;
00102 }
00103
00104
00105
00106
00107
00108
00109 inline void Time_Out::ResetTime(const Time_Clock & tm)
00110 {
00111 _alarm_time = tm + _duration;
00112
00113 }
00114
00115
00116
00117
00118
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
00129
00130
00131
00132
00133 inline bool Time_Out::Expired(bool reset)
00134 {
00135 return Expired(Time_Clock::GetCurrentTime(), reset);
00136 }
00137
00138
00139
00140
00141
00142
00143 inline Time_Span Time_Out::Remaining(const Time_Clock & tm) const
00144 {
00145 return _alarm_time - tm;
00146 }
00147
00148
00149
00150
00151
00152
00153 inline Time_Span Time_Out::Remaining() const
00154 {
00155 return Remaining(Time_Clock::GetCurrentTime());
00156 }
00157
00158 #endif //__TIME_OUT_H__