]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/histogram/accumulators/ostream.hpp
import new upstream nautilus stable release 14.2.8
[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 template <class CharT, class Traits, class W>
56 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
57 const sum<W>& x) {
58 if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
59 return detail::handle_nonzero_width(os, x);
60 }
61
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) {
65 if (os.width() == 0)
66 return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
67 return detail::handle_nonzero_width(os, x);
68 }
69
70 template <class CharT, class Traits, class W>
71 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
72 const mean<W>& x) {
73 if (os.width() == 0)
74 return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
75 return detail::handle_nonzero_width(os, x);
76 }
77
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) {
81 if (os.width() == 0)
82 return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
83 << x.variance() << ")";
84 return detail::handle_nonzero_width(os, x);
85 }
86
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) {
90 os << x.load();
91 return os;
92 }
93 } // namespace accumulators
94 } // namespace histogram
95 } // namespace boost
96
97 #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED
98
99 #endif