Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74749 - in trunk/boost/chrono: io stopwatches stopwatches/collectors
From: vicente.botet_at_[hidden]
Date: 2011-10-05 23:47:25


Author: viboes
Date: 2011-10-05 23:47:24 EDT (Wed, 05 Oct 2011)
New Revision: 74749
URL: http://svn.boost.org/trac/boost/changeset/74749

Log:
Chrono: elapsed takes in account the elapsed time stored on the LapsCollector + added time_point time_fmt and io_savers.
Text files modified:
   trunk/boost/chrono/io/time_point_io.hpp | 143 ++++++++++++++++++++++++++++++++++++++++
   trunk/boost/chrono/stopwatches/basic_stopwatch.hpp | 55 +++++++++++++-
   trunk/boost/chrono/stopwatches/collectors/laps_accumulator_set.hpp | 2
   trunk/boost/chrono/stopwatches/collectors/laps_sequence_container.hpp | 8 ++
   trunk/boost/chrono/stopwatches/collectors/last_lap.hpp | 1
   trunk/boost/chrono/stopwatches/collectors/no_memory.hpp | 1
   6 files changed, 203 insertions(+), 7 deletions(-)

Modified: trunk/boost/chrono/io/time_point_io.hpp
==============================================================================
--- trunk/boost/chrono/io/time_point_io.hpp (original)
+++ trunk/boost/chrono/io/time_point_io.hpp 2011-10-05 23:47:24 EDT (Wed, 05 Oct 2011)
@@ -142,11 +142,154 @@
 
     }
 
+ template<class CharT>
+ inline
+ detail::time_manip<CharT>
+ time_fmt(timezone_type tz, const CharT* fmt)
+ {
+ return detail::time_manip<CharT>(tz, fmt);
+ }
+
+ template<class CharT>
+ inline
+ detail::time_manip<CharT>
+ time_fmt(timezone tz, std::basic_string<CharT> fmt)
+ {
+ // todo move semantics
+ return detail::time_manip<CharT>(tz, fmt);
+ }
+
     inline detail::time_man time_fmt(timezone_type f)
     {
       return detail::time_man(f);
     }
 
+ /**
+ * time_fmt_io_saver i/o saver.
+ *
+ * See Boost.IO i/o state savers for a motivating compression.
+ */
+ template<typename CharT = char, typename Traits = std::char_traits<CharT> >
+ struct time_fmt_io_saver
+ {
+
+ //! the type of the state to restore
+ typedef std::basic_ostream<CharT, Traits> state_type;
+ //! the type of aspect to save
+ typedef std::basic_string<CharT, Traits> aspect_type;
+
+ /**
+ * Explicit construction from an i/o stream.
+ *
+ * Store a reference to the i/o stream and the value of the associated @c time format .
+ */
+ explicit time_fmt_io_saver(state_type &s) :
+ s_save_(s)
+ {
+ typedef duration_punct<CharT> Facet;
+ std::locale loc = s_save_.getloc();
+ if (!std::has_facet<Facet>(loc))
+ s_save_.imbue(std::locale(loc, new Facet()));
+
+ const Facet& f = std::use_facet<Facet>(s_save_.getloc());
+ a_save_ = f.get_duration_style();
+ }
+
+ /**
+ * Construction from an i/o stream and a @c time format to restore.
+ *
+ * Stores a reference to the i/o stream and the value @c new_value to restore given as parameter.
+ */
+ time_fmt_io_saver(state_type &s, aspect_type new_value) :
+ s_save_(s), a_save_(new_value)
+ {
+ }
+
+ /**
+ * Destructor.
+ *
+ * Restores the i/o stream with the format to be restored.
+ */
+ ~time_fmt_io_saver()
+ {
+ this->restore();
+ }
+
+ /**
+ * Restores the i/o stream with the time format to be restored.
+ */
+ void restore()
+ {
+ s_save_ << time_fmt(a_save_);
+ }
+ private:
+ state_type& s_save_;
+ aspect_type a_save_;
+ };
+
+ /**
+ * timezone_io_saver i/o saver.
+ *
+ * See Boost.IO i/o state savers for a motivating compression.
+ */
+ template<typename CharT = char, typename Traits = std::char_traits<CharT> >
+ struct timezone_io_saver
+ {
+
+ //! the type of the state to restore
+ typedef std::basic_ostream<CharT, Traits> state_type;
+ //! the type of aspect to save
+ typedef timezone_type aspect_type;
+
+ /**
+ * Explicit construction from an i/o stream.
+ *
+ * Store a reference to the i/o stream and the value of the associated @c timezone.
+ */
+ explicit timezone_io_saver(state_type &s) :
+ s_save_(s)
+ {
+ typedef duration_punct<CharT> Facet;
+ std::locale loc = s_save_.getloc();
+ if (!std::has_facet<Facet>(loc))
+ s_save_.imbue(std::locale(loc, new Facet()));
+
+ const Facet& f = std::use_facet<Facet>(s_save_.getloc());
+ a_save_ = f.get_duration_style();
+ }
+
+ /**
+ * Construction from an i/o stream and a @c timezone to restore.
+ *
+ * Stores a reference to the i/o stream and the value @c new_value to restore given as parameter.
+ */
+ timezone_io_saver(state_type &s, aspect_type new_value) :
+ s_save_(s), a_save_(new_value)
+ {
+ }
+
+ /**
+ * Destructor.
+ *
+ * Restores the i/o stream with the format to be restored.
+ */
+ ~timezone_io_saver()
+ {
+ this->restore();
+ }
+
+ /**
+ * Restores the i/o stream with the timezone to be restored.
+ */
+ void restore()
+ {
+ s_save_ << time_fmt(a_save_);
+ }
+ private:
+ state_type& s_save_;
+ aspect_type a_save_;
+ };
+
     template<class CharT, class Traits, class Clock, class Duration>
     std::basic_ostream<CharT, Traits>&
     operator<<(std::basic_ostream<CharT, Traits>& os, const time_point<Clock,

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-05 23:47:24 EDT (Wed, 05 Oct 2011)
@@ -297,14 +297,12 @@
       }
 
       /**
- * Elapsed time getter.
- *
- * Effects: Assign the error code if any internal error occur while retrieving the current time.
+ * Elapsed time getter for the current lap.
        *
- * Returns: the elapsed time since the start if no internal error occur.
+ * Returns: the elapsed time since the last start if no internal error occur.
        *
        */
