ostream.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
  7. #define BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
  8. #include <boost/histogram/detail/counting_streambuf.hpp>
  9. #include <boost/histogram/fwd.hpp>
  10. #include <ios>
  11. /**
  12. \file boost/histogram/accumulators/ostream.hpp
  13. Simple streaming operators for the builtin accumulator types.
  14. The text representation is not guaranteed to be stable between versions of
  15. Boost.Histogram. This header is only included by
  16. [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
  17. To you use your own, include your own implementation instead of this header and do not
  18. include
  19. [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
  20. */
  21. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  22. namespace boost {
  23. namespace histogram {
  24. namespace detail {
  25. template <class CharT, class Traits, class T>
  26. std::basic_ostream<CharT, Traits>& handle_nonzero_width(
  27. std::basic_ostream<CharT, Traits>& os, const T& x) {
  28. const auto w = os.width();
  29. os.width(0);
  30. counting_streambuf<CharT, Traits> cb;
  31. const auto saved = os.rdbuf(&cb);
  32. os << x;
  33. os.rdbuf(saved);
  34. if (os.flags() & std::ios::left) {
  35. os << x;
  36. for (auto i = cb.count; i < w; ++i) os << os.fill();
  37. } else {
  38. for (auto i = cb.count; i < w; ++i) os << os.fill();
  39. os << x;
  40. }
  41. return os;
  42. }
  43. } // namespace detail
  44. namespace accumulators {
  45. template <class CharT, class Traits, class W>
  46. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  47. const sum<W>& x) {
  48. if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
  49. return detail::handle_nonzero_width(os, x);
  50. }
  51. template <class CharT, class Traits, class W>
  52. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  53. const weighted_sum<W>& x) {
  54. if (os.width() == 0)
  55. return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
  56. return detail::handle_nonzero_width(os, x);
  57. }
  58. template <class CharT, class Traits, class W>
  59. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  60. const mean<W>& x) {
  61. if (os.width() == 0)
  62. return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
  63. return detail::handle_nonzero_width(os, x);
  64. }
  65. template <class CharT, class Traits, class W>
  66. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  67. const weighted_mean<W>& x) {
  68. if (os.width() == 0)
  69. return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
  70. << x.variance() << ")";
  71. return detail::handle_nonzero_width(os, x);
  72. }
  73. template <class CharT, class Traits, class T>
  74. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  75. const thread_safe<T>& x) {
  76. os << x.load();
  77. return os;
  78. }
  79. } // namespace accumulators
  80. } // namespace histogram
  81. } // namespace boost
  82. #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED
  83. #endif