ostream.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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. std::streamsize count = 0;
  31. {
  32. auto g = make_count_guard(os, count);
  33. os << x;
  34. }
  35. if (os.flags() & std::ios::left) {
  36. os << x;
  37. for (auto i = count; i < w; ++i) os << os.fill();
  38. } else {
  39. for (auto i = count; i < w; ++i) os << os.fill();
  40. os << x;
  41. }
  42. return os;
  43. }
  44. } // namespace detail
  45. namespace accumulators {
  46. template <class CharT, class Traits, class U>
  47. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  48. const count<U>& x) {
  49. return os << x.value();
  50. }
  51. template <class CharT, class Traits, class U>
  52. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  53. const sum<U>& x) {
  54. if (os.width() == 0) return os << "sum(" << x.large() << " + " << x.small() << ")";
  55. return detail::handle_nonzero_width(os, x);
  56. }
  57. template <class CharT, class Traits, class U>
  58. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  59. const weighted_sum<U>& x) {
  60. if (os.width() == 0)
  61. return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
  62. return detail::handle_nonzero_width(os, x);
  63. }
  64. template <class CharT, class Traits, class U>
  65. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  66. const mean<U>& x) {
  67. if (os.width() == 0)
  68. return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
  69. return detail::handle_nonzero_width(os, x);
  70. }
  71. template <class CharT, class Traits, class U>
  72. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  73. const weighted_mean<U>& x) {
  74. if (os.width() == 0)
  75. return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
  76. << x.variance() << ")";
  77. return detail::handle_nonzero_width(os, x);
  78. }
  79. template <class CharT, class Traits, class T>
  80. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
  81. const thread_safe<T>& x) {
  82. os << x.load();
  83. return os;
  84. }
  85. } // namespace accumulators
  86. } // namespace histogram
  87. } // namespace boost
  88. #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED
  89. #endif