00001 /* 00002 * Timer.cpp 00003 * 00004 * Created on: Oct 27, 2011 00005 * Author: mriedel 00006 */ 00007 00008 #include <telekyb_base/Time/Timer.hpp> 00009 00010 namespace TELEKYB_NAMESPACE { 00011 00012 Timer::Timer(){ 00013 // gettimeofday(¤t,NULL); 00014 // _start = Time(current.tv_sec,current.tv_usec); 00015 _start = Time(); // now 00016 _accu = Time(0,0); 00017 _pause = false; 00018 } 00019 00020 00021 Timer::Timer(const Time& elapsed){ 00022 // gettimeofday(¤t,NULL); 00023 // _start = Time(current.tv_sec,current.tv_usec); 00024 _start = Time(); // now 00025 _accu = elapsed; 00026 _pause = false; 00027 } 00028 00029 00030 Timer::Timer(const Timer& t) { 00031 _start = t._start; 00032 _accu = t._accu; 00033 _pause = t._pause; 00034 }; 00035 00036 Timer::~Timer() { 00037 00038 } 00039 00040 Timer& Timer::operator=(const Timer& t){ 00041 if (this != &t){ 00042 _start = t._start; 00043 _accu = t._accu; 00044 _pause = t._pause; 00045 } 00046 return *this; 00047 } 00048 00049 00050 void Timer::reset(){ 00051 *this = Timer(); 00052 } 00053 00054 00055 void Timer::pause(){ 00056 if(!_pause){ 00057 // ?? NOT needed. 00058 // gettimeofday(¤t,NULL); 00059 // Time now(current.tv_sec,current.tv_usec); 00060 Time now; 00061 _accu += now - _start; 00062 _pause = true; 00063 } 00064 } 00065 00066 00067 void Timer::resume(){ 00068 if(_pause){ 00069 //gettimeofday(¤t,NULL); 00070 //_start = Time(current.tv_sec,current.tv_usec); 00071 _start = Time(); 00072 _pause = false; 00073 } 00074 } 00075 00076 00077 Time Timer::getElapsed() const { 00078 if(_pause){ 00079 return _accu; 00080 }else{ 00081 // gettimeofday(¤t,NULL); 00082 // Time now(current.tv_sec,current.tv_usec); 00083 Time now; 00084 return now - _start + _accu; 00085 } 00086 } 00087 00088 00089 std::string Timer::toString(){ 00090 // stringstream s; 00091 // Time state=get(); 00092 // s << state.print() ; 00093 Time elapsed = getElapsed(); 00094 return elapsed.toString(); 00095 } 00096 00097 00098 double Timer::frequency(){ 00099 Time elapsed = getElapsed(); 00100 return elapsed.frequency(); 00101 } 00102 00103 00104 std::string Timer::freqToString(){ 00105 Time elapsed = getElapsed(); 00106 return elapsed.freqToString(); 00107 } 00108 00109 } // end namespace