Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
timingTools.hpp
00001 /* $Id$ */
00002 
00003 #ifndef KERNEL_TIMINGTOOLS_HPP
00004 #define KERNEL_TIMINGTOOLS_HPP
00005 
00006 #include "boost/date_time/posix_time/posix_time.hpp"
00007 // we could use a better singleton here...
00008 #include "boost/serialization/singleton.hpp"
00009 #include <unistd.h>
00010 #include <sys/time.h>
00011 
00012 #include "kernel/jafarDebug.hpp"
00013 
00014 
00015 namespace jafar {
00016   namespace kernel {
00017 
00018     class Timestamp
00019     {
00020       private:
00021         /*long*/ double data;
00022       public:
00023         Timestamp(): data(-1.0) {}
00024         Timestamp(double param): data(param) {}
00025         //Timestamp(long double param): data(param) {}
00026         operator double() const { return data; }
00027         //operator long double() const { return data; }
00028         friend std::ostream& operator<<(std::ostream& os, const jafar::kernel::Timestamp &t);
00029     };
00030     
00031     std::ostream& operator<<(std::ostream& os, const Timestamp &t);
00032     
00033     
00039     class Chrono {
00040 
00041     private :
00042 
00043       boost::posix_time::ptime refTime;
00044 
00045     public :
00046 
00048       Chrono() {
00049   reset();
00050       }
00051 
00053       void reset() {
00054   refTime = boost::posix_time::microsec_clock::local_time();
00055       }
00056 
00058       long elapsed() {
00059   boost::posix_time::ptime curTime = boost::posix_time::microsec_clock::local_time();
00060   return (curTime - refTime).total_milliseconds();
00061       }
00062 
00064       long elapsedSecond() {
00065   boost::posix_time::ptime curTime = boost::posix_time::microsec_clock::local_time();
00066   return (curTime - refTime).total_seconds();
00067       }
00068 
00070       long elapsedMicrosecond() {
00071   boost::posix_time::ptime curTime = boost::posix_time::microsec_clock::local_time();
00072   return (curTime - refTime).total_microseconds();
00073       }
00074       
00075 
00076     }; // class Chrono
00077 
00078 
00080     class Clock
00081     {
00082       public:
00083       static double getTime()
00084       {
00085         // FIXME should use boost::posix_time
00086         //boost::posix_time::ptime curTime = boost::posix_time::microsec_clock::local_time();
00087         //return curTime.total_seconds() + curTime.fractional_seconds() / time_duration::ticks_per_second();
00088         struct timeval tv; struct timezone tz;
00089         gettimeofday(&tv, &tz);
00090         return tv.tv_sec + tv.tv_usec*1e-6;
00091       }
00092       
00093     };
00094 
00095     class Timer
00096     {
00097       private:
00098         double period;
00099         double last_tic_time;
00100       public:
00101         Timer(int period_us): period(period_us*1e-6) { restart(); }
00102         void restart()  { last_tic_time = Clock::getTime(); }
00103         void wait()
00104         {
00105           // FIXME should use nanosleep
00106           double current_time = Clock::getTime();
00107           last_tic_time += period;
00108           double wait_time = last_tic_time - current_time;
00109           if (wait_time < 0)
00110             last_tic_time = current_time;
00111           else
00112             usleep(wait_time*1e6);
00113         }
00114         
00115     };
00116 
00117 #ifndef SWIG
00118 
00119     void tic();
00120     long toc();
00121 
00122     namespace detail {
00123 
00128       class TicTocChrono : public boost::serialization::singleton<TicTocChrono> {
00129       private:
00130   
00131 
00132       Chrono chrono;
00133 
00134       static TicTocChrono& instance() {
00135         return boost::serialization::singleton<TicTocChrono>::get_mutable_instance(); 
00136       }
00137 
00138       // necessary because the constructor is private
00139 
00140       friend void jafar::kernel::tic();
00141       friend long jafar::kernel::toc();
00142       };
00143     } // namespace detail
00144 
00145 #endif // SWIG
00146     
00151     class FrameRate {
00152       
00153     public:
00154 
00155       enum TypeUpdate{
00156           ontime,
00157           onframe
00158       };
00159 
00165       FrameRate(TypeUpdate upd_ = ontime, int updTimeInt_ = 2000, int updFrameInt_ = 10);
00166       
00169       void updateFps();
00170 
00173       float getFps();
00174 
00175     private:
00176       TypeUpdate upd;
00177       boost::posix_time::ptime lastUpdate;
00178       boost::posix_time::time_duration fpsUpdateInterval;
00179       int numFramesInterval;
00180       int  numFrames;
00181       float fps;
00182     }; // class FrameRate
00183 
00184   } // namespace kernel
00185 } // namespace jafar
00186 
00187 
00188 #endif // KERNEL_TIMINGTOOLS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS