00001 //---------------------------------------------------------------------------- 00002 /** @file SgTimer.h 00003 Class SgTimer. */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef SG_TIMER_H 00007 #define SG_TIMER_H 00008 00009 #include "SgTime.h" 00010 00011 //---------------------------------------------------------------------------- 00012 00013 /** Timer. 00014 For checking the elapsed time, without calling SgTime::Get each time. */ 00015 class SgTimer 00016 { 00017 public: 00018 /** Constructor. 00019 Also starts the timer. */ 00020 SgTimer(); 00021 00022 /** Get elapsed time. 00023 Returns time since last start or between last start and stop if timer 00024 is stopped. */ 00025 double GetTime() const; 00026 00027 bool IsStopped() const; 00028 00029 /** Check for timeout. 00030 This function can only be used with fixed parameters per instance 00031 of SgTimer. 00032 @param maxTime 00033 @param checkFreq Do the comparison only every n calls for efficiency. 00034 @todo The timeout functionality should be extracted to a separate 00035 class SgTimeout, which takes maxTime as constructor arguments. */ 00036 bool IsTimeOut(double maxTime, std::size_t checkFreq = 16); 00037 00038 /** Reset timer. */ 00039 void Start(); 00040 00041 /** Stop timer. */ 00042 void Stop(); 00043 00044 private: 00045 bool m_isStopped; 00046 00047 bool m_isTimeOut; 00048 00049 /* For managing the frequency of calling SgTime::Get() in IsTimeOut(). */ 00050 std::size_t m_counter; 00051 00052 /* Time when we start searching. */ 00053 double m_timeStart; 00054 00055 /* Elapsed time when timer was stopped. */ 00056 double m_timeStop; 00057 00058 /** Not implemented */ 00059 SgTimer(const SgTimer& timer); 00060 }; 00061 00062 inline SgTimer::SgTimer() 00063 : m_isStopped(false), 00064 m_isTimeOut(false), 00065 m_counter(0), 00066 m_timeStart(0), 00067 m_timeStop(0) 00068 { 00069 Start(); 00070 } 00071 00072 inline double SgTimer::GetTime() const 00073 { 00074 if (m_isStopped) 00075 return m_timeStop; 00076 return (SgTime::Get() - m_timeStart); 00077 } 00078 00079 inline bool SgTimer::IsStopped() const 00080 { 00081 return m_isStopped; 00082 } 00083 00084 inline bool SgTimer::IsTimeOut(double maxTime, std::size_t checkFreq) 00085 { 00086 if (m_isTimeOut) 00087 return true; 00088 if (m_counter == 0) 00089 { 00090 double timeNow = SgTime::Get(); 00091 if (timeNow - m_timeStart > maxTime) 00092 { 00093 m_isTimeOut = true; 00094 return true; 00095 } 00096 else 00097 m_counter = checkFreq; 00098 } 00099 else 00100 --m_counter; 00101 return false; 00102 } 00103 00104 inline void SgTimer::Start() 00105 { 00106 m_timeStart = SgTime::Get(); 00107 m_isStopped = false; 00108 } 00109 00110 inline void SgTimer::Stop() 00111 { 00112 SG_ASSERT(! IsStopped()); 00113 m_timeStop = (SgTime::Get() - m_timeStart); 00114 m_isStopped = true; 00115 } 00116 00117 //---------------------------------------------------------------------------- 00118 00119 #endif // SG_TIMER_H