• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

timer.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
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       // Without impl.
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       // Restart is equivalent to reset then start.
00066       void restart();
00067 
00068       float read() const;
00069 
00070       // As a proxy:
00071       float subj_();
00072 
00073 //       void print() const
00074 //       {
00075 //      std::cout << running_ << ' '
00076 //                << start_ << ' '
00077 //                << std::clock() << ' '
00078 //                << float(std::clock()) / CLOCKS_PER_SEC << ' '
00079 //                << time_ << std::endl;
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   } // end of namespace mln::util
00172 
00173 } // end of namespace mln
00174 
00175 
00176 #endif // ! MLN_UTIL_TIMER_HH

Generated on Thu Sep 8 2011 18:32:41 for Milena (Olena) by  doxygen 1.7.1