Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59227 - sandbox/chrono/boost/chrono
From: vicente.botet_at_[hidden]
Date: 2010-01-22 13:33:46


Author: viboes
Date: 2010-01-22 13:33:45 EST (Fri, 22 Jan 2010)
New Revision: 59227
URL: http://svn.boost.org/trac/boost/changeset/59227

Log:
Boost.Chrono: Version 0.3.2,
* added scoped_stopper
* add kind running or running_and_idle so stopwatch_accumulator can provide just run times or also idle times
*
Text files modified:
   sandbox/chrono/boost/chrono/stopwatch.hpp | 1
   sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp | 217 +++++++++++++++++++++++++++++----------
   sandbox/chrono/boost/chrono/stopwatch_reporter.hpp | 1
   sandbox/chrono/boost/chrono/stopwatch_scoped.hpp | 26 ++++
   4 files changed, 186 insertions(+), 59 deletions(-)

Modified: sandbox/chrono/boost/chrono/stopwatch.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch.hpp 2010-01-22 13:33:45 EST (Fri, 22 Jan 2010)
@@ -161,6 +161,7 @@
         }
 
         typedef stopwatch_runner<stopwatch<Clock> > scoped_run;
+ typedef stopwatch_stopper<stopwatch<Clock> > scoped_stop;
         typedef stopwatch_suspender<stopwatch<Clock> > scoped_suspend;
         typedef stopwatch_resumer<stopwatch<Clock> > scoped_resume;
         typedef stopwatch_reporter<stopwatch<Clock> > reporter;

Modified: sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_accumulator.hpp 2010-01-22 13:33:45 EST (Fri, 22 Jan 2010)
@@ -25,10 +25,7 @@
 
 #include <boost/config/abi_prefix.hpp> // must be the last #include
 
