1 // boost/chrono/stopwatches/formatters/ accumulator_set_formatter.hpp ------------------------------------------------------------//
2 // Copyright 2011 Vicente J. Botet Escriba
3 // Distributed under the Boost Software License, Version 1.0.
4 // See http://www.boost.org/LICENSE_1_0.txt
5 // See http://www.boost.org/libs/chrono/stopwatches for documentation.
7 #ifndef BOOST_CHRONO_STOPWATCHES_FORMATTERS_ACCUMULATOR_SET_HPP
8 #define BOOST_CHRONO_STOPWATCHES_FORMATTERS_ACCUMULATOR_SET_HPP
10 #include <boost/chrono/stopwatches/formatters/base_formatter.hpp>
11 #include <boost/chrono/chrono_io.hpp>
12 #include <boost/current_function.hpp>
13 #include <boost/accumulators/framework/accumulator_set.hpp>
14 #include <boost/accumulators/statistics/count.hpp>
15 #include <boost/accumulators/statistics/sum.hpp>
16 #include <boost/accumulators/statistics/min.hpp>
17 #include <boost/accumulators/statistics/max.hpp>
18 #include <boost/accumulators/statistics/mean.hpp>
19 #include <boost/accumulators/accumulators.hpp>
20 #include <boost/format.hpp>
21 #include <boost/format/group.hpp>
22 #include <boost/cstdint.hpp>
23 #include <boost/assert.hpp>
29 #define BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT_DEFAULT "count=%1%, sum=%2%, min=%3%, max=%4%, mean=%5%\n"
36 template<typename Ratio = milli, typename CharT = char,
37 typename Traits = std::char_traits<CharT>,
38 class Alloc = std::allocator<CharT> >
39 class basic_accumulator_set_formatter: public base_formatter<CharT, Traits> , public basic_format<
44 typedef base_formatter<CharT, Traits> base_type;
45 typedef basic_format<CharT, Traits> format_type;
46 typedef std::basic_string<CharT, Traits, Alloc> string_type;
47 typedef CharT char_type;
48 typedef std::basic_ostream<CharT, Traits> ostream_type;
50 basic_accumulator_set_formatter() :
52 format_type(BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT_DEFAULT)
55 basic_accumulator_set_formatter(ostream_type& os) :
57 format_type(BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT_DEFAULT)
60 basic_accumulator_set_formatter(const char* fmt, ostream_type& os =
62 base_type(os), format_type(fmt)
65 basic_accumulator_set_formatter(string_type const& fmt, ostream_type& os =
67 base_type(os), format_type(fmt)
71 // static string_type format(const char* s)
73 // string_type res(s);
74 // res += boost::chrono::detail::adaptive_string(" : ");
75 // res += BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT_DEFAULT;
79 template<class Stopwatch>
80 void operator()(Stopwatch & stopwatch_
81 //, system::error_code & ec = BOOST_CHRONO_THROWS
85 typedef typename Stopwatch::laps_collector::storage_type
87 laps_collector_acc const& acc =
88 stopwatch_.get_laps_collector().accumulator_set();
90 typedef typename Stopwatch::duration duration_t;
92 duration_style_io_saver dsios(this->os_);
94 << static_cast<format_type&> (*this)
95 % boost::accumulators::count(acc)
96 % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), (boost::accumulators::count(acc)
97 == 0) ? boost::chrono::duration<double, Ratio>(duration_t::zero()) : boost::chrono::duration<
98 double, Ratio>(duration_t(boost::accumulators::sum(acc))))
99 % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), (boost::accumulators::count(acc)
100 == 0) ? boost::chrono::duration<double, Ratio>(duration_t::zero()) : boost::chrono::duration<
101 double, Ratio>(duration_t((boost::accumulators::min)(acc))))
102 % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), (boost::accumulators::count(acc)
103 == 0) ? boost::chrono::duration<double, Ratio>(duration_t::zero()) : boost::chrono::duration<
104 double, Ratio>(duration_t((boost::accumulators::max)(acc))))
105 % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), ((boost::accumulators::count(acc)
106 > 0) ? boost::chrono::duration<double, Ratio>(duration_t(boost::accumulators::sum(acc)
107 / boost::accumulators::count(acc))) : boost::chrono::duration<
108 double, Ratio>(duration_t::zero())));
113 typedef basic_accumulator_set_formatter<milli, char>
114 accumulator_set_formatter;
115 typedef basic_accumulator_set_formatter<milli, wchar_t>
116 waccumulator_set_formatter;
118 } // namespace chrono
122 #define BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT(F) \
123 boost::chrono::detail::adaptive_string(F " : " BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT_DEFAULT)
125 #define BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FUNCTION_FORMAT \
126 boost::chrono::elapsed_formatter::format(BOOST_CURRENT_FUNCTION)
128 #define BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FUNCTION_FORMAT \
129 BOOST_CHRONO_STOPWATCHES_ACCUMULATOR_SET_FORMAT(BOOST_CURRENT_FUNCTION)