nullstream.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright 2021 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_IO_NULLSTREAM_HPP
  8. #define BOOST_IO_NULLSTREAM_HPP
  9. #include <boost/config.hpp>
  10. #include <ostream>
  11. #include <streambuf>
  12. namespace boost {
  13. namespace io {
  14. template<class CharT, class Traits = std::char_traits<CharT> >
  15. class basic_nullbuf
  16. : public std::basic_streambuf<CharT, Traits> {
  17. protected:
  18. typename Traits::int_type overflow(typename Traits::int_type c)
  19. BOOST_OVERRIDE {
  20. return Traits::not_eof(c);
  21. }
  22. std::streamsize xsputn(const CharT*, std::streamsize n) BOOST_OVERRIDE {
  23. return n;
  24. }
  25. };
  26. namespace detail {
  27. template<class CharT, class Traits>
  28. struct nullbuf {
  29. boost::io::basic_nullbuf<CharT, Traits> buf;
  30. };
  31. } /* detail */
  32. template<class CharT, class Traits = std::char_traits<CharT> >
  33. class basic_onullstream
  34. : detail::nullbuf<CharT, Traits>
  35. , public std::basic_ostream<CharT, Traits> {
  36. public:
  37. basic_onullstream()
  38. : std::basic_ostream<CharT, Traits>(&(detail::nullbuf<CharT,
  39. Traits>::buf)) { }
  40. };
  41. typedef basic_onullstream<char> onullstream;
  42. typedef basic_onullstream<wchar_t> wonullstream;
  43. } /* io */
  44. } /* boost */
  45. #endif