-namespace boost
-{
- namespace chrono
- {
+namespace boost { namespace chrono {
 
 //--------------------------------------------------------------------------------------//
 // stopwatch
@@ -45,8 +42,106 @@
 //~ but the watch mechanism continues running to record total elapsed time.
 //--------------------------------------------------------------------------------------//
 
+namespace kind {
+struct idle {};
+struct running {};
+struct running_and_idle {};
+}
+namespace detail {
+template <
+ class Clock,
+ class RunningIdleKind,
+ class Accumulator
+>
+struct basic_stopwatch_accumulator;
+
+template <
+ class Clock,
+ class Accumulator
+>
+class basic_stopwatch_accumulator<Clock, kind::running, Accumulator> {
+public:
+ typedef Clock clock;
+ typedef typename Clock::duration duration;
+ typedef typename Clock::time_point time_point;
+ typedef Accumulator accumulator;
+
+ accumulator& accumulated( ) { return accumulated_; }
+
+protected:
+ basic_stopwatch_accumulator( )
+ : running_(false), suspended_(false), accumulated_(),
+ partial_(duration::zero()), start_((duration::zero())), level_(0), suspend_level_(0)
+ {}
+
+ void idle_on_start(time_point &) {}
+ void idle_on_stop(time_point &) {}
+
+ bool running_;
+ bool suspended_;
+ accumulator accumulated_;
+ duration partial_;
+ time_point start_;
+ std::size_t level_;
+ std::size_t suspend_level_;
+
+};
+
+
+template <
+ class Clock,
+ class Accumulator
+>
+class basic_stopwatch_accumulator<Clock, kind::running_and_idle, Accumulator>
+ : public basic_stopwatch_accumulator<Clock, kind::running, Accumulator>
+{
+ typedef basic_stopwatch_accumulator<Clock, kind::running, Accumulator> base_type;
+public:
+ typedef Clock clock;
+ typedef typename Clock::duration duration;
+ typedef typename Clock::time_point time_point;
+ typedef Accumulator accumulator;
+
+ accumulator& idle_accumulated( ) { return idle_accumulated_; }
+
+protected:
+ basic_stopwatch_accumulator( )
+ : base_type(),
+ stopped_(false), idle_accumulated_(), idle_partial_(duration::zero()), stop_(duration::zero())
+ {}
+
+ void idle_on_start(time_point &tmp) {
+ if (stopped_) {
+ idle_partial_ += tmp - this->start_;
+ idle_accumulated_(idle_partial_.count());
+ idle_partial_=duration::zero();
+ }
+ }
+
+ void idle_on_stop(time_point &tmp) {
+ stop_ = tmp;
+ stopped_=true;
+ }
+
+ void idle_on_reset() {
+ stopped_=false;
+ idle_accumulated_ = accumulator();
+ idle_partial_=duration::zero();
+ stop_ = time_point(duration::zero());
+ }
+
+ bool stopped_;
+ accumulator idle_accumulated_;
+ duration idle_partial_;
+ time_point stop_;
+};
+
+}
+
+
     // forward declaration
     template <class Clock=high_resolution_clock,
+ class RunningIdleKind=kind::running,
         class Accumulator=accumulators::accumulator_set<typename Clock::duration::rep,
                     accumulators::features<
                         accumulators::tag::count,
@@ -64,71 +159,79 @@
     };
 
 //--------------------------------------------------------------------------------------//
- template <class Clock, class Accumulator>
- class stopwatch_accumulator
+ template <class Clock, class RunningIdleKind, class Accumulator >
+ class stopwatch_accumulator
+ : public detail::basic_stopwatch_accumulator<Clock, RunningIdleKind, Accumulator>
     {
+ typedef detail::basic_stopwatch_accumulator<Clock, RunningIdleKind, Accumulator> base_type;
     public:
         typedef Clock clock;
         typedef typename Clock::duration duration;
         typedef typename Clock::time_point time_point;
         typedef Accumulator accumulator;
 
- stopwatch_accumulator( )
- : running_(false), suspended_(false), accumulated_(),
- partial_(), start_(duration::zero()), level_(0), suspend_level_(0)
- {}
+ stopwatch_accumulator( ) : base_type() {}
 
         std::pair<duration, time_point> restart( system::error_code & ec = system::throws ) {
             time_point tmp=clock::now( ec );
             if (ec) return time_point();
- if (running_&&(--level_==0)) {
- partial_ += tmp - start_;
- accumulated_(partial_.count());
- partial_=duration::zero();
+ if (this->running_&&(--this->level_==0)) {
+ this->partial_ += tmp - this->start_;
+ this->accumulated_(this->partial_.count());
+ this->partial_=duration::zero();
             } else {
- running_=true;
+ this->running_=true;
             }
- start_=tmp;
- ++level_;
- return std::make_pair(duration(accumulators::sum(accumulated_)),start_);
+ this->start_=tmp;
+ ++this->level_;
+ return std::make_pair(duration(accumulators::sum(this->accumulated_)),this->start_);
         }
 
         time_point start( system::error_code & ec = system::throws ) {
- ++level_;
- if (!running_) {
+ if (!this->running_) {
                 time_point tmp = clock::now( ec );
                 if (ec) return time_point();
- start_ = tmp;
- running_ = true;
- return start_;
+ ++this->level_;
+ this->start_ = tmp;
+ this->running_ = true;
+ this->idle_on_start(tmp);
+ return this->start_;
             } else {
+ ++this->level_;
                 return time_point();
             }
         }
 
         duration stop( system::error_code & ec = system::throws ) {
- if (running_&&(--level_==0)) {
- time_point tmp=clock::now( ec );
- if (ec) return duration::zero();
- partial_ += tmp - start_;
- accumulated_(partial_.count());
- partial_=duration::zero();
- running_=false;
- return duration(accumulators::extract::sum(accumulated_));
+ if (this->running_) {
+ if (this->level_==1) {
+ time_point tmp=clock::now( ec );
+ if (ec) return duration::zero();
+ --this->level_;
+ this->partial_ += tmp - this->start_;
+ this->accumulated_(this->partial_.count());
+ this->partial_=duration::zero();
+ this->running_=false;
+ this->idle_on_stop(tmp);
+ return duration(accumulators::extract::sum(this->accumulated_));
+ } else {
+ --this->level_;
+ return duration::zero();
+ }
             } else {
                 return duration::zero();
             }
         }
 
         duration suspend( system::error_code & ec = system::throws ) {
- if (running_) {
- ++suspend_level_;
- if (!suspended_) {
+ if (this->running_) {
+ ++this->suspend_level_;
+ if (!this->suspended_) {
                     time_point tmp=clock::now( ec );
                     if (ec) return duration::zero();
- partial_ += tmp - start_;
- suspended_=true;
- return duration(accumulators::sum(accumulated_));
+ this->partial_ += tmp - this->start_;
+ this->suspended_=true;
+ return duration(accumulators::sum(this->accumulated_));
                 } else {
                     return duration::zero();
                 }
@@ -137,51 +240,47 @@
             }
         }
         time_point resume( system::error_code & ec = system::throws ) {
- if (suspended_&&(--suspend_level_==0)) {
+ if (this->suspended_&&(--this->suspend_level_==0)) {
                 time_point tmp = clock::now( ec );
                 if (ec) return time_point();
- start_ = tmp;
- suspended_=false;
- return start_;
+ this->start_ = tmp;
+ this->suspended_=false;
+ return this->start_;
             } else {
                 return time_point();
             }
         }
         duration elapsed( system::error_code & ec = system::throws )
         {
- if (running_) {
- if (suspended_)
- return duration(accumulators::sum(accumulated_));
+ if (this->running_) {
+ if (this->suspended_)
+ return duration(accumulators::sum(this->accumulated_));
                 else {
                     time_point tmp = clock::now( ec );
                     if (ec) return duration::zero();
- return duration(accumulators::sum(accumulated_))+tmp - start_;
+ return duration(accumulators::sum(this->accumulated_))+tmp - this->start_;
                 }
             } else {
- return duration(accumulators::sum(accumulated_));
+ return duration(accumulators::sum(this->accumulated_));
             }
         }
 
         void reset( ) {
- time_point tmp = time_point();
- start_ = tmp;
- accumulator tmp2;
- accumulated_ = tmp2;
+ this->running_=false;
+ this->suspended_=false;
+ this->accumulated_ = accumulator();
+ this->partial_=duration::zero();
+ this->start_ = time_point(duration::zero());
+ this->level_=0;
+ this->suspend_level_=0;
+ this->idle_on_reset();
         }
- accumulator& accumulated( ) { return accumulated_; }
 
         typedef stopwatch_runner<stopwatch_accumulator<Clock> > scoped_run;
+ typedef stopwatch_stopper<stopwatch_accumulator<Clock> > scoped_stop;
         typedef stopwatch_suspender<stopwatch_accumulator<Clock> > scoped_suspend;
         typedef stopwatch_resumer<stopwatch_accumulator<Clock> > scoped_resume;
         typedef stopwatch_reporter<stopwatch_accumulator<Clock> > reporter;
- private:
- bool running_;
- bool suspended_;
- accumulator accumulated_;
- duration partial_;
- time_point start_;
- std::size_t level_;
- std::size_t suspend_level_;
     };
 
 //--------------------------------------------------------------------------------------//

Modified: sandbox/chrono/boost/chrono/stopwatch_reporter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_reporter.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_reporter.hpp 2010-01-22 13:33:45 EST (Fri, 22 Jan 2010)
@@ -142,6 +142,7 @@
 
 
         typedef stopwatch_runner<stopwatch_reporter<Stopwatch> > scoped_run;
+ typedef stopwatch_stopper<stopwatch_reporter<Stopwatch> > scoped_stop;
         typedef stopwatch_suspender<stopwatch_reporter<Stopwatch> > scoped_suspend;
         typedef stopwatch_resumer<stopwatch_reporter<Stopwatch> > scoped_resume;
 

Modified: sandbox/chrono/boost/chrono/stopwatch_scoped.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_scoped.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_scoped.hpp 2010-01-22 13:33:45 EST (Fri, 22 Jan 2010)
@@ -47,6 +47,32 @@
     };
 
 //--------------------------------------------------------------------------------------//
+ template <class Stopwatch> class stopwatch_stopper {
+ public:
+ typedef Stopwatch stopwatch;
+ stopwatch_stopper(stopwatch & a, system::error_code & ec = system::throws)
+ : stopwatch_(a) {
+ stopwatch_.stop(ec);
+ }
+ ~stopwatch_stopper() {
+ system::error_code ec;
+ stopwatch_.start(ec);
+ }
+#if 0
+ typename Stopwatch::duration elapsed(system::error_code & ec = system::throws)
+ {
+ return stopwatch_.elapsed(ec)-stopwatch_.accumulated();
+ }
+#endif
+ private:
+ stopwatch& stopwatch_;
+ stopwatch_stopper();//= delete;
+ stopwatch_stopper(const stopwatch_stopper&); // = delete;
+ stopwatch_stopper& operator=(const stopwatch_stopper&); // = delete;
+
+ };
+
+//--------------------------------------------------------------------------------------//
     template <class Stopwatch> class stopwatch_suspender {
     public:
         typedef Stopwatch stopwatch;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk