00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _Chrono_
00020 #define _Chrono_
00021
00022 #include <iostream>
00023 #include <sys/time.h>
00024 #include <assert.h>
00025
00026 namespace animal {
00027
00029
00032 class Chrono
00033 {
00034 private:
00035
00037
00038
00040 double last_action_val;
00041
00044 bool etat;
00045
00046
00048
00049 public:
00050
00052
00053
00055 inline Chrono(): last_action_val(0.0), etat(false) {}
00056
00058
00061 inline double reset()
00062 {
00063 double t=this->get_time();
00064 this->last_action_val=0;
00065 this->etat=false;
00066 return t;
00067 }
00068
00070
00073 inline double stop()
00074 {
00075 if (this->etat)
00076 {
00077 this->last_action_val=get_time();
00078 this->etat=false;
00079 }
00080
00081 return get_time();
00082 }
00083
00085
00088 inline double go()
00089 {
00090 if (!this->etat)
00091 {
00092 this->last_action_val=this->get_system_time()-this->last_action_val;
00093 this->etat=true;
00094 }
00095
00096 return get_time();
00097 }
00098
00099
00101
00104 inline double get_time() const
00105 {
00106 if (this->etat)
00107 {
00108 return this->get_system_time()-this->last_action_val;
00109 }
00110 else
00111 {
00112 return this->last_action_val;
00113 }
00114 }
00115
00117
00120 inline static double get_system_time()
00121 {
00122 timeval now;
00123 if (gettimeofday(&now,NULL)==-1) assert(false);
00124 double second=static_cast<double>(now.tv_sec);
00125 double microsecond=static_cast<double>(now.tv_usec);
00126 return second+microsecond/1.0E6;
00127 }
00128
00130
00133 inline static long get_system_second()
00134 {
00135 timeval now;
00136 if (gettimeofday(&now,NULL)==-1) assert(false);
00137 return now.tv_sec;
00138 }
00139
00141
00144 inline static long get_system_microsecond()
00145 {
00146 timeval now;
00147 if (gettimeofday(&now,NULL)==-1) assert(false);
00148 return now.tv_usec;
00149 }
00150
00152 inline std::ostream& write(std::ostream& os) const
00153 {
00154 os<<"Chrono "<<" : "<<get_time()<<std::endl;
00155 return os;
00156 }
00157
00159 inline friend std::ostream& operator<<(std::ostream& os, const Chrono& obj)
00160 {
00161 return obj.write(os);
00162 }
00163
00164
00166 };
00167
00168 }
00169
00170 #endif