00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef MLN_UTIL_TIMER_HH
00027 # define MLN_UTIL_TIMER_HH
00028 
00032 
00033 # include <mln/core/concept/proxy.hh>
00034 # include <ctime>
00035 
00036 
00037 namespace mln
00038 {
00039 
00040   namespace util
00041   {
00042 
00044     class timer : public Proxy< timer >,
00045                   public mln::internal::proxy_impl<float, timer>
00046     {
00047     public:
00048 
00049       timer();
00050 
00051       
00052       timer(const timer&);
00053       void operator=(const timer&);
00054 
00055       ~timer();
00056 
00057       void start();
00058 
00059       float stop();
00060 
00061       void resume();
00062 
00063       void reset();
00064 
00065       
00066       void restart();
00067 
00068       float read() const;
00069 
00070       
00071       float subj_();
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082     private:
00083 
00084       bool running_;
00085       float start_;
00086       float time_;
00087     };
00088 
00089 
00090 # ifndef MLN_INCLUDE_ONLY
00091 
00092     inline
00093     timer::timer()
00094     {
00095       reset();
00096     }
00097 
00098     inline
00099     timer::~timer()
00100     {
00101       reset();
00102     }
00103 
00104     inline
00105     void
00106     timer::start()
00107     {
00108       mln_precondition(running_ == false);
00109       start_ = float(std::clock()) / CLOCKS_PER_SEC;
00110       time_ = 0;
00111       running_ = true;
00112     }
00113 
00114     inline
00115     float
00116     timer::stop()
00117     {
00118       mln_precondition(running_ == true);
00119       time_ += float(std::clock()) / CLOCKS_PER_SEC - start_;
00120       running_ = false;
00121       return time_;
00122     }
00123 
00124     inline
00125     void
00126     timer::resume()
00127     {
00128       mln_precondition(running_ == false);
00129       start_ = float(std::clock()) / CLOCKS_PER_SEC;
00130       running_ = true;
00131     }
00132 
00133     inline
00134     void
00135     timer::reset()
00136     {
00137       running_ = false;
00138       start_ = -1;
00139       time_ = 0;
00140     }
00141 
00142     inline
00143     void
00144     timer::restart()
00145     {
00146       reset();
00147       start();
00148     }
00149 
00150 
00151     inline
00152     float
00153     timer::read() const
00154     {
00155       mln_precondition(start_ != -1);
00156       return running_ ?
00157         time_ + float(std::clock()) / CLOCKS_PER_SEC - start_ :
00158         time_;
00159     }
00160 
00161     inline
00162     float
00163     timer::subj_()
00164     {
00165       mln_precondition(start_ != -1);
00166       return read();
00167     }
00168 
00169 # endif // ! MLN_INCLUDE_ONLY
00170 
00171   } 
00172 
00173 } 
00174 
00175 
00176 #endif // ! MLN_UTIL_TIMER_HH