Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgTimer.h

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


17 Jun 2010 Doxygen 1.4.7