Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / histogram / accumulators / ostream.hpp
index 76e102a..0ff1ab2 100644 (file)
@@ -7,8 +7,9 @@
 #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 <iosfwd>
+#include <ios>
 
 /**
   \file boost/histogram/accumulators/ostream.hpp
 
 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);
+  counting_streambuf<CharT, Traits> cb;
+  const auto saved = os.rdbuf(&cb);
+  os << x;
+  os.rdbuf(saved);
+  if (os.flags() & std::ios::left) {
+    os << x;
+    for (auto i = cb.count; i < w; ++i) os << os.fill();
+  } else {
+    for (auto i = cb.count; i < w; ++i) os << os.fill();
+    os << x;
+  }
+  return os;
+}
+
+} // namespace detail
+
 namespace accumulators {
 template <class CharT, class Traits, class W>
 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                               const sum<W>& x) {
-  os << "sum(" << x.large() << " + " << x.small() << ")";
-  return os;
+  if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
+  return detail::handle_nonzero_width(os, x);
 }
 
 template <class CharT, class Traits, class W>
 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                               const weighted_sum<W>& x) {
-  os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
-  return os;
+  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 W>
 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                               const mean<W>& x) {
-  os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
-  return os;
+  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 W>
 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                               const weighted_mean<W>& x) {
-  os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
-     << x.variance() << ")";
-  return os;
+  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 T>