Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74627 - in trunk/boost/chrono/stopwatches: . memories reporters
From: vicente.botet_at_[hidden]
Date: 2011-10-01 16:48:26


Author: viboes
Date: 2011-10-01 16:48:25 EDT (Sat, 01 Oct 2011)
New Revision: 74627
URL: http://svn.boost.org/trac/boost/changeset/74627

Log:
Chrono: Rename lap_memory by las_collector
Text files modified:
   trunk/boost/chrono/stopwatches/basic_stopwatch.hpp | 265 +++++++++++++++++++++++++++++++++++----
   trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp | 2
   trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp | 2
   trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp | 41 +++---
   4 files changed, 258 insertions(+), 52 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 16:48:25 EDT (Sat, 01 Oct 2011)
@@ -9,9 +9,10 @@
 
 #include <utility>
 
-//#include <boost/chrono/chrono.hpp>
+#include <boost/chrono/config.hpp>
 #include <boost/chrono/stopwatches/stopwatch_scoped.hpp>
 #include <boost/chrono/stopwatches/memories/no_memory.hpp>
+#include <boost/chrono/system_clocks.hpp>
 #include <boost/system/error_code.hpp>
 
 namespace boost
@@ -19,71 +20,175 @@
   namespace chrono
   {
 
+ /**
+ * Type used to don't start a basic_stopwatch at construction time.
+ */
     struct dont_start_t
     {
     };
+
+ /**
+ * Instance used to don't start a basic_stopwatch at construction time.
+ */
     static const dont_start_t dont_start =
     { };
 
- template<typename Clock, typename LapsMemory=no_memory<typename Clock::duration> >
+ /**
+ * A basic_stopwatch is a model of @c Stopwatch taking as parameters the @c Clock and the @c LapsCollector.
+ *
+ * The main difference respect to a @c simple_stopwatch is that the user can stop it.
+ * Each sequence of start-stop results in a new elapsed duration sample that is provided to the LapsCollector.
+ *
+ * It is up to the LapsCollector to make whatever wants with each sample.
+ * A LapCollector must define a store(duration const&) and a clear() functions.
+ *
+ * The library provides LapsCollectors that forget the sample, store the
+ * last one, cummulates the samples in an accumulator set or store them in a container.
+ * For simplicity the default LapCollector is the one that forget the samples.
+ *
+ * Even if it is preferable to use process or thread wide clocks,
+ * the default of the Clock parameter is high_resolution_clock,
+ * as it is the single one ensured on all platforms.
+ */
+ template<typename Clock=high_resolution_clock, typename LapsCollector=no_memory<typename Clock::duration> >
     class basic_stopwatch
     {
     public:
- typedef LapsMemory laps_memory;
+ typedef LapsCollector laps_collector;
       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;
+ BOOST_CHRONO_STATIC_CONSTEXPR bool is_steady = Clock::is_steady;
 
+ /**
+ * Default constructor.
+ *
+ * Effects: Starts the stopwatch.
+ * Post-conditions: is_running().
+ */
+ explicit basic_stopwatch()
+ :
+ start_(duration::zero()),
+ running_(false),
+ laps_collector_()
+ {
+ start();
+ }
+
+ /**
+ * Default constructor.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Effects: Starts the stopwatch.
+ * Post-conditions: is_running() if no error occur.
+ */
       explicit basic_stopwatch(
- system::error_code & ec = BOOST_CHRONO_THROWS
+ system::error_code & ec
           ) :
         start_(duration::zero()),
         running_(false),
- storage_()
+ laps_collector_()
       {
         start(ec);
       }
+
+ /**
+ * Not starting constructor.
+ *
+ * Effects: Don't starts the stopwatch.
+ * Post-conditions: ! is_running() if no error occur.
+ */
       explicit basic_stopwatch(
           const dont_start_t&
           ) :
         start_(duration::zero()),
         running_(false),
- storage_()
+ laps_collector_()
       {
       }
 
+ /**
+ * Starting constructor from a LapsCollector instance.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Effects: Copies the LapsCollector. Starts the stopwatch.
+ * Post-conditions: is_running() if no error occur.
+ *
+ * Remark: The LapsCollector is copied and owned by the stopwatch.
+ */
       explicit basic_stopwatch(
- laps_memory const& acc,
+ laps_collector const& acc,
           system::error_code & ec = BOOST_CHRONO_THROWS
           ) :
         start_(duration::zero()),
         running_(false),
- storage_(acc)
+ laps_collector_(acc)
       {
         start(ec);
       }
 
+ /**
+ * Not starting constructor from a LapsCollector instance.
+ *
+ * Effects: Copies the LapsCollector. Don't starts the stopwatch.
+ * Post-conditions: ! is_running() if no error occur.
+ *
+ * Remark: The LapsCollector is copied and owned by the stopwatch.
+ */
       basic_stopwatch(
- laps_memory const& acc,
+ laps_collector const& acc,
           const dont_start_t&
           ) :
         start_(duration::zero()),
         running_(false),
- storage_(acc)
+ laps_collector_(acc)
       {
       }
 
+ /**
+ * Destructor.
+ *
+ * Effects: Do nothing.
+ */
       ~basic_stopwatch()
       {
- system::error_code ec;
- stop(ec);
       }
 
+ /**
+ * Restart the stopwatch.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Effects: As if stop(); start() were called, but ensuring that the start time is the same as the stop time.
+ *
+ * Post-conditions: is_running() if no error occur.
+ */
+ void restart()
+ {
+ time_point tmp = clock::now();
+
+ if (is_running())
+ {
+ laps_collector_.store(tmp - start_);
+ }
+ else
+ {
+ running_ = true;
+ }
+ start_ = tmp;
+ }
+
+ /**
+ * Restart the stopwatch.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Effects: As if stop(); start() were called, but ensuring that the start time is the same as the stop time.
+ *
+ * Post-conditions: is_running() if no error occur.
+ */
       void restart(
- system::error_code & ec = BOOST_CHRONO_THROWS
+ system::error_code & ec
           )
       {
         time_point tmp = clock::now(ec);
@@ -91,7 +196,7 @@
 
         if (is_running())
         {
- storage_.store(tmp - start_);
+ laps_collector_.store(tmp - start_);
         }
         else
         {
@@ -100,8 +205,29 @@
         start_ = tmp;
       }
 
+ /**
+ * Start the stopwatch.
+ *
+ * Effects: Memorize the current time.
+ *
+ * Post-conditions: is_running().
+ */
+ void start()
+ {
+ start_ = clock::now();
+ running_ = true;
+ }
+
+ /**
+ * Start the stopwatch.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Effects: Memorize the current time.
+ *
+ * Post-conditions: is_running() if no error occur.
+ */
       void start(
- system::error_code & ec = BOOST_CHRONO_THROWS
+ system::error_code & ec
           )
       {
           time_point tmp = clock::now(ec);
@@ -111,8 +237,38 @@
           running_ = true;
       }
 
+
+ /**
+ * Start the stopwatch.
+ *
+ * Effects: Gives the elapsed time since start time to the LapCollector.
+ *
+ * Throws: Any exception that the LapCollector can throw when .
+ *
+ * Post-conditions: !is_running() if no error occur.
+ */
+ void stop()
+ {
+ if (is_running())
+ {
+ laps_collector_.store(clock::now() - start_);
+ start_ = time_point(duration::zero());
+ running_ = false;
+ }
+ }
+
+ /**
+ * Start the stopwatch.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Effects: Gives the elapsed time since start time to the LapCollector if no internal error occurs.
+ *
+ * Throws: Any exception that the LapCollector can Throw.
+ *
+ * Post-conditions: !is_running() if no error occur.
+ */
       void stop(
- system::error_code & ec = BOOST_CHRONO_THROWS
+ system::error_code & ec
           )
       {
         if (is_running())
@@ -120,18 +276,49 @@
           time_point tmp = clock::now(ec);
           if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
 
- storage_.store(tmp - start_);
+ laps_collector_.store(tmp - start_);
           start_ = time_point(duration::zero());
           running_ = false;
         }
       }
 
+ /**
+ * States if the Stopwatch is running.
+ */
       bool is_running() const {
         return running_;
       }
 
+ /**
+ * Elapsed time getter.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ *
+ * Returns: the elapsed time since the start if no internal error occur.
+ *
+ */
+ duration elapsed() const
+ {
+ if (is_running())
+ {
+ return clock::now() - start_;
+ }
+ else
+ {
+ return duration::zero();
+ }
+ }
+
+ /**
+ * Elapsed time getter.
+ *
+ * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ *
+ * Returns: the elapsed time since the start if no internal error occur.
+ *
+ */
       duration elapsed(
- system::error_code & ec = BOOST_CHRONO_THROWS
+ system::error_code & ec
           ) const
       {
         if (is_running())
@@ -145,32 +332,48 @@
           return duration::zero();
         }
       }
-
-
- void reset(
- )
+ /**
+ * Resets the stopwatch.
+ *
+ * Effects: Resets the LapCollector.
+ *
+ * Post-conditions: !is_running() if no error occur.
+ *
+ */
+ void reset()
       {
 
- storage_.reset();
+ laps_collector_.reset();
         running_ = false;
         start_ = time_point(duration::zero());
       }
 
- laps_memory const& get_laps_memory()
- {
- return storage_;
+ /**
+ * LapsCollector getter.
+ *
+ * Returns: the LapCollector instance.
+ *
+ */
+ laps_collector const& get_laps_collector() BOOST_CHRONO_NOEXCEPT
+ {
+ return laps_collector_;
       }
 
-
- typedef stopwatch_runner<basic_stopwatch<Clock, LapsMemory> >
+ /**
+ * Useful typedef for scoped run
+ */
+ typedef stopwatch_runner<basic_stopwatch<Clock, LapsCollector> >
           scoped_run;
- typedef stopwatch_stopper<basic_stopwatch<Clock, LapsMemory> >
+ /**
+ * Useful typedef for scoped stop
+ */
+ typedef stopwatch_stopper<basic_stopwatch<Clock, LapsCollector> >
           scoped_stop;
 
     private:
       time_point start_;
       bool running_;
- laps_memory storage_;
+ laps_collector laps_collector_;
     };
 
   } // namespace chrono

