$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r74380 - in sandbox/stopwatches: boost/chrono/stopwatches/formatters libs/chrono/stopwatches/example
From: vicente.botet_at_[hidden]
Date: 2011-09-14 18:47:22
Author: viboes
Date: 2011-09-14 18:47:21 EDT (Wed, 14 Sep 2011)
New Revision: 74380
URL: http://svn.boost.org/trac/boost/changeset/74380
Log:
Stopwatches: Added base_formatter and missing duration_style.hpp
Added:
   sandbox/stopwatches/boost/chrono/stopwatches/formatters/duration_style.hpp   (contents, props changed)
Text files modified: 
   sandbox/stopwatches/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp    |    88 ++++++++++++++++++++++++--------------- 
   sandbox/stopwatches/boost/chrono/stopwatches/formatters/oneshot_formatter.hpp    |    25 +++++++----                             
   sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp |     2                                         
   3 files changed, 71 insertions(+), 44 deletions(-)
Added: sandbox/stopwatches/boost/chrono/stopwatches/formatters/duration_style.hpp
==============================================================================
--- (empty file)
+++ sandbox/stopwatches/boost/chrono/stopwatches/formatters/duration_style.hpp	2011-09-14 18:47:21 EDT (Wed, 14 Sep 2011)
@@ -0,0 +1,110 @@
+//  boost/chrono/stopwatches/stopwatch_formatter.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_STOPWATCHES_FORMATTERS_DURATION_STYLE_HPP
+#define BOOST_STOPWATCHES_FORMATTERS_DURATION_STYLE_HPP
+
+#include <boost/chrono/chrono_io.hpp>
+#include <boost/chrono/config.hpp>
+
+namespace boost
+{
+  namespace chrono
+  {
+
+    struct duration_style
+    {
+      enum type {
+        prefix_text, symbol
+      };
+    };
+
+
+    class duration_fmt
+    {
+      duration_style::type style_;
+    public:
+      explicit duration_fmt(duration_style::type style) BOOST_CHRONO_NOEXCEPT
+      : style_(style)
+      {}
+
+#ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS
+      explicit
+      operator duration_style::type() const BOOST_CHRONO_NOEXCEPT
+      { return style_;}
+#endif
+
+      duration_style::type get_duration_style() const BOOST_CHRONO_NOEXCEPT
+      { return style_;}
+    };
+
+    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;
+    }
+
+    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;
+    }
+
+    template<typename CharT = char, typename Traits = std::char_traits<CharT> >
+    struct duration_style_io_saver
+    {
+
+      typedef std::basic_ios<CharT, Traits> state_type;
+      typedef duration_style::type aspect_type;
+
+      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;
+      }
+
+      duration_style_io_saver(state_type &s, aspect_type new_value) :
+        s_save_(s), a_save_(new_value)
+      {
+      }
+
+      ~duration_style_io_saver()
+      {
+        this->restore();
+      }
+
+      void restore()
+      {
+        s_save_ << duration_fmt(a_save_);
+      }
+    private:
+      state_type& s_save_;
+      aspect_type a_save_;
+    };
+  } // namespace chrono
+} // namespace boost
+
+
+#endif
Modified: sandbox/stopwatches/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp
==============================================================================
--- sandbox/stopwatches/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp	(original)
+++ sandbox/stopwatches/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp	2011-09-14 18:47:21 EDT (Wed, 14 Sep 2011)
@@ -28,59 +28,84 @@
   namespace chrono
   {
 
+    template<typename CharT = char, typename Traits = std::char_traits<CharT> >
+    class base_formatter
+    {
+
+    public:
+      typedef basic_format<CharT, Traits> format_type;
+      typedef CharT char_type;
+      typedef std::basic_ostream<CharT, Traits> ostream_type;
+
+      base_formatter() :
+        precision_(3), os_(std::cout),
+            style_(duration_style::symbol)
+      {
+      }
+      base_formatter(ostream_type& os) :
+        precision_(3), os_(os),
+            style_(duration_style::symbol)
+      {
+      }
+
+      void set_precision(std::size_t precision)
+      {
+        precision_=precision;
+        if (precision_ > 9)
+          precision_ = 9; // sanity check
+      }
+      void set_os(ostream_type& os)
+      {
+        os_=os;
+      }
+      void set_duration_style(duration_style::type style)
+      {
+        style_==style;
+      }
+
+    protected:
+      std::size_t precision_;
+      ostream_type & os_;
+      duration_style::type style_;
+
+
+    };
     template<typename Ratio=micro, typename CharT = char, typename Traits = std::char_traits<CharT>,
         class Alloc = std::allocator<CharT> >
-    class basic_elapsed_formatter
+    class basic_elapsed_formatter : public base_formatter<CharT, Traits>
     {
 
     public:
+      typedef base_formatter<CharT, Traits> base_type;
       typedef basic_format<CharT, Traits> format_type;
       typedef std::basic_string<CharT, Traits, Alloc> string_type;
       typedef CharT char_type;
       typedef std::basic_ostream<CharT, Traits> ostream_type;
 
-      basic_elapsed_formatter() :
+      basic_elapsed_formatter() : base_type(),
         internal_fmt_(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT),
-            fmt_(internal_fmt_), precision_(3), os_(std::cout),
-            style_(symbol)
+            fmt_(internal_fmt_)
       {
       }
-      basic_elapsed_formatter(ostream_type& os) :
+      basic_elapsed_formatter(ostream_type& os) : base_type(os),
         internal_fmt_(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT),
-            fmt_(internal_fmt_), precision_(3), os_(os),
-            style_(symbol)
+            fmt_(internal_fmt_)
       {
       }
       basic_elapsed_formatter(const char* fmt, ostream_type& os=std::cout) :
-        internal_fmt_(fmt), fmt_(internal_fmt_), precision_(3), os_(os),
-        style_(symbol)
+        base_type(os),
+        internal_fmt_(fmt), fmt_(internal_fmt_)
       {
       }
       basic_elapsed_formatter(string_type const& fmt, ostream_type& os=std::cout) :
-        internal_fmt_(fmt), fmt_(internal_fmt_), precision_(3), os_(os),
-        style_(symbol)
+        base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
       {
       }
       basic_elapsed_formatter(format_type & fmt, ostream_type& os=std::cout) :
-        fmt_(fmt), precision_(3), os_(os),
-        style_(symbol)
+        base_type(os), fmt_(fmt)
       {
       }
 
-      void set_precision(std::size_t precision)
-      {
-        precision_=precision;
-        if (precision_ > 9)
-          precision_ = 9; // sanity check
-      }
-      void set_os(ostream_type& os)
-      {
-        os_=os;
-      }
-      void set_duration_style(duration_style style)
-      {
-        style_==style;
-      }
       static string_type format(const char* s)
       {
         string_type res(s);
@@ -98,20 +123,15 @@
         if (d < duration_t::zero())
           return;
 
-        os_
+        this->os_
             << fmt_
-                % io::group(std::fixed, std::setprecision(precision_), duration_fmt(style_), boost::chrono::duration<
+                % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<
                     double, Ratio>(d)) << std::endl;
 
       }
     private:
       boost::format internal_fmt_;
       boost::format& fmt_;
-      std::size_t precision_;
-      ostream_type & os_;
-      duration_style style_;
-
-
     };
 
     typedef basic_elapsed_formatter<micro, char> elapsed_formatter;
Modified: sandbox/stopwatches/boost/chrono/stopwatches/formatters/oneshot_formatter.hpp
==============================================================================
--- sandbox/stopwatches/boost/chrono/stopwatches/formatters/oneshot_formatter.hpp	(original)
+++ sandbox/stopwatches/boost/chrono/stopwatches/formatters/oneshot_formatter.hpp	2011-09-14 18:47:21 EDT (Wed, 14 Sep 2011)
@@ -38,27 +38,34 @@
       typedef std::basic_ostream<CharT, Traits> ostream_type;
 
       basic_oneshot_formatter() :
-        format_(BOOST_CHRONO_STOPWATCHES_ONESHOOT_FORMAT_DEFAULT), precision_(3),
-            os_(std::cout)
+        format_(BOOST_CHRONO_STOPWATCHES_ONESHOOT_FORMAT_DEFAULT),
+            precision_(3), os_(std::cout)
       {
       }
-      basic_oneshot_formatter(const char_type* fmt) :
-        format_(fmt), precision_(3), os_(std::cout)
+      basic_oneshot_formatter(ostream_type& os) :
+        format_(BOOST_CHRONO_STOPWATCHES_ONESHOOT_FORMAT_DEFAULT),
+            precision_(3), os_(os)
       {
       }
-      basic_oneshot_formatter(string_type const& fmt) :
-        str_(fmt), format_(str_.c_str()), precision_(3), os_(std::cout)
+      basic_oneshot_formatter(const char_type* fmt, ostream_type& os =
+          std::cout) :
+        format_(fmt), precision_(3), os_(os)
+      {
+      }
+      basic_oneshot_formatter(string_type const& fmt, ostream_type& os =
+          std::cout) :
+        str_(fmt), format_(str_.c_str()), precision_(3), os_(os)
       {
       }
       void set_precision(std::size_t precision)
       {
-        precision_=precision;
+        precision_ = precision;
         if (precision_ > 9)
           precision_ = 9; // sanity check
-        }
+      }
       void set_os(ostream_type os)
       {
-        os_=os;
+        os_ = os;
       }
       static string_type format(const char* s)
       {
Modified: sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp
==============================================================================
--- sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp	(original)
+++ sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp	2011-09-14 18:47:21 EDT (Wed, 14 Sep 2011)
@@ -59,7 +59,7 @@
 {
   typedef basic_elapsed_formatter<milli > formatter;
   formatter fmt("Elapsed time: %1%",std::cerr);
-  fmt.set_duration_style(text);
+  fmt.set_duration_style(duration_style::prefix_text);
   fmt.set_precision(6);
 
   stopwatch_reporter2<stopwatch<high_resolution_clock>, formatter> sw(fmt);