Panda3D

time_out.h
1 #ifndef __TIME_OUT_H__
2 #define __TIME_OUT_H__
3
4 // think of this class as a time based alarm.. would be nice to have a
5 // template implementation of this class .. could avoud some storage and some
6 // math .. I would do this but not sure how to represent the duration in the
7 // template ??
8 class Time_Out
9 {
10 public:
11 Time_Out()
12 {
13 }
14
15 Time_Out(const Time_Span & dur) : _alarm_time(Time_Clock::GetCurrentTime() + dur) , _duration(dur)
16 {
17 }
18 /*
19 Time_Out(const Time_Clock & tm, const Time_Span & dur) : _alarm_time(tm + dur) , _duration(dur)
20 {
21 }
22 */
23 void ResetAll(const Time_Clock &tm, const Time_Span &sp);
24 void ReStart();
25 void ResetTime(const Time_Clock & tm);
26 void SetTimeOutSec(int sec);
27
28 bool Expired(const Time_Clock &tm, bool reset = false);
29 bool Expired(bool reset = false);
30
31 Time_Span Remaining(const Time_Clock & tm) const;
32 Time_Span Remaining() const;
33
34 void ForceToExpired()
35 {
36 _alarm_time.ToCurrentTime();
37 }
38
39 bool operator() (bool reset= false)
40 {
41 return Expired(reset);
42 }
43 bool operator() (const Time_Clock &tm, bool reset = false)
44 {
45 return Expired(tm, reset);
46 }
47
48 Time_Clock GetAlarm(void)
49 {
50 return _alarm_time;
51 }
52
53 Time_Span Duration() const { return _duration; };
54
55 void NextInStep(Time_Clock &curtime)
56 {
57 _alarm_time += _duration;
58 if(_alarm_time <=curtime) // if we fall way behind.. just ratchet it up ...
59 _alarm_time = curtime+_duration;
60 }
61 private:
62 Time_Clock _alarm_time;
63 Time_Span _duration;
64 };
65 /**
66 *
67 */
68 inline void Time_Out::ResetAll(const Time_Clock &tm, const Time_Span &sp)
69 {
70 _duration = sp;
71 _alarm_time = tm + _duration;
72 }
73 /**
74 *
75 */
76 inline void Time_Out::SetTimeOutSec(int sec)
77 {
78 _duration.Set(0, 0, 0, sec, 0);
79 ReStart();
80 }
81 /**
82 *
83 */
84 inline void Time_Out::ReStart()
85 {
86 _alarm_time = Time_Clock::GetCurrentTime() + _duration;
87 }
88 /**
89 *
90 */
91 inline void Time_Out::ResetTime(const Time_Clock & tm)
92 {
93 _alarm_time = tm + _duration;
94
95 }
96 /**
97 *
98 */
99 inline bool Time_Out::Expired(const Time_Clock &tm, bool reset)
100 {
101 bool answer = (_alarm_time <= tm) ;
102 if (answer && reset)
103 ResetTime(tm);
104 return answer;
105 }
106 /**
107 *
108 */
109 inline bool Time_Out::Expired(bool reset)
110 {
111 return Expired(Time_Clock::GetCurrentTime(), reset);
112 }
113 /**
114 *
115 */
116 inline Time_Span Time_Out::Remaining(const Time_Clock & tm) const
117 {
118 return _alarm_time - tm;
119 }
120 /**
121 *
122 */
123 inline Time_Span Time_Out::Remaining() const
124 {
125 return Remaining(Time_Clock::GetCurrentTime());
126 }
127
128 #endif //__TIME_OUT_H__
Time_Clock
This class is to provide a consistant interface and storage to clock time
Definition: time_clock.h:14
Time_Clock::GetCurrentTime
static Time_Clock GetCurrentTime()
The Default no param constructor.
Definition: time_clock.h:128
Time_Clock::ToCurrentTime
void ToCurrentTime()
Load this object with the current OS time.
Definition: time_clock.h:144
Time_Out
Definition: time_out.h:9
Time_Span
Definition: time_span.h:7