]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/histogram/accumulators/ostream.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / histogram / accumulators / ostream.hpp
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 you 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>
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 counting_streambuf<CharT, Traits> cb;
39 const auto saved = os.rdbuf(&cb);
40 os << x;
41 os.rdbuf(saved);
42 if (os.flags() & std::ios::left) {
43 os << x;
44 for (auto i = cb.count; i < w; ++i) os << os.fill();
45 } else {
46 for (auto i = cb.count; i < w; ++i) os << os.fill();
47 os << x;
48 }
49 return os;
50 }
51
52 } // namespace detail
53
54 namespace accumulators {
55
56 template <class CharT, class Traits, class U>
57 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
58 const count<U>& x) {
59 return os << x.value();
60 }
61
62 template <class CharT, class Traits, class U>
63 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
64 const sum<U>& x) {
65 if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
66 return detail::handle_nonzero_width(os, x);
67 }
68
69 template <class CharT, class Traits, class U>
70 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
71 const weighted_sum<U>& x) {
72 if (os.width() == 0)
73 return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
74 return detail::handle_nonzero_width(os, x);
75 }
76
77 template <class CharT, class Traits, class U>
78 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
79 const mean<U>& x) {
80 if (os.width() == 0)
81 return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
82 return detail::handle_nonzero_width(os, x);
83 }
84
85 template <class CharT, class Traits, class U>
86 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
87 const weighted_mean<U>& x) {
88 if (os.width() == 0)
89 return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
90 << x.variance() << ")";
91 return detail::handle_nonzero_width(os, x);
92 }
93
94 template <class CharT, class Traits, class T>
95 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
96 const thread_safe<T>& x) {
97 os << x.load();
98 return os;
99 }
100 } // namespace accumulators
101 } // namespace histogram
102 } // namespace boost
103
104 #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED
105
106 #endif