counting_streambuf.hpp 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2019 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_DETAIL_COUNTING_STREAMBUF_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_COUNTING_STREAMBUF_HPP
  8. #include <streambuf>
  9. namespace boost {
  10. namespace histogram {
  11. namespace detail {
  12. // detect how many characters will be printed by formatted output
  13. template <class CharT, class Traits = std::char_traits<CharT>>
  14. struct counting_streambuf : std::basic_streambuf<CharT, Traits> {
  15. using base_t = std::basic_streambuf<CharT, Traits>;
  16. using typename base_t::char_type;
  17. using typename base_t::int_type;
  18. std::streamsize count = 0;
  19. std::streamsize xsputn(const char_type* /* s */, std::streamsize n) override {
  20. count += n;
  21. return n;
  22. }
  23. int_type overflow(int_type ch) override {
  24. ++count;
  25. return ch;
  26. }
  27. };
  28. } // namespace detail
  29. } // namespace histogram
  30. } // namespace boost
  31. #endif