Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74623 - in trunk/boost/chrono/stopwatches: . memories
From: vicente.botet_at_[hidden]
Date: 2011-10-01 12:49:37


Author: viboes
Date: 2011-10-01 12:49:36 EDT (Sat, 01 Oct 2011)
New Revision: 74623
URL: http://svn.boost.org/trac/boost/changeset/74623

Log:
Chrono: Simplify basic_stopwatch and extract the suspend/resume part. Added different models of SampleCollectors-Memories
Added:
   trunk/boost/chrono/stopwatches/memories/
   trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/memories/last_lap.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/memories/no_memory.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp (contents, props changed)
Text files modified:
   trunk/boost/chrono/stopwatches/basic_stopwatch.hpp | 184 +++++++++------------------------------
   1 files changed, 45 insertions(+), 139 deletions(-)

Modified: trunk/boost/chrono/stopwatches/basic_stopwatch.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/basic_stopwatch.hpp (original)
+++ trunk/boost/chrono/stopwatches/basic_stopwatch.hpp 2011-10-01 12:49:36 EDT (Sat, 01 Oct 2011)
@@ -11,6 +11,7 @@
 
 //#include <boost/chrono/chrono.hpp>
 #include <boost/chrono/stopwatches/stopwatch_scoped.hpp>
+#include <boost/chrono/stopwatches/memories/no_memory.hpp>
 #include <boost/system/error_code.hpp>
 
 namespace boost
@@ -24,59 +25,54 @@
     static const dont_start_t dont_start =
     { };
 
- template<typename Clock, typename Memory>
+ template<typename Clock, typename LapsMemory=no_memory<typename Clock::duration> >
     class basic_stopwatch
     {
     public:
- typedef Memory storage_type;
+ typedef LapsMemory laps_memory;
       typedef Clock clock;
       typedef typename Clock::duration duration;
       typedef typename Clock::time_point time_point;
       typedef typename Clock::rep rep;
       typedef typename Clock::period period;
+ BOOST_CHRONO_STATIC_CONSTEXPR bool is_steady = Clock::is_steady;
 
       explicit basic_stopwatch(
           system::error_code & ec = BOOST_CHRONO_THROWS
           ) :
- running_(false), suspended_(false), start_(duration::zero()),
- level_(0), partial_(duration::zero()), suspend_level_(0),
- storage_(), construction_(clock::now(ec))
+ start_(duration::zero()),
+ running_(false),
+ storage_()
       {
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
-
         start(ec);
       }
       explicit basic_stopwatch(
- const dont_start_t&,
- system::error_code & ec = BOOST_CHRONO_THROWS
+ const dont_start_t&
           ) :
- running_(false), suspended_(false), start_(duration::zero()),
- level_(0), partial_(duration::zero()), suspend_level_(0),
- storage_(), construction_(clock::now(ec))
+ start_(duration::zero()),
+ running_(false),
+ storage_()
       {
       }
 
       explicit basic_stopwatch(
- storage_type const& acc,
+ laps_memory const& acc,
           system::error_code & ec = BOOST_CHRONO_THROWS
           ) :
- running_(false), suspended_(false), start_(duration::zero()),
- level_(0), partial_(duration::zero()), suspend_level_(0),
- storage_(acc), construction_(clock::now(ec))
+ start_(duration::zero()),
+ running_(false),
+ storage_(acc)
       {
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
-
         start(ec);
       }
 
       basic_stopwatch(
- storage_type const& acc,
- const dont_start_t&,
- system::error_code & ec = BOOST_CHRONO_THROWS
+ laps_memory const& acc,
+ const dont_start_t&
           ) :
- running_(false), suspended_(false), start_(duration::zero()),
- level_(0), partial_(duration::zero()), suspend_level_(0),
- storage_(acc), construction_(clock::now(ec))
+ start_(duration::zero()),
+ running_(false),
+ storage_(acc)
       {
       }
 
@@ -86,185 +82,95 @@
         stop(ec);
       }
 
- std::pair<duration, time_point> restart(
+ void restart(
           system::error_code & ec = BOOST_CHRONO_THROWS
           )
       {
         time_point tmp = clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return time_point();
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
 
- if (running_ && (level_ == 1))
+ if (is_running())
         {
- partial_ += tmp - start_;
- storage_.store(partial_);
- partial_ = duration::zero();
- } else
+ storage_.store(tmp - start_);
+ }
+ else
         {
           running_ = true;
         }
         start_ = tmp;
- return std::make_pair(storage_.elapsed(), start_);
       }
 
