123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #ifndef BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
- #define BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
- #include <boost/histogram/detail/counting_streambuf.hpp>
- #include <boost/histogram/fwd.hpp>
- #include <ios>
- #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
- namespace boost {
- namespace histogram {
- namespace detail {
- template <class CharT, class Traits, class T>
- std::basic_ostream<CharT, Traits>& handle_nonzero_width(
- std::basic_ostream<CharT, Traits>& os, const T& x) {
- const auto w = os.width();
- os.width(0);
- std::streamsize count = 0;
- {
- auto g = make_count_guard(os, count);
- os << x;
- }
- if (os.flags() & std::ios::left) {
- os << x;
- for (auto i = count; i < w; ++i) os << os.fill();
- } else {
- for (auto i = count; i < w; ++i) os << os.fill();
- os << x;
- }
- return os;
- }
- }
- namespace accumulators {
- template <class CharT, class Traits, class U, bool B>
- std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
- const count<U, B>& x) {
- return os << x.value();
- }
- template <class CharT, class Traits, class U>
- std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
- const sum<U>& x) {
- if (os.width() == 0)
- return os << "sum(" << x.large_part() << " + " << x.small_part() << ")";
- return detail::handle_nonzero_width(os, x);
- }
- template <class CharT, class Traits, class U>
- std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
- const weighted_sum<U>& x) {
- if (os.width() == 0)
- return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
- return detail::handle_nonzero_width(os, x);
- }
- template <class CharT, class Traits, class U>
- std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
- const mean<U>& x) {
- if (os.width() == 0)
- return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
- return detail::handle_nonzero_width(os, x);
- }
- template <class CharT, class Traits, class U>
- std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
- const weighted_mean<U>& x) {
- if (os.width() == 0)
- return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
- << x.variance() << ")";
- return detail::handle_nonzero_width(os, x);
- }
- template <class CharT, class Traits, class U>
- std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
- const fraction<U>& x) {
- if (os.width() == 0)
- return os << "fraction(" << x.successes() << ", " << x.failures() << ")";
- return detail::handle_nonzero_width(os, x);
- }
- }
- }
- }
- #endif
- #endif
|