Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74490 - trunk/boost/chrono/stopwatches/formatters
From: vicente.botet_at_[hidden]
Date: 2011-09-21 13:04:14


Author: viboes
Date: 2011-09-21 13:04:12 EDT (Wed, 21 Sep 2011)
New Revision: 74490
URL: http://svn.boost.org/trac/boost/changeset/74490

Log:
Chrono: refactor formatter to inherit from boost::basic_format
Text files modified:
   trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp | 8 ++++----
   trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp | 37 ++++++++++++++-----------------------
   trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp | 35 +++++++++++++----------------------
   3 files changed, 31 insertions(+), 49 deletions(-)

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 13:04:12 EDT (Wed, 21 Sep 2011)
@@ -27,11 +27,11 @@
       typedef std::basic_ostream<CharT, Traits> ostream_type;
 
       base_formatter() :
- precision_(3), os_(std::cout), style_(duration_style::symbol)
+ precision_(3), os_(std::cout), duration_style_(duration_style::symbol)
       {
       }
       base_formatter(ostream_type& os) :
- precision_(3), os_(os), style_(duration_style::symbol)
+ precision_(3), os_(os), duration_style_(duration_style::symbol)
       {
       }
 
@@ -47,13 +47,13 @@
       }
       void set_duration_style(duration_style::type style)
       {
- style_ == style;
+ duration_style_ == style;
       }
 
     protected:
       std::size_t precision_;
       ostream_type & os_;
- duration_style::type style_;
+ duration_style::type duration_style_;
 
     };
 

Modified: trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp (original)
+++ trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp 2011-09-21 13:04:12 EDT (Wed, 21 Sep 2011)
@@ -20,17 +20,17 @@
 #include <cassert>
 #include <iomanip>
 
-#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT "%1%"
+#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT "%1%\n"
 
 namespace boost
 {
   namespace chrono
   {
 
- template<typename Ratio = micro, typename CharT = char,
+ template<typename Ratio = milli, typename CharT = char,
         typename Traits = std::char_traits<CharT>,
         class Alloc = std::allocator<CharT> >
- class basic_elapsed_formatter: public base_formatter<CharT, Traits>
+ class basic_elapsed_formatter: public base_formatter<CharT, Traits>, public basic_format<CharT, Traits>
     {
 
     public:
@@ -42,27 +42,21 @@
 
       basic_elapsed_formatter() :
         base_type(),
- internal_fmt_(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT),
- fmt_(internal_fmt_)
+ format_type(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT)
       {
       }
       basic_elapsed_formatter(ostream_type& os) :
         base_type(os),
- internal_fmt_(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT),
- fmt_(internal_fmt_)
+ format_type(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT)
       {
       }
       basic_elapsed_formatter(const char* fmt, ostream_type& os = std::cout) :
- base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
+ base_type(os), format_type(fmt)
       {
       }
       basic_elapsed_formatter(string_type const& fmt, ostream_type& os =
           std::cout) :
- base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
- {
- }
- basic_elapsed_formatter(format_type & fmt, ostream_type& os = std::cout) :
- base_type(os), fmt_(fmt)
+ base_type(os), format_type(fmt)
       {
       }
 
@@ -75,7 +69,7 @@
 // }
 
       template<class Stopwatch>
- void operator()(Stopwatch & stopwatch_, system::error_code & ec)
+ void operator()(Stopwatch & stopwatch_, system::error_code & ec= BOOST_CHRONO_THROWS)
       {
         typedef typename Stopwatch::duration duration_t;
         duration_t d = stopwatch_.elapsed(ec);
@@ -83,19 +77,16 @@
         if (d < duration_t::zero())
           return;
 
- this->os_
- << fmt_
- % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<
- double, Ratio>(d)) << std::endl;
+ this->os_ << static_cast<format_type&>(*this)
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), boost::chrono::duration<
+ double, Ratio>(d))
+ ;
 
       }
- private:
- boost::format internal_fmt_;
- boost::format& fmt_;
     };
 
- typedef basic_elapsed_formatter<micro, char> elapsed_formatter;
- typedef basic_elapsed_formatter<micro, wchar_t> welapsed_formatter;
+ typedef basic_elapsed_formatter<milli, char> elapsed_formatter;
+ typedef basic_elapsed_formatter<milli, wchar_t> welapsed_formatter;
 
   } // namespace chrono
 } // namespace boost

Modified: trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp (original)
+++ trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp 2011-09-21 13:04:12 EDT (Wed, 21 Sep 2011)
@@ -27,10 +27,10 @@
   namespace chrono
   {
 
- template<typename Ratio = micro, typename CharT = char,
+ template<typename Ratio = milli, typename CharT = char,
         typename Traits = std::char_traits<CharT>,
         class Alloc = std::allocator<CharT> >
- class basic_times_formatter: public base_formatter<CharT, Traits>
+ class basic_times_formatter: public base_formatter<CharT, Traits>, public basic_format<CharT, Traits>
     {
 
     public:
@@ -42,27 +42,21 @@
 
       basic_times_formatter() :
         base_type(),
- internal_fmt_(BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT),
- fmt_(internal_fmt_)
+ format_type(BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT)
       {
       }
       basic_times_formatter(ostream_type& os) :
         base_type(os),
- internal_fmt_(BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT),
- fmt_(internal_fmt_)
+ format_type(BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT)
       {
       }
       basic_times_formatter(const char* fmt, ostream_type& os = std::cout) :
- base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
+ base_type(os), format_type(fmt)
       {
       }
       basic_times_formatter(string_type const& fmt, ostream_type& os =
           std::cout) :
- base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
- {
- }
- basic_times_formatter(format_type & fmt, ostream_type& os = std::cout) :
- base_type(os), fmt_(fmt)
+ base_type(os), format_type(fmt)
       {
       }
 
@@ -75,7 +69,7 @@
 // }
 
       template<class Stopwatch>
- void operator()(Stopwatch & stopwatch_, system::error_code & ec)
+ void operator()(Stopwatch & stopwatch_, system::error_code & ec= BOOST_CHRONO_THROWS)
       {
         typedef typename Stopwatch::duration::rep times_type;
         times_type times = stopwatch_.elapsed(ec).count();
@@ -88,17 +82,14 @@
         else
           p=0;
 
- this->os_ << fmt_
- % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.real)))
- % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.user)))
- % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.system)))
- % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.user+times.system)))
+ this->os_ << static_cast<format_type&>(*this)
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.real)))
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.user)))
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.system)))
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.user+times.system)))
             % io::group(std::fixed, std::setprecision(2), p)
- << std::endl;
+ ;
       }
- private:
- boost::format internal_fmt_;
- boost::format& fmt_;
     };
 
     typedef basic_times_formatter<milli, char> times_formatter;


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