- time_point start(
+ void start(
           system::error_code & ec = BOOST_CHRONO_THROWS
           )
       {
- if (!running_)
- {
           time_point tmp = clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return time_point();
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
 
           start_ = tmp;
- ++level_;
           running_ = true;
- return start_;
- } else
- {
- ++level_;
- ec.clear();
- return time_point();
- }
       }
 
- duration stop(
+ void stop(
           system::error_code & ec = BOOST_CHRONO_THROWS
           )
       {
- if (running_ && (--level_ == 0))
+ if (is_running())
         {
           time_point tmp = clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
 
- partial_ += tmp - start_;
- storage_.store(partial_);
- partial_ = duration::zero();
+ storage_.store(tmp - start_);
+ start_ = time_point(duration::zero());
           running_ = false;
- return storage_.elapsed();
- } else
- {
- ec.clear();
- return duration::zero();
- }
- }
-
- duration suspend(
- system::error_code & ec = BOOST_CHRONO_THROWS
- )
- {
- if (running_)
- {
- if (!suspended_)
- {
- time_point tmp = clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
-
- ++suspend_level_;
- partial_ += tmp - start_;
- suspended_ = true;
- return storage_.elapsed();
- } else
- {
- ++suspend_level_;
- ec.clear();
- return duration::zero();
- }
- } else
- {
- ec.clear();
- return duration::zero();
         }
       }
 
- time_point resume(
- system::error_code & ec = BOOST_CHRONO_THROWS
- )
- {
- if (suspended_ && (--suspend_level_ == 0))
- {
- time_point tmp = clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return time_point();
-
- start_ = tmp;
- suspended_ = false;
- return start_;
- } else
- {
- ec.clear();
- return time_point();
- }
+ bool is_running() const {
+ return running_;
       }
 
       duration elapsed(
           system::error_code & ec = BOOST_CHRONO_THROWS
- )
+ ) const
       {
- if (running_)
+ if (is_running())
         {
- if (suspended_)
- return storage_.elapsed();
- else
- {
             time_point tmp = clock::now(ec);
             if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
 
- return storage_.elapsed() + tmp - start_;
- }
+ return tmp - start_;
         } else
         {
- return storage_.elapsed();
+ return duration::zero();
         }
       }
 
 
       void reset(
- system::error_code & ec = BOOST_CHRONO_THROWS
           )
       {
- construction_ = clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
 
         storage_.reset();
         running_ = false;
- suspended_ = false;
- partial_ = duration::zero();
         start_ = time_point(duration::zero());
- level_ = 0;
- suspend_level_ = 0;
       }
 
- storage_type const& get_storage()
+ laps_memory const& get_laps_memory()
       {
         return storage_;
       }
 
- duration lifetime(
- system::error_code & ec = BOOST_CHRONO_THROWS
- )
- {
- typename clock::time_point tmp= clock::now(ec);
- if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
-
- return tmp - construction_;
- }
 
- typedef stopwatch_runner<basic_stopwatch<Clock, Memory> >
+ typedef stopwatch_runner<basic_stopwatch<Clock, LapsMemory> >
           scoped_run;
- typedef stopwatch_stopper<basic_stopwatch<Clock, Memory> >
+ typedef stopwatch_stopper<basic_stopwatch<Clock, LapsMemory> >
           scoped_stop;
- typedef stopwatch_suspender<basic_stopwatch<Clock, Memory> >
- scoped_suspend;
- typedef stopwatch_resumer<basic_stopwatch<Clock, Memory> >
- scoped_resume;
+
     private:
- bool running_;
- bool suspended_;
       time_point start_;
- std::size_t level_;
- duration partial_;
- std::size_t suspend_level_;
- storage_type storage_;
- time_point construction_;
+ bool running_;
+ laps_memory storage_;
     };
 
   } // namespace chrono