Modified: trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp (original)
+++ trunk/boost/chrono/stopwatches/memories/laps_accumulator_set.hpp 2011-10-01 16:48:25 EDT (Sat, 01 Oct 2011)
@@ -42,11 +42,13 @@
         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_; }
 
 

Modified: trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp (original)
+++ trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp 2011-10-01 16:48:25 EDT (Sat, 01 Oct 2011)
@@ -56,7 +56,7 @@
         formatter_(fmt), reported_(false)
       {
       }
- explicit basic_stopwatch_reporter(formatter_type const& fmt) :
+ explicit basic_stopwatch_reporter(formatter_type fmt) :
         formatter_(fmt), reported_(false)
       {
       }

Modified: trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp (original)
+++ trunk/boost/chrono/stopwatches/suspendable_stopwatch.hpp 2011-10-01 16:48:25 EDT (Sat, 01 Oct 2011)
@@ -9,10 +9,11 @@
 
 #include <utility>
 
-//#include <boost/chrono/chrono.hpp>
+#include <boost/chrono/config.hpp>
 #include <boost/chrono/stopwatches/stopwatch_scoped.hpp>
 #include <boost/chrono/stopwatches/memories/no_memory.hpp>
 #include <boost/system/error_code.hpp>
+#include <boost/chrono/system_clocks.hpp>
 
 namespace boost
 {
@@ -25,24 +26,24 @@
     static const dont_start_t dont_start =
     { };
 
- template<typename Clock, typename LapsMemory=no_memory<typename Clock::duration> >
+ template<typename Clock=high_resolution_clock, typename LapsCollector=no_memory<typename Clock::duration> >
     class suspendable_stopwatch
     {
     public:
- typedef LapsMemory laps_memory;
+ typedef LapsCollector laps_collector;
       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;
+ 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_(),
+ laps_collector_(),
         suspended_(false),
         partial_(duration::zero())
       {
@@ -53,19 +54,19 @@
           ) :
           start_(duration::zero()),
           running_(false),
- storage_(),
+ laps_collector_(),
           suspended_(false),
           partial_(duration::zero())
       {
       }
 
       explicit suspendable_stopwatch(
- laps_memory const& acc,
+ laps_collector const& acc,
           system::error_code & ec = BOOST_CHRONO_THROWS
           ) :
           start_(duration::zero()),
           running_(false),
- storage_(acc),
+ laps_collector_(acc),
           suspended_(false),
           partial_(duration::zero())
       {
@@ -73,12 +74,12 @@
       }
 
       suspendable_stopwatch(
- laps_memory const& acc,
+ laps_collector const& acc,
           const dont_start_t&
           ) :
             start_(duration::zero()),
             running_(false),
- storage_(acc),
+ laps_collector_(acc),
             suspended_(false),
             partial_(duration::zero())
       {
@@ -100,7 +101,7 @@
         if (running_)
         {
           partial_ += tmp - start_;
- storage_.store(partial_);
+ laps_collector_.store(partial_);
           partial_ = duration::zero();
         }
         else
@@ -130,7 +131,7 @@
           if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
 
           partial_ += tmp - start_;
- storage_.store(partial_);
+ laps_collector_.store(partial_);
           start_ = time_point(duration::zero());
           running_ = false;
           suspended_ = false;
@@ -210,31 +211,31 @@
       void reset(
           )
       {
- storage_.reset();
+ laps_collector_.reset();
         running_ = false;
         suspended_ = false;
         partial_ = duration::zero();
         start_ = time_point(duration::zero());
       }
 
- laps_memory const& get_laps_memory()
+ laps_collector const& get_laps_memory()
       {
- return storage_;
+ return laps_collector_;
       }
 
 
- typedef stopwatch_runner<suspendable_stopwatch<Clock, LapsMemory> >
+ typedef stopwatch_runner<suspendable_stopwatch<Clock, LapsCollector> >
           scoped_run;
- typedef stopwatch_stopper<suspendable_stopwatch<Clock, LapsMemory> >
+ typedef stopwatch_stopper<suspendable_stopwatch<Clock, LapsCollector> >
           scoped_stop;
- typedef stopwatch_suspender<suspendable_stopwatch<Clock, LapsMemory> >
+ typedef stopwatch_suspender<suspendable_stopwatch<Clock, LapsCollector> >
           scoped_suspend;
- typedef stopwatch_resumer<suspendable_stopwatch<Clock, LapsMemory> >
+ typedef stopwatch_resumer<suspendable_stopwatch<Clock, LapsCollector> >
           scoped_resume;
     private:
       time_point start_;
       bool running_;
- laps_memory storage_;
+ laps_collector laps_collector_;
       bool suspended_;
       duration partial_;
     };


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