1 // Copyright 2015-2017 Hans Dembinski
2 //
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)
6
7 #ifndef BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
8 #define BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
9
10 #include <boost/histogram/detail/counting_streambuf.hpp>
11 #include <boost/histogram/fwd.hpp>
12 #include <ios>
13
14 /**
15 \file boost/histogram/accumulators/ostream.hpp
16 Simple streaming operators for the builtin accumulator types.
17
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 use your own, include your own implementation instead of this header and do not
22 include
23 [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
24 */
25
26 #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
27
28 namespace boost {
29 namespace histogram {
30
31 namespace detail {
32
33 template <class CharT, class Traits, class T>
handle_nonzero_width(std::basic_ostream<CharT,Traits> & os,const T & x)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();
37 os.width(0);
38 std::streamsize count = 0;
39 {
40 auto g = make_count_guard(os, count);
41 os << x;
42 }
43 if (os.flags() & std::ios::left) {
44 os << x;
45 for (auto i = count; i < w; ++i) os << os.fill();
46 } else {
47 for (auto i = count; i < w; ++i) os << os.fill();
48 os << x;
49 }
50 return os;
51 }
52
53 } // namespace detail
54
55 namespace accumulators {
56
57 template <class CharT, class Traits, class U>
operator <<(std::basic_ostream<CharT,Traits> & os,const count<U> & x)58 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
59 const count<U>& x) {
60 return os << x.value();
61 }
62
63 template <class CharT, class Traits, class U>
operator <<(std::basic_ostream<CharT,Traits> & os,const sum<U> & x)64 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
65 const sum<U>& x) {
66 if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
67 return detail::handle_nonzero_width(os, x);
68 }
69
70 template <class CharT, class Traits, class U>
operator <<(std::basic_ostream<CharT,Traits> & os,const weighted_sum<U> & x)71 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
72 const weighted_sum<U>& x) {
73 if (os.width() == 0)
74 return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
75 return detail::handle_nonzero_width(os, x);
76 }
77
78 template <class CharT, class Traits, class U>
operator <<(std::basic_ostream<CharT,Traits> & os,const mean<U> & x)79 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
80 const mean<U>& x) {
81 if (os.width() == 0)
82 return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
83 return detail::handle_nonzero_width(os, x);
84 }
85
86 template <class CharT, class Traits, class U>
operator <<(std::basic_ostream<CharT,Traits> & os,const weighted_mean<U> & x)87 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
88 const weighted_mean<U>& x) {
89 if (os.width() == 0)
90 return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
91 << x.variance() << ")";
92 return detail::handle_nonzero_width(os, x);
93 }
94
95 template <class CharT, class Traits, class T>
operator <<(std::basic_ostream<CharT,Traits> & os,const thread_safe<T> & x)96 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
97 const thread_safe<T>& x) {
98 os << x.load();
99 return os;
100 }
101 } // namespace accumulators
102 } // namespace histogram
103 } // namespace boost
104
105 #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED
106
107 #endif
108