Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / histogram / detail / counting_streambuf.hpp
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
7 #ifndef BOOST_HISTOGRAM_DETAIL_COUNTING_STREAMBUF_HPP
8 #define BOOST_HISTOGRAM_DETAIL_COUNTING_STREAMBUF_HPP
9
10 #include <streambuf>
11
12 namespace boost {
13 namespace histogram {
14 namespace detail {
15
16 // detect how many characters will be printed by formatted output
17 template <class CharT, class Traits = std::char_traits<CharT>>
18 struct counting_streambuf : std::basic_streambuf<CharT, Traits> {
19   using base_t = std::basic_streambuf<CharT, Traits>;
20   using typename base_t::char_type;
21   using typename base_t::int_type;
22
23   std::streamsize count = 0;
24
25   std::streamsize xsputn(const char_type* /* s */, std::streamsize n) override {
26     count += n;
27     return n;
28   }
29
30   int_type overflow(int_type ch) override {
31     ++count;
32     return ch;
33   }
34 };
35
36 } // namespace detail
37 } // namespace histogram
38 } // namespace boost
39
40 #endif