Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74455 - in trunk/boost/chrono/stopwatches: formatters reporters
From: vicente.botet_at_[hidden]
Date: 2011-09-18 16:20:18


Author: viboes
Date: 2011-09-18 16:20:17 EDT (Sun, 18 Sep 2011)
New Revision: 74455
URL: http://svn.boost.org/trac/boost/changeset/74455

Log:
Chrono: Added basic Stopwatch reporter and formaters
Added:
   trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/reporters/system_default_formatter.hpp (contents, props changed)

Added: trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/formatters/base_formatter.hpp 2011-09-18 16:20:17 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,64 @@
+// 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_CHRONO_STOPWATCHES_FORMATTERS_BASE_FORMATTER_HPP
+#define BOOST_CHRONO_STOPWATCHES_FORMATTERS_BASE_FORMATTER_HPP
+
+#include <boost/chrono/duration.hpp>
+#include <boost/chrono/chrono_io.hpp>
+#include <boost/chrono/duration_style.hpp>
+#include <boost/cstdint.hpp>
+#include <iostream>
+#include <iomanip>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<typename CharT = char, typename Traits = std::char_traits<CharT> >
+ class base_formatter
+ {
+
+ public:
+ 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_;
+
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+
+#endif

Added: trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp 2011-09-18 16:20:17 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,115 @@
+// 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_CHRONO_STOPWATCHES_FORMATTERS_ELAPSED_HPP
+#define BOOST_CHRONO_STOPWATCHES_FORMATTERS_ELAPSED_HPP
+
+#include <boost/chrono/stopwatches/formatters/base_formatter.hpp>
+#include <boost/system/error_code.hpp>
+#include <boost/current_function.hpp>
+//#include <boost/chrono/stopwatches/detail/adaptive_string.hpp>
+#include <boost/format.hpp>
+#include <boost/format/group.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/assert.hpp>
+#include <string>
+#include <iostream>
+#include <cassert>
+#include <iomanip>
+
+#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT "%1%"
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<typename Ratio = micro, typename CharT = char,
+ typename Traits = std::char_traits<CharT>,
+ class Alloc = std::allocator<CharT> >
+ 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() :
+ base_type(),
+ internal_fmt_(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT),
+ fmt_(internal_fmt_)
+ {
+ }
+ basic_elapsed_formatter(ostream_type& os) :
+ base_type(os),
+ internal_fmt_(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT),
+ fmt_(internal_fmt_)
+ {
+ }
+ basic_elapsed_formatter(const char* fmt, ostream_type& os = std::cout) :
+ base_type(os), internal_fmt_(fmt), fmt_(internal_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)
+ {
+ }
+
+// static string_type format(const char* s)
+// {
+// string_type res(s);
+// res += boost::chrono::detail::adaptive_string(" : ");
+// res += BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT;
+// return res;
+// }
+
+ template<class Stopwatch>
+ void operator()(Stopwatch & stopwatch_, system::error_code & ec)
+ {
+ typedef typename Stopwatch::duration duration_t;
+ duration_t d = stopwatch_.elapsed(ec);
+
+ 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;
+
+ }
+ 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;
+
+ } // namespace chrono
+} // namespace boost
+
+#if 0
+#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT(F) \
+ boost::chrono::detail::adaptive_string(F " : " BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT)
+#ifdef __GNUC__
+#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FUNCTION_FORMAT \
+ boost::chrono::elapsed_formatter::format(BOOST_CURRENT_FUNCTION)
+#else
+#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FUNCTION_FORMAT \
+ BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT(BOOST_CURRENT_FUNCTION)
+#endif
+#endif
+
+#endif

