Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74509 - in trunk/boost/chrono: . io stopwatches stopwatches/formatters stopwatches/reporters
From: vicente.botet_at_[hidden]
Date: 2011-09-21 18:39:39


Author: viboes
Date: 2011-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
New Revision: 74509
URL: http://svn.boost.org/trac/boost/changeset/74509

Log:
Chrono: Fix issue with duration_style and Add stopwatch_scoped.hpp basic_stopwatch.hpp
Added:
   trunk/boost/chrono/stopwatches/basic_stopwatch.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/stopwatch_scoped.hpp (contents, props changed)
Removed:
   trunk/boost/chrono/duration_style.hpp
Text files modified:
   trunk/boost/chrono/io/duration_io.hpp | 1 -
   trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp | 2 +-
   trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp | 33 +++++++++++++++++----------------
   3 files changed, 18 insertions(+), 18 deletions(-)

Deleted: trunk/boost/chrono/duration_style.hpp
==============================================================================
--- trunk/boost/chrono/duration_style.hpp 2011-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
+++ (empty file)
@@ -1,160 +0,0 @@
-// boost/chrono/duration_style.hpp ------------------------------------------------------------//
-// Copyright 2011 Vicente J. Botet Escriba
-// Distributed under the Boost Software License, Version 1.0.
-// See http://www.boost.org/LICENSE_1_0.txt
-// See http://www.boost.org/libs/chrono/stopwatches for documentation.
-
-#ifndef BOOST_CHRONO_DURATION_STYLE_HPP
-#define BOOST_CHRONO_DURATION_STYLE_HPP
-
-#include <boost/chrono/chrono_io.hpp>
-#include <boost/chrono/config.hpp>
-
-namespace boost
-{
- namespace chrono
- {
-
- /**
- * Scoped enumeration emulation stating whether the duration I/O style is long or short.
- * prefix_text means duration::rep with whatever stream/locale settings are set for it followed by a long name representing the unit
- * symbol means duration::rep with whatever stream/locale settings are set for it followed by a SI unit abbreviation
- */
- struct duration_style
- {
- enum type {
- prefix_text, symbol
- };
- };
-
-
- /**
- * duration parameterized manipulator.
- */
- class duration_fmt
- {
- duration_style::type style_;
- public:
-
- /**
- * explicit manipulator constructor from a @c duration_style
- */
- explicit duration_fmt(duration_style::type style) BOOST_CHRONO_NOEXCEPT
- : style_(style)
- {}
-
-#ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS
- /**
- * explicit conversion to the @c duration_style scoped enum.
- */
- explicit
- operator duration_style::type() const BOOST_CHRONO_NOEXCEPT
- { return style_;}
-#endif
-
- /**
- * gets the @c duration_style scoped enum.
- */
- duration_style::type get_duration_style() const BOOST_CHRONO_NOEXCEPT
- { return style_;}
- };
-
- /**
- * Change the duration_punc facet associated to the output stream depending on the duration_format style parameter.
- */
- template<class charT, class traits>
- std::basic_ostream<charT, traits>&
- operator <<(std::basic_ostream<charT, traits>& os, duration_fmt d)
- {
- if (d.get_duration_style() == duration_style::symbol)
- os << duration_short;
- else if (d.get_duration_style() == duration_style::prefix_text)
- os << duration_long;
- return os;
- }
-
- /**
- * Change the duration_punc facet associated to the input stream depending on the duration_format style parameter.
- */
- template<class charT, class traits>
- std::basic_istream<charT, traits>&
- operator >>(std::basic_istream<charT, traits>& is, duration_fmt d)
- {
- if (d.get_duration_style() == duration_style::symbol)
- is >> duration_short;
- else if (d.get_duration_style() == duration_style::prefix_text)
- is >> duration_long;
- return is;
- }
-
- /**
- * duration_style 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 duration_style_io_saver
- {
-
- //! the type of the state to restore
- typedef std::basic_ios<CharT, Traits> state_type;
- //! the type of aspect to save
- typedef duration_style::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 duration_style.
- */
- explicit duration_style_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>(loc);
- if (f.is_long_name())
- a_save_ = duration_style::prefix_text;
- else
- a_save_ = duration_style::symbol;
- }
-
- /**
- * Construction from an i/o stream and a @c duration_style to restore.
- *
- * Stores a reference to the i/o stream and the value @c duration_style to restore given as parameter.
- */
- duration_style_io_saver(state_type &s, aspect_type new_value) :
- s_save_(s), a_save_(new_value)
- {
- }
-
- /**
- * Destructor.
- *
- * Restores the i/o stream with the duration_style to be restored.
- */
- ~duration_style_io_saver()
- {
- this->restore();
- }
-
- /**
- * Restores the i/o stream with the duration_style to be restored.
- */
- void restore()
- {
- s_save_ << duration_fmt(a_save_);
- }
- private:
- state_type& s_save_;
- aspect_type a_save_;
- };
-
- } // namespace chrono
-} // namespace boost
-
-
-#endif

