• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <boost/core/exchange.hpp>
11 #include <ostream>
12 #include <streambuf>
13 
14 namespace boost {
15 namespace histogram {
16 namespace detail {
17 
18 // detect how many characters will be printed by formatted output
19 template <class CharT, class Traits = std::char_traits<CharT>>
20 struct counting_streambuf : std::basic_streambuf<CharT, Traits> {
21   using base_t = std::basic_streambuf<CharT, Traits>;
22   using typename base_t::char_type;
23   using typename base_t::int_type;
24 
25   std::streamsize* p_count;
26 
counting_streambufboost::histogram::detail::counting_streambuf27   counting_streambuf(std::streamsize& c) : p_count(&c) {}
28 
xsputnboost::histogram::detail::counting_streambuf29   std::streamsize xsputn(const char_type* /* s */, std::streamsize n) override {
30     *p_count += n;
31     return n;
32   }
33 
overflowboost::histogram::detail::counting_streambuf34   int_type overflow(int_type ch) override {
35     ++*p_count;
36     return ch;
37   }
38 };
39 
40 template <class C, class T>
41 struct count_guard {
42   using bos = std::basic_ostream<C, T>;
43   using bsb = std::basic_streambuf<C, T>;
44 
45   counting_streambuf<C, T> csb;
46   bos* p_os;
47   bsb* p_rdbuf;
48 
count_guardboost::histogram::detail::count_guard49   count_guard(bos& os, std::streamsize& s) : csb(s), p_os(&os), p_rdbuf(os.rdbuf(&csb)) {}
50 
count_guardboost::histogram::detail::count_guard51   count_guard(count_guard&& o)
52       : csb(o.csb), p_os(boost::exchange(o.p_os, nullptr)), p_rdbuf(o.p_rdbuf) {}
53 
operator =boost::histogram::detail::count_guard54   count_guard& operator=(count_guard&& o) {
55     if (this != &o) {
56       csb = std::move(o.csb);
57       p_os = boost::exchange(o.p_os, nullptr);
58       p_rdbuf = o.p_rdbuf;
59     }
60     return *this;
61   }
62 
~count_guardboost::histogram::detail::count_guard63   ~count_guard() {
64     if (p_os) p_os->rdbuf(p_rdbuf);
65   }
66 };
67 
68 template <class C, class T>
make_count_guard(std::basic_ostream<C,T> & os,std::streamsize & s)69 count_guard<C, T> make_count_guard(std::basic_ostream<C, T>& os, std::streamsize& s) {
70   return {os, s};
71 }
72 
73 } // namespace detail
74 } // namespace histogram
75 } // namespace boost
76 
77 #endif
78