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