Modified: trunk/boost/chrono/io/duration_io.hpp
==============================================================================
--- trunk/boost/chrono/io/duration_io.hpp (original)
+++ trunk/boost/chrono/io/duration_io.hpp 2011-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
@@ -671,6 +671,5 @@
   } // chrono
 
 }
-#include <boost/chrono/duration_style.hpp>
 
 #endif // BOOST_CHRONO_CHRONO_IO_HPP

Added: trunk/boost/chrono/stopwatches/basic_stopwatch.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/basic_stopwatch.hpp 2011-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
@@ -0,0 +1,273 @@
+// boost/chrono/stopwatches/basic_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_BASIC_STOPWATCH__HPP
+#define BOOST_CHRONO_STOPWATCHES_BASIC_STOPWATCH__HPP
+
+#include <utility>
+
+//#include <boost/chrono/chrono.hpp>
+#include <boost/chrono/stopwatches/stopwatch_scoped.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 Memory>
+ class basic_stopwatch
+ {
+ public:
+ typedef Memory storage_type;
+ 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;
+
+ 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))
+ {
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
+
+ start(ec);
+ }
+ explicit basic_stopwatch(
+ const dont_start_t&,
+ 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))
+ {
+ }
+
+ explicit basic_stopwatch(
+ storage_type 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))
+ {
+ 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
+ ) :
+ running_(false), suspended_(false), start_(duration::zero()),
+ level_(0), partial_(duration::zero()), suspend_level_(0),
+ storage_(acc), construction_(clock::now(ec))
+ {
+ }
+
+ ~basic_stopwatch()
+ {
+ system::error_code ec;
+ stop(ec);
+ }
+
+ std::pair<duration, time_point> 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 (running_ && (level_ == 1))
+ {
+ partial_ += tmp - start_;
+ storage_.store(partial_);
+ partial_ = duration::zero();
+ } else
+ {
+ running_ = true;
+ }
+ start_ = tmp;
+ return std::make_pair(storage_.elapsed(), start_);
+ }
+
+ time_point 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();
+
+ start_ = tmp;
+ ++level_;
+ running_ = true;
+ return start_;
+ } else
+ {
+ ++level_;
+ ec.clear();
+ return time_point();
+ }
+ }
+
+ duration stop(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ if (running_ && (--level_ == 0))
+ {
+ time_point tmp = clock::now(ec);
+ if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
+
+ partial_ += tmp - start_;
+ storage_.store(partial_);
+ partial_ = 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();
+ }
+ }
+
+ duration elapsed(
+ system::error_code & ec = BOOST_CHRONO_THROWS
+ )
+ {
+ if (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_;
+ }
+ } else
+ {
+ return storage_.elapsed();
+ }
+ }
+
+
+ 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()
+ {
+ 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> >
+ scoped_run;
+ typedef stopwatch_stopper<basic_stopwatch<Clock, Memory> >
+ 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_;
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+#endif

Modified: trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp (original)
+++ trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp 2011-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
@@ -9,7 +9,7 @@
 
 #include <boost/chrono/duration.hpp>
 #include <boost/chrono/chrono_io.hpp>
-#include <boost/chrono/duration_style.hpp>
+#include <boost/chrono/io/duration_io.hpp>
 #include <boost/cstdint.hpp>
 #include <iostream>
 #include <iomanip>

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-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
@@ -19,6 +19,7 @@
 #endif
 
 #include <boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp>
+#include <boost/chrono/stopwatches/stopwatch_scoped.hpp>
 #include <boost/chrono/chrono.hpp>
 #include <boost/system/error_code.hpp>
 #include <boost/cstdint.hpp>
@@ -137,14 +138,14 @@
         base_type(fmt)
       {
       }
-// typedef stopwatch_runner<stopwatch_reporter<Stopwatch, Formatter> >
-// scoped_run;
-// typedef stopwatch_stopper<stopwatch_reporter<Stopwatch, Formatter> >
-// scoped_stop;
-// typedef stopwatch_suspender<stopwatch_reporter<Stopwatch, Formatter> >
-// scoped_suspend;
-// typedef stopwatch_resumer<stopwatch_reporter<Stopwatch, Formatter> >
-// scoped_resume;
+ typedef stopwatch_runner<stopwatch_reporter<Stopwatch, Formatter> >
+ scoped_run;
+ typedef stopwatch_stopper<stopwatch_reporter<Stopwatch, Formatter> >
+ scoped_stop;
+ typedef stopwatch_suspender<stopwatch_reporter<Stopwatch, Formatter> >
+ scoped_suspend;
+ typedef stopwatch_resumer<stopwatch_reporter<Stopwatch, Formatter> >
+ scoped_resume;
 
     protected:
 
@@ -187,14 +188,14 @@
       {
       }
 
-// typedef stopwatch_runner<wstopwatch_reporter<Stopwatch, Formatter> >
-// scoped_run;
-// typedef stopwatch_stopper<wstopwatch_reporter<Stopwatch, Formatter> >
-// scoped_stop;
-// typedef stopwatch_suspender<wstopwatch_reporter<Stopwatch, Formatter> >
-// scoped_suspend;
-// typedef stopwatch_resumer<wstopwatch_reporter<Stopwatch, Formatter> >
-// scoped_resume;
+ typedef stopwatch_runner<wstopwatch_reporter<Stopwatch, Formatter> >
+ scoped_run;
+ typedef stopwatch_stopper<wstopwatch_reporter<Stopwatch, Formatter> >
+ scoped_stop;
+ typedef stopwatch_suspender<wstopwatch_reporter<Stopwatch, Formatter> >
+ scoped_suspend;
+ typedef stopwatch_resumer<wstopwatch_reporter<Stopwatch, Formatter> >
+ scoped_resume;
 
     protected:
 

Added: trunk/boost/chrono/stopwatches/stopwatch_scoped.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/stopwatch_scoped.hpp 2011-09-21 18:39:38 EDT (Wed, 21 Sep 2011)
@@ -0,0 +1,127 @@
+// boost/chrono/stopwatches/stopwatch_scoped.hpp ------------------------------------------------------------//
+// Copyright 2009-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_STOPWATCH_SCOPED_HPP
+#define BOOST_CHRONO_STOPWATCHES_STOPWATCH_SCOPED_HPP
+
+#include <boost/chrono/chrono.hpp>
+#include <boost/system/error_code.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ //--------------------------------------------------------------------------------------//
+ template<class Stopwatch> class stopwatch_runner
+ {
+ public:
+ typedef Stopwatch stopwatch;
+ stopwatch_runner(stopwatch & a, system::error_code & ec =
+ BOOST_CHRONO_THROWS) :
+ stopwatch_(a)
+ {
+ stopwatch_.start(ec);
+ }
+ ~stopwatch_runner()
+ {
+ system::error_code ec;
+ stopwatch_.stop(ec);
+ }
+#if 0
+ typename Stopwatch::duration elapsed(system::error_code & ec = BOOST_CHRONO_THROWS)
+ {
+ return stopwatch_.elapsed(ec)-stopwatch_.get_storage();
+ }
+#endif
+ private:
+ stopwatch& stopwatch_;
+ stopwatch_runner();//= delete;
+ stopwatch_runner(const stopwatch_runner&); // = delete;
+ stopwatch_runner& operator=(const stopwatch_runner&); // = delete;
+
+ };
+
+ //--------------------------------------------------------------------------------------//
+ template<class Stopwatch> class stopwatch_stopper
+ {
+ public:
+ typedef Stopwatch stopwatch;
+ stopwatch_stopper(stopwatch & a, system::error_code & ec =
+ BOOST_CHRONO_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 = BOOST_CHRONO_THROWS)
+ {
+ return stopwatch_.elapsed(ec)-stopwatch_.get_storage();
+ }
+#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;
+ stopwatch_suspender(stopwatch & a, system::error_code & ec =
+ BOOST_CHRONO_THROWS) :
+ stopwatch_(a)
+ {
+ stopwatch_.suspend(ec);
+ }
+ ~stopwatch_suspender()
+ {
+ system::error_code ec;
+ stopwatch_.resume(ec);
+ }
+ private:
+ stopwatch& stopwatch_;
+ stopwatch_suspender(); // = delete;
+ stopwatch_suspender(const stopwatch_suspender&); // = delete;
+ stopwatch_suspender& operator=(const stopwatch_suspender&); // = delete;
+ };
+
+ //--------------------------------------------------------------------------------------//
+ template<class Stopwatch> class stopwatch_resumer
+ {
+ public:
+ typedef Stopwatch stopwatch;
+ stopwatch_resumer(stopwatch & a, system::error_code & ec =
+ BOOST_CHRONO_THROWS) :
+ stopwatch_(a)
+ {
+ stopwatch_.resume(ec);
+ }
+ ~stopwatch_resumer()
+ {
+ system::error_code ec;
+ stopwatch_.suspend(ec);
+ }
+ private:
+ stopwatch& stopwatch_;
+ stopwatch_resumer(); // = delete;
+ stopwatch_resumer(const stopwatch_resumer&); // = delete;
+ stopwatch_resumer& operator=(const stopwatch_resumer&); // = delete;
+ };
+
+ } // 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