source/tool/timer.cxx

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  The Contents of this file are made available subject to
00004  *  the terms of GNU Lesser General Public License Version 2.1.
00005  *
00006  *
00007  *    GNU Lesser General Public License Version 2.1
00008  *    =============================================
00009  *    Copyright 2005 by Kohei Yoshida.
00010  *    1039 Kingsway Dr., Apex, NC 27502, USA
00011  *
00012  *    This library is free software; you can redistribute it and/or
00013  *    modify it under the terms of the GNU Lesser General Public
00014  *    License version 2.1, as published by the Free Software Foundation.
00015  *
00016  *    This library is distributed in the hope that it will be useful,
00017  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *    Lesser General Public License for more details.
00020  *
00021  *    You should have received a copy of the GNU Lesser General Public
00022  *    License along with this library; if not, write to the Free Software
00023  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  *    MA  02111-1307  USA
00025  *
00026  ************************************************************************/
00027 
00028 #include "tool/timer.hxx"
00029 #ifndef _WIN32
00030 #include <sys/time.h>
00031 #else
00032 #include <windows.h>
00033 #endif
00034 
00035 namespace scsolver {
00036 
00037 class TimerImpl
00038 {
00039 public:
00040         TimerImpl( double duration ) :
00041                 m_fDuration(duration)
00042         {
00043         }
00044 
00045         ~TimerImpl() throw()
00046         {
00047         }
00048 
00049         void init()
00050         {
00051                 m_fCurTime = getTime();
00052         }
00053 
00054         bool isTimedOut()
00055         {
00056                 return (getTime() - m_fCurTime) > m_fDuration;
00057         }
00058 
00059 private:
00060 
00066         double getTime()
00067         {
00068 #ifndef _WIN32
00069                 timeval tv;
00070                 gettimeofday(&tv, NULL);
00071                 return tv.tv_sec + tv.tv_usec / 1000000.0;
00072 #else
00073                 FILETIME ft;
00074                 __int64 *time64 = (__int64 *) &ft;
00075 
00076                 GetSystemTimeAsFileTime (&ft);
00077 
00078                 /* Convert from 100s of nanoseconds since 1601-01-01
00079                  * to seconds since the Unix epoch.
00080                  */
00081                 *time64 -= 116444736000000000i64;
00082                 return *time64 / 10000000.0;
00083 #endif
00084         }
00085 
00086         double m_fDuration;
00087         double m_fCurTime;
00088 };
00089 
00090 //-----------------------------------------------------------------
00091 
00092 /*
00093 Timer::Timer()
00094 {
00095         // disabled
00096 }
00097 */
00098 
00099 Timer::Timer( double duration ) :
00100         m_pImpl( new TimerImpl(duration) )
00101 {
00102 }
00103 
00104 Timer::~Timer() throw()
00105 {
00106 }
00107 
00108 void Timer::init()
00109 {
00110         m_pImpl->init();
00111 }
00112 
00113 bool Timer::isTimedOut() const
00114 {
00115         return m_pImpl->isTimedOut();
00116 }
00117 
00118 }

Generated on Mon Jul 28 09:13:20 2008 for scsolver by  doxygen 1.5.3