00001 /*----------------------------------------------------------------------------- 00002 Name: StopWatch.h 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Handling the Client data 00006 -----------------------------------------------------------------------------*/ 00007 00008 #ifndef _UTIL_STOPWATCH_H 00009 #define _UTIL_STOPWATCH_H 00010 00011 #include <util/XmlBCfg.h> 00012 #if !defined(WINCE) 00013 #include <sys/timeb.h> 00014 #endif 00015 #include <string> 00016 #include <util/lexical_cast.h> 00017 00018 00019 namespace org { namespace xmlBlaster { 00020 namespace util { 00021 00029 class Dll_Export StopWatch { 00030 00031 private: 00032 #if defined(WINCE) 00033 #else 00034 struct timeb *startTime_, *currentTime_; 00035 #endif 00036 long stopTime_; 00037 00038 public: 00039 StopWatch(long stopTime=-1) { 00040 #if defined(WINCE) 00041 #else 00042 startTime_ = new timeb(); 00043 currentTime_ = new timeb(); 00044 restart(stopTime); 00045 #endif 00046 } 00047 00048 00049 ~StopWatch() { 00050 #if defined(WINCE) 00051 #else 00052 delete startTime_; 00053 delete currentTime_; 00054 #endif 00055 } 00056 00057 00063 long elapsed() const { 00064 #if defined(WINCE) 00065 return 0; 00066 #else 00067 ftime(currentTime_); 00068 double seconds = difftime(currentTime_->time, startTime_->time); 00069 double milliSec = 00070 (double)currentTime_->millitm - (double)startTime_->millitm; 00071 return (long)(1000.0 * seconds + milliSec); 00072 #endif //(WINCE) 00073 } 00074 00075 00076 00082 std::string nice() { 00083 std::string ret = toString(); 00084 restart(); 00085 return ret; 00086 } 00087 00088 00095 std::string toString() const { 00096 return std::string("elapsed time: ") + lexical_cast<std::string>(elapsed()) + " milliseconds"; 00097 } 00098 00099 00104 void restart(long stopTime=-1) { 00105 stopTime_ = stopTime; 00106 #if defined(WINCE) 00107 #else 00108 ftime(startTime_); 00109 #endif 00110 } 00111 00118 bool isRunning() const { 00119 if (stopTime_ < 0) return true; // always running 00120 return (stopTime_ > elapsed()); 00121 } 00122 00123 00128 void wait(long millisec) { 00129 restart(millisec); 00130 while (isRunning()) { 00131 } 00132 } 00133 }; 00134 }}} // namespace 00135 00136 #endif 00137