Added: trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp 2011-10-01 12:49:36 EDT (Sat, 01 Oct 2011)
@@ -0,0 +1,62 @@
+// boost/chrono/stopwatches/stopwatch_reporter.hpp
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or
+// copy at http://www.boost.org/LICENSE_1_0.txt)
+// See http://www.boost.org/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_MEMORIES_LAPS_ACCUMULETOR_SET_HPP
+#define BOOST_CHRONO_STOPWATCHES_MEMORIES_LAPS_ACCUMULETOR_SET_HPP
+
+#include <boost/chrono/stopwatches/memories/last_lap.hpp>
+#include <boost/accumulators/framework/accumulator_set.hpp>
+#include <boost/accumulators/statistics/count.hpp>
+#include <boost/accumulators/statistics/sum.hpp>
+#include <boost/accumulators/statistics/min.hpp>
+#include <boost/accumulators/statistics/max.hpp>
+#include <boost/accumulators/statistics/mean.hpp>
+#include <boost/accumulators/accumulators.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<
+ typename Duration,
+ typename Features = accumulators::features<accumulators::tag::count,
+ accumulators::tag::sum, accumulators::tag::min,
+ accumulators::tag::max, accumulators::tag::mean>,
+ typename Weight = void>
+ struct laps_accumulator_set : last_lap<Duration>
+ {
+ typedef last_lap<Duration> base_type;
+ typedef Duration duration;
+ typedef typename duration::rep rep;
+ typedef accumulators::accumulator_set<rep, Features,
+ Weight> storage_type;
+ storage_type acc_;
+
+ void store(duration const& d)
+ {
+ this->base_type::store(d);
+ acc_(d.count());
+ }
+ void reset()
+ {
+ this->base_type::reset();
+ acc_ = storage_type();
+ }
+ storage_type const& accumulator_set() const { return acc_; }
+
+
+ };
+
+
+ } // namespace chrono
+} // namespace boost
+
+
+#endif
+
+

Added: trunk/boost/chrono/stopwatches/memories/last_lap.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/memories/last_lap.hpp 2011-10-01 12:49:36 EDT (Sat, 01 Oct 2011)
@@ -0,0 +1,41 @@
+// boost/chrono/stopwatches/stopwatch_reporter.hpp
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or
+// copy at http://www.boost.org/LICENSE_1_0.txt)
+// See http://www.boost.org/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_MEMORIES_LAST_LAP_HPP
+#define BOOST_CHRONO_STOPWATCHES_MEMORIES_LAST_LAP_HPP
+
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<typename Duration>
+ struct last_lap
+ {
+ typedef Duration duration;
+ duration last_;
+ void store(duration const& d)
+ {
+ last_ = d;
+ }
+ void reset()
+ {
+ last_ = duration::zero();
+ }
+ duration last() const { return last_; }
+
+ };
+
+
+ } // namespace chrono
+} // namespace boost
+
+
+#endif
+
+

Added: trunk/boost/chrono/stopwatches/memories/no_memory.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/memories/no_memory.hpp 2011-10-01 12:49:36 EDT (Sat, 01 Oct 2011)
@@ -0,0 +1,35 @@
+// boost/chrono/stopwatches/stopwatch_reporter.hpp
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or
+// copy at http://www.boost.org/LICENSE_1_0.txt)
+// See http://www.boost.org/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_MEMORIES_NO_MEMORY_HPP
+#define BOOST_CHRONO_STOPWATCHES_MEMORIES_NO_MEMORY_HPP
+
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<typename Duration>
+ struct no_memory
+ {
+ typedef Duration duration;
+
+ duration last() const { return duration::zero(); }
+ void store(duration const& ) {}
+ void reset() {}
+
+ };
+
+
+ } // namespace chrono
+} // namespace boost
+
+
+#endif
+
+

