Panda3D
time_accumulator.h
1 #ifndef __TIME_ACCUMULATOR_H__
2 #define __TIME_ACCUMULATOR_H__
3 ///////////////////////////////////////////
4 //
5 // Think of this as a stopwatch that can be restarted.
6 //
7 ///////////////////////////////////////////
9 {
10 public:
13 
14  void Start();
15  void Stop();
16  void Reset();
17  void Set(const Time_Span & in);
18 
19  Time_Span Report();
20 private:
21 
22  Time_Span _total_time; // the collected time from previous start/stops
23  Time_Clock *_accum_start; // the time of day the clock started
24 };
25 
26 //////////////////////////////////////////////////////////
27 // you can set the internal accumilator to a value..
28 ////////////////////////////////////////////////////////
29 inline void Time_Accumulator::Set(const Time_Span & in)
30 {
31  _total_time = in;
32  //
33  // this seems to make the most since ..
34  // if you are running the clock right know... assume the timespane you
35  // are passing in is inclusive.. but keep clock running..
36  //
37  // May need to rethink this...
38  //
39  if(_accum_start != NULL)
40  {
41  Stop();
42  Start();
43  }
44 }
45 //////////////////////////////////////////////////////////////
46 // Function name : Time_Accumulator::Time_Accumulator
47 // Description :
48 //////////////////////////////////////////////////////////////
49 inline Time_Accumulator::Time_Accumulator() : _total_time(0,0,0,0,0), _accum_start(NULL)
50 {
51 }
52 //////////////////////////////////////////////////////////////
53 // Function name : Time_Accumulator::~Time_Accumulator
54 // Description :
55 //////////////////////////////////////////////////////////////
56 inline Time_Accumulator::~Time_Accumulator()
57 {
58  if(_accum_start != NULL)
59  delete _accum_start;
60 }
61 //////////////////////////////////////////////////////////////
62 // Function name : void Time_Accumulator::Start
63 // Description :
64 //////////////////////////////////////////////////////////////
65 inline void Time_Accumulator::Start()
66 {
67  if(_accum_start == NULL)
68  _accum_start = new Time_Clock();
69 }
70 //////////////////////////////////////////////////////////////
71 // Function name : void Time_Accumulator::Stop
72 // Description :
73 //////////////////////////////////////////////////////////////
74 inline void Time_Accumulator::Stop()
75 {
76  if(_accum_start != NULL)
77  {
78  Time_Span work1(Time_Clock::GetCurrentTime() - *_accum_start);
79  _total_time += work1;
80  delete _accum_start;
81  _accum_start = NULL;
82  }
83 }
84 //////////////////////////////////////////////////////////////
85 // Function name : Time_Accumulator::Reset
86 // Description :
87 //////////////////////////////////////////////////////////////
88 void Time_Accumulator::Reset()
89 {
90  if(_accum_start != NULL)
91  {
92  delete _accum_start;
93  _accum_start = NULL;
94  }
95  _total_time.Set(0,0,0,0,0);
96 }
97 //////////////////////////////////////////////////////////////
98 // Function name : Time_Accumulator::Report
99 // Description :
100 //////////////////////////////////////////////////////////////
101 inline Time_Span Time_Accumulator::Report()
102 {
103  Time_Span answer(_total_time);
104  if(_accum_start != NULL)
105  {
106  Time_Span ww(Time_Clock::GetCurrentTime() - *_accum_start);
107  answer += ww;
108  }
109  return answer;
110 }
111 
112 #endif //__TIME_ACCUMULATOR_H__
113 
static Time_Clock GetCurrentTime()
The Default no param constructor.
Definition: time_clock.h:160