Panda3D
 All Classes Functions Variables Enumerations
time_accumulator.h
00001 #ifndef __TIME_ACCUMULATOR_H__
00002 #define __TIME_ACCUMULATOR_H__
00003 ///////////////////////////////////////////
00004 //
00005 // Think of this as a stopwatch that can be restarted. 
00006 //
00007 ///////////////////////////////////////////
00008 class Time_Accumulator
00009 {
00010 public:
00011     Time_Accumulator();
00012     ~Time_Accumulator();
00013 
00014     void Start();
00015     void Stop();
00016     void Reset();
00017     void Set(const Time_Span & in);
00018 
00019     Time_Span  Report();
00020 private:
00021 
00022     Time_Span       _total_time;    // the collected time from previous start/stops
00023     Time_Clock      *_accum_start;  // the time of day the clock started
00024 };
00025 
00026 //////////////////////////////////////////////////////////
00027 // you can set the internal accumilator to a value..
00028 ////////////////////////////////////////////////////////
00029 inline void Time_Accumulator::Set(const Time_Span & in)
00030 {        
00031     _total_time = in;
00032     //
00033     // this seems to make the most since .. 
00034     //  if you are running the clock right know... assume the timespane you 
00035     //   are passing in is inclusive.. but keep clock running..
00036     //  
00037     //      May need to rethink this...
00038     //
00039     if(_accum_start != NULL) 
00040     {
00041         Stop();
00042         Start();
00043     }
00044 }
00045 //////////////////////////////////////////////////////////////
00046 // Function name    : Time_Accumulator::Time_Accumulator
00047 // Description      : 
00048 //////////////////////////////////////////////////////////////
00049 inline Time_Accumulator::Time_Accumulator() : _total_time(0,0,0,0,0), _accum_start(NULL)
00050 {
00051 }
00052 //////////////////////////////////////////////////////////////
00053 // Function name    : Time_Accumulator::~Time_Accumulator
00054 // Description      : 
00055 //////////////////////////////////////////////////////////////
00056 inline Time_Accumulator::~Time_Accumulator()
00057 {
00058     if(_accum_start != NULL)
00059         delete _accum_start;
00060 }
00061 //////////////////////////////////////////////////////////////
00062 // Function name    :    void Time_Accumulator::Start
00063 // Description      : 
00064 //////////////////////////////////////////////////////////////
00065 inline     void Time_Accumulator::Start() 
00066 {   
00067     if(_accum_start == NULL)
00068         _accum_start = new Time_Clock();
00069 }
00070 //////////////////////////////////////////////////////////////
00071 // Function name    :    void Time_Accumulator::Stop
00072 // Description      : 
00073 //////////////////////////////////////////////////////////////
00074 inline     void Time_Accumulator::Stop()
00075 {
00076     if(_accum_start != NULL)
00077     {
00078          Time_Span   work1(Time_Clock::GetCurrentTime() - *_accum_start);
00079         _total_time += work1;
00080         delete _accum_start;
00081         _accum_start = NULL;
00082     }
00083 }
00084 //////////////////////////////////////////////////////////////
00085 // Function name    : Time_Accumulator::Reset
00086 // Description      : 
00087 //////////////////////////////////////////////////////////////
00088 void Time_Accumulator::Reset()
00089 {
00090     if(_accum_start != NULL)
00091     {            
00092         delete _accum_start;
00093         _accum_start = NULL;
00094     }
00095     _total_time.Set(0,0,0,0,0);
00096 }
00097 //////////////////////////////////////////////////////////////
00098 // Function name    :  Time_Accumulator::Report
00099 // Description      : 
00100 //////////////////////////////////////////////////////////////
00101 inline Time_Span  Time_Accumulator::Report() 
00102 {
00103     Time_Span   answer(_total_time);
00104     if(_accum_start != NULL)
00105     {
00106         Time_Span ww(Time_Clock::GetCurrentTime() - *_accum_start);    
00107         answer += ww;
00108     }
00109     return answer;
00110 }
00111 
00112 #endif //__TIME_ACCUMULATOR_H__
00113 
 All Classes Functions Variables Enumerations