1 // Copyright 2015-2017 Hans Dembinski
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
8 #define BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
10 #include <boost/histogram/detail/counting_streambuf.hpp>
11 #include <boost/histogram/fwd.hpp>
15 \file boost/histogram/accumulators/ostream.hpp
16 Simple streaming operators for the builtin accumulator types.
18 The text representation is not guaranteed to be stable between versions of
19 Boost.Histogram. This header is only included by
20 [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
21 To you use your own, include your own implementation instead of this header and do not
23 [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
26 #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
33 template <class CharT, class Traits, class T>
34 std::basic_ostream<CharT, Traits>& handle_nonzero_width(
35 std::basic_ostream<CharT, Traits>& os, const T& x) {
36 const auto w = os.width();
38 counting_streambuf<CharT, Traits> cb;
39 const auto saved = os.rdbuf(&cb);
42 if (os.flags() & std::ios::left) {
44 for (auto i = cb.count; i < w; ++i) os << os.fill();
46 for (auto i = cb.count; i < w; ++i) os << os.fill();
54 namespace accumulators {
55 template <class CharT, class Traits, class W>
56 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
58 if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
59 return detail::handle_nonzero_width(os, x);
62 template <class CharT, class Traits, class W>
63 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
64 const weighted_sum<W>& x) {
66 return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
67 return detail::handle_nonzero_width(os, x);
70 template <class CharT, class Traits, class W>
71 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
74 return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
75 return detail::handle_nonzero_width(os, x);
78 template <class CharT, class Traits, class W>
79 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
80 const weighted_mean<W>& x) {
82 return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
83 << x.variance() << ")";
84 return detail::handle_nonzero_width(os, x);
87 template <class CharT, class Traits, class T>
88 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
89 const thread_safe<T>& x) {
93 } // namespace accumulators
94 } // namespace histogram
97 #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED