nullstream.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2021-2022 Glen Joseph Fernandes
  3. ([email protected])
  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. class nullbuf {
  29. public:
  30. boost::io::basic_nullbuf<CharT, Traits>* buf() {
  31. return &buf_;
  32. }
  33. private:
  34. boost::io::basic_nullbuf<CharT, Traits> buf_;
  35. };
  36. } /* detail */
  37. template<class CharT, class Traits = std::char_traits<CharT> >
  38. class basic_onullstream
  39. : detail::nullbuf<CharT, Traits>
  40. , public std::basic_ostream<CharT, Traits> {
  41. public:
  42. basic_onullstream()
  43. : std::basic_ostream<CharT, Traits>(detail::nullbuf<CharT,
  44. Traits>::buf()) { }
  45. };
  46. typedef basic_onullstream<char> onullstream;
  47. typedef basic_onullstream<wchar_t> wonullstream;
  48. } /* io */
  49. } /* boost */
  50. #endif