Added: trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp 2011-10-01 12:49:36 EDT (Sat, 01 Oct 2011)
@@ -0,0 +1,245 @@
+// boost/chrono/stopwatches/suspendable_stopwatch.hpp ------------------------------------------------------------//
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+// See http://www.boost.org/libs/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_SUSPENDABLE_STOPWATCH__HPP
+#define BOOST_CHRONO_STOPWATCHES_SUSPENDABLE_STOPWATCH__HPP
+
+#include <utility>
+
+//#include <boost/chrono/chrono.hpp>
+#include <boost/chrono/stopwatches/stopwatch_scoped.hpp>
+#include <boost/chrono/stopwatches/memories/no_memory.hpp>
+#include <boost/system/error_code.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ struct dont_start_t
+ {
+ };
+ static const dont_start_t dont_start =
+ { };
+
+ template<typename Clock, typename LapsMemory=no_memory<typename Clock::duration> >
+ class suspendable_stopwatch
+ {
+ public:
+ typedef LapsMemory laps_memory;
+ typedef Clock clock;
+ typedef typename Clock::duration duration;
+ typedef typename Clock::time_point time_point;
+ typedef typename Clock::rep rep;
+ typedef typename Clock::period period;
+ BOOST_CHRONO_STATIC_CONSTEXPR bool is_steady = Clock::is_steady;
+
+ explicit suspendable_stopwatch(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ ) :
+ start_(duration::zero()),
+ running_(false),
+ storage_(),
+ suspended_(false),
+ partial_(duration::zero())
+ {
+ start(ec);
+ }
+ explicit suspendable_stopwatch(
+ const dont_start_t&
+ ) :
+ start_(duration::zero()),
+ running_(false),
+ storage_(),
+ suspended_(false),
+ partial_(duration::zero())
+ {
+ }
+
+ explicit suspendable_stopwatch(
+ laps_memory const& acc,
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ ) :
+ start_(duration::zero()),
+ running_(false),
+ storage_(acc),
+ suspended_(false),
+ partial_(duration::zero())
+ {
+ start(ec);
+ }
+
+ suspendable_stopwatch(
+ laps_memory const& acc,
+ const dont_start_t&
+ ) :
+ start_(duration::zero()),
+ running_(false),
+ storage_(acc),
+ suspended_(false),
+ partial_(duration::zero())
+ {
+ }
+
+ ~suspendable_stopwatch()
+ {
+ system::error_code ec;
+ stop(ec);
+ }
+
+ void restart(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
+
+ if (running_)
+ {
+ partial_ += tmp - start_;
+ storage_.store(partial_);
+ partial_ = duration::zero();
+ }
+ else
+ {
+ running_ = true;
+ }
+ start_ = tmp;
+ }
+
+ void start(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
+
+ partial_ = duration::zero();
+ start_ = tmp;
+ running_ = true;
+ }
+
+ void stop(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
+
+ partial_ += tmp - start_;
+ storage_.store(partial_);
+ start_ = time_point(duration::zero());
+ running_ = false;
+ suspended_ = false;
+ }
+
+ void suspend(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ if (is_running())
+ {
+ if (!suspended_)
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
+
+ partial_ += tmp - start_;
+ suspended_ = true;
+ }
+ else
+ {
+ ec.clear();
+ }
+ } else
+ {
+ ec.clear();
+ }
+ }
+
+ void resume(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ if (suspended_)
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
+
+ start_ = tmp;
+ suspended_ = false;
+ } else
+ {
+ ec.clear();
+ }
+ }
+
+ bool is_running() const {
+ return running_;
+ }
+ bool is_suspended() const {
+ return suspended_;
+ }
+
+ duration elapsed(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ ) const
+ {
+ if (is_running())
+ {
+ if (suspended_) {
+ return partial_;
+ }
+ else
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
+
+ return partial_ + tmp - start_;
+ }
+ } else
+ {
+ return duration::zero();
+ }
+ }
+
+
+ void reset(
+ )
+ {
+ storage_.reset();
+ running_ = false;
+ suspended_ = false;
+ partial_ = duration::zero();
+ start_ = time_point(duration::zero());
+ }
+
+ laps_memory const& get_laps_memory()
+ {
+ return storage_;
+ }
+
+
+ typedef stopwatch_runner<suspendable_stopwatch<Clock, LapsMemory> >
+ scoped_run;
+ typedef stopwatch_stopper<suspendable_stopwatch<Clock, LapsMemory> >
+ scoped_stop;
+ typedef stopwatch_suspender<suspendable_stopwatch<Clock, LapsMemory> >
+ scoped_suspend;
+ typedef stopwatch_resumer<suspendable_stopwatch<Clock, LapsMemory> >
+ scoped_resume;
+ private:
+ time_point start_;
+ bool running_;
+ laps_memory storage_;
+ bool suspended_;
+ duration partial_;
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+#endif


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