00001 /* 00002 Copyright (C) 2010 - 2014 Christoph Haenisch 00003 00004 Chair of Medical Engineering (mediTEC) 00005 RWTH Aachen University 00006 Pauwelsstr. 20 00007 52074 Aachen 00008 Germany 00009 00010 See license.txt for more information. 00011 00012 Version 0.1.3 (2019-06-12) 00013 */ 00014 00021 #include "TRTK/Clock.hpp" 00022 #include "TRTK/ErrorObj.hpp" 00023 00024 00025 namespace TRTK 00026 { 00027 00028 00031 Clock::Clock() : m_time_when_paused(0), state(RUNNING) 00032 { 00033 m_time = std::clock(); 00034 } 00035 00036 00055 double Clock::elapsed_time() const 00056 { 00057 switch(state) 00058 { 00059 case RUNNING: 00060 return static_cast<double>(std::clock() - m_time) / CLOCKS_PER_SEC; 00061 break; 00062 00063 case PAUSED: 00064 return static_cast<double>(m_time_when_paused) / CLOCKS_PER_SEC; 00065 break; 00066 00067 default: 00068 ErrorObj error; 00069 error.setClassName("Clock"); 00070 error.setFunctionName("elapsed_time"); 00071 error.setErrorCode(UNKNOWN_STATE); 00072 throw error; 00073 } 00074 } 00075 00076 00085 void Clock::pause() 00086 { 00087 switch(state) 00088 { 00089 case RUNNING: 00090 m_time_when_paused = std::clock() - m_time; 00091 state = PAUSED; 00092 break; 00093 00094 case PAUSED: 00095 // do nothing 00096 break; 00097 } 00098 } 00099 00100 00106 void Clock::reset() 00107 { 00108 m_time = std::clock(); 00109 m_time_when_paused = 0; 00110 } 00111 00112 00121 void Clock::resume() 00122 { 00123 switch(state) 00124 { 00125 case RUNNING: 00126 // do nothing 00127 break; 00128 00129 case PAUSED: 00130 m_time = std::clock() - m_time_when_paused; 00131 state = RUNNING; 00132 break; 00133 } 00134 } 00135 00136 00151 void Clock::wait_milliseconds(const double time) const 00152 { 00153 clock_t start_time = std::clock(); 00154 00155 while (static_cast<double>(std::clock() - start_time) / CLOCKS_PER_SEC < time / 1000.0) 00156 { 00157 // do nothing 00158 } 00159 } 00160 00161 00176 void Clock::wait_seconds(const double time) const 00177 { 00178 clock_t start_time = std::clock(); 00179 00180 while (static_cast<double>(std::clock() - start_time) / CLOCKS_PER_SEC < time) 00181 { 00182 // do nothing 00183 } 00184 } 00185 00186 00188 00189 00209 std::ostream & operator<< (std::ostream & output, Clock & clock) 00210 { 00211 output << "Elapsed time: " << clock.elapsed_time() << " seconds." << std::endl; 00212 return output; 00213 } 00214 00215 00216 } // namespace TRTK
Documentation generated by Doxygen