Added: trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp 2011-09-18 16:20:17 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,200 @@
+// boost/chrono/stopwatches/stopwatch_reporter.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/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_REPORTERS_STOPWATCH_REPORTER_HPP
+#define BOOST_CHRONO_STOPWATCHES_REPORTERS_STOPWATCH_REPORTER_HPP
+
+#if !defined(BOOST_ENABLE_WARNINGS) && !defined(BOOST_CHRONO_ENABLE_WARNINGS)
+#if defined __GNUC__
+#pragma GCC system_header
+#elif defined __SUNPRO_CC
+#pragma disable_warn
+#elif defined _MSC_VER
+#pragma warning(push, 1)
+#endif
+#endif
+
+#include <boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp>
+#include <boost/chrono/chrono.hpp>
+#include <boost/system/error_code.hpp>
+#include <boost/cstdint.hpp>
+#include <cassert>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<class Stopwatch, class Formatter>
+ class basic_stopwatch_reporter: public Stopwatch
+ {
+ public:
+ typedef typename Stopwatch::clock clock;
+ typedef Stopwatch stopwatch;
+ typedef Formatter formatter_type;
+
+ basic_stopwatch_reporter() BOOST_CHRONO_NOEXCEPT :
+ formatter_(), reported_(false)
+ {
+ }
+
+ explicit basic_stopwatch_reporter(const typename Formatter::char_type* fmt) :
+ formatter_(fmt), reported_(false)
+ {
+ }
+ explicit basic_stopwatch_reporter(typename Formatter::string_type const& fmt) :
+ formatter_(fmt), reported_(false)
+ {
+ }
+ explicit basic_stopwatch_reporter(formatter_type const& fmt) :
+ formatter_(fmt), reported_(false)
+ {
+ }
+
+ ~basic_stopwatch_reporter() BOOST_CHRONO_NOEXCEPT
+ {
+ system::error_code ec;
+ if (!reported())
+ {
+ this->report(ec);
+ }
+ }
+
+ inline void report(system::error_code & ec= BOOST_CHRONO_THROWS)
+ {
+ reported_ = true;
+ formatter_(*this, ec);
+ }
+ bool reported() const
+ {
+ return reported_;
+ }
+
+ protected:
+ formatter_type formatter_;
+ bool reported_;
+
+ basic_stopwatch_reporter(const basic_stopwatch_reporter&); // = delete;
+ basic_stopwatch_reporter& operator=(const basic_stopwatch_reporter&); // = delete;
+ };
+
+
+ template<class Stopwatch,
+ class Formatter = typename stopwatch_reporter_default_formatter<
+ Stopwatch>::type>
+ class stopwatch_reporter;
+
+ template<class Stopwatch, class Formatter>
+ struct stopwatch_reporter_default_formatter<stopwatch_reporter<Stopwatch,
+ Formatter> >
+ {
+ typedef Formatter type;
+ };
+
+ template<class Stopwatch, class Formatter>
+ class stopwatch_reporter: public basic_stopwatch_reporter<Stopwatch,
+ Formatter>
+ {
+ typedef basic_stopwatch_reporter<Stopwatch, Formatter> base_type;
+ public:
+ typedef typename Stopwatch::clock clock;
+ typedef Stopwatch stopwatch;
+ typedef Formatter formatter_type;
+
+ stopwatch_reporter()
+ //: base_type()
+ {
+ }
+ explicit stopwatch_reporter(formatter_type const& fmt) :
+ base_type(fmt)
+ {
+ }
+ explicit stopwatch_reporter(const typename Formatter::char_type* fmt) :
+ base_type(fmt)
+ {
+ }
+ explicit stopwatch_reporter(typename Formatter::string_type const& fmt) :
+ 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;
+
+ protected:
+
+ stopwatch_reporter(const stopwatch_reporter&); // = delete;
+ stopwatch_reporter& operator=(const stopwatch_reporter&); // = delete;
+ };
+
+ template<class Stopwatch,
+ class Formatter = typename wstopwatch_reporter_default_formatter<
+ Stopwatch>::type>
+ class wstopwatch_reporter;
+
+ template<class Stopwatch, class Formatter>
+ struct wstopwatch_reporter_default_formatter<wstopwatch_reporter<
+ Stopwatch, Formatter> >
+ {
+ typedef Formatter type;
+ };
+
+ template<class Stopwatch, class Formatter>
+ class wstopwatch_reporter: public basic_stopwatch_reporter<Stopwatch,
+ Formatter>
+ {
+ typedef basic_stopwatch_reporter<Stopwatch, Formatter> base_type;
+ public:
+ typedef typename Stopwatch::clock clock;
+ typedef Stopwatch stopwatch;
+ typedef Formatter formatter_type;
+
+ wstopwatch_reporter() :
+ base_type()
+ {
+ }
+ explicit wstopwatch_reporter(formatter_type const& fmt) :
+ base_type(fmt)
+ {
+ }
+
+// 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:
+
+ //wstopwatch_reporter(); // = delete;
+ wstopwatch_reporter(const wstopwatch_reporter&); // = delete;
+ wstopwatch_reporter& operator=(const wstopwatch_reporter&); // = delete;
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+
+#if !defined(BOOST_ENABLE_WARNINGS) && !defined(BOOST_CHRONO_ENABLE_WARNINGS)
+#if defined __SUNPRO_CC
+#pragma enable_warn
+#elif defined _MSC_VER
+#pragma warning(pop)
+#endif
+#endif
+
+#endif
+
+

Added: trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp 2011-09-18 16:20:17 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,29 @@
+// boost/chrono/stopwatches/stopwatch_reporter.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/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_REPORTERS_DEFAULT_FORMATTER_HPP
+#define BOOST_CHRONO_STOPWATCHES_REPORTERS_DEFAULT_FORMATTER_HPP
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<class Stopwatch>
+ struct stopwatch_reporter_default_formatter;
+
+ template<class Stopwatch>
+ struct wstopwatch_reporter_default_formatter;
+
+ } // namespace chrono
+} // namespace boost
+
+
+
+#endif
+
+

Added: trunk/boost/chrono/stopwatches/reporters/system_default_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/reporters/system_default_formatter.hpp 2011-09-18 16:20:17 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,52 @@
+// boost/chrono/stopwatches/stopwatch_reporter.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/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_REPORTERS_SYSTEM_DEFAULT_FORMATTER_HPP
+#define BOOST_CHRONO_STOPWATCHES_REPORTERS_SYSTEM_DEFAULT_FORMATTER_HPP
+
+#include <boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp>
+#include <boost/chrono/stopwatches/formatters/elapsed_formatter.hpp>
+#include <boost/chrono/stopwatches/simple_stopwatch.hpp>
+#include <boost/chrono/system_clocks.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<system_clock> >
+ {
+ typedef elapsed_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<system_clock> >
+ {
+ typedef welapsed_formatter type;
+ };
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<steady_clock> >
+ {
+ typedef elapsed_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<steady_clock> >
+ {
+ typedef welapsed_formatter type;
+ };
+
+ } // 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