- duration elapsed() const
+ duration elapsed_current_lap() const
       {
         if (is_running())
         {
@@ -317,14 +315,14 @@
       }
 
       /**
- * Elapsed time getter.
+ * Elapsed time getter for the current lap.
        *
        * 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(
+ duration elapsed_current_lap(
           system::error_code & ec
           ) const
       {
@@ -339,6 +337,49 @@
           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() const
+ {
+ return laps_collector_.elapsed()+elapsed_current_lap();
+ }
+
+ /**
+ * 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
+ ) const
+ {
+ duration tmp = elapsed_current_lap(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
+ return laps_collector_.elapsed() + tmp;
+ }
+
+ /**
+ * Elapsed time for the last lap.
+ *
+ * Returns: the elapsed time of the last lap.
+ *
+ */
+
+ duration last() const
+ {
+ return laps_collector_.last();
+ }
       /**
        * Resets the stopwatch.
        *

Modified: trunk/boost/chrono/stopwatches/collectors/laps_accumulator_set.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/collectors/laps_accumulator_set.hpp (original)
+++ trunk/boost/chrono/stopwatches/collectors/laps_accumulator_set.hpp 2011-10-05 23:47:24 EDT (Wed, 05 Oct 2011)
@@ -16,6 +16,7 @@
 #include <boost/accumulators/statistics/max.hpp>
 #include <boost/accumulators/statistics/mean.hpp>
 #include <boost/accumulators/accumulators.hpp>
+#include <boost/accumulators/framework/features.hpp>
 
 namespace boost
 {
@@ -51,6 +52,7 @@
 
       storage_type const& accumulator_set() const { return acc_; }
 
+ duration elapsed() const { return duration(accumulators::sum(acc_)); }
 
     };
 

Modified: trunk/boost/chrono/stopwatches/collectors/laps_sequence_container.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/collectors/laps_sequence_container.hpp (original)
+++ trunk/boost/chrono/stopwatches/collectors/laps_sequence_container.hpp 2011-10-05 23:47:24 EDT (Wed, 05 Oct 2011)
@@ -24,6 +24,8 @@
       typedef Duration duration;
       typedef typename duration::rep rep;
       typedef SequenceContainer storage_type;
+ typedef typename SequenceContainer::iterator iterator;
+ typedef typename SequenceContainer::const_iterator const_iterator;
       storage_type cont_;
 
       void store(duration const& d)
@@ -45,6 +47,12 @@
           return *cont_.begin();
       }
 
+ duration elapsed() const {
+ duration elapsed_ = duration::zero();
+ for (const_iterator it = cont_.begin(); it !=cont_.end(); ++it) elapsed_ += *it;
+ return elapsed_;
+ }
+
     };
 
 

Modified: trunk/boost/chrono/stopwatches/collectors/last_lap.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/collectors/last_lap.hpp (original)
+++ trunk/boost/chrono/stopwatches/collectors/last_lap.hpp 2011-10-05 23:47:24 EDT (Wed, 05 Oct 2011)
@@ -28,6 +28,7 @@
         last_ = duration::zero();
       }
       duration last() const { return last_; }
+ duration elapsed() const { return duration::zero(); }
 
     };
 

Modified: trunk/boost/chrono/stopwatches/collectors/no_memory.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/collectors/no_memory.hpp (original)
+++ trunk/boost/chrono/stopwatches/collectors/no_memory.hpp 2011-10-05 23:47:24 EDT (Wed, 05 Oct 2011)
@@ -19,6 +19,7 @@
     {
       typedef Duration duration;
 
+ duration elapsed() const { return duration::zero(); }
       duration last() const { return duration::zero(); }
       void store(duration const& ) {}
       void reset() {}


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