nullstream.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // (C) Copyright Gennadiy Rozental 2002-2008.
  2. // (C) Copyright Daryle Walker 2000-2001.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/test for the library home page.
  7. //
  8. // File : $RCSfile$
  9. //
  10. // Version : $Revision: 49312 $
  11. //
  12. // Description : simulate /dev/null stream
  13. // ***************************************************************************
  14. #ifndef BOOST_NULLSTREAM_HPP_071894GER
  15. #define BOOST_NULLSTREAM_HPP_071894GER
  16. #include <ostream> // for std::basic_ostream
  17. #include <streambuf> // for std::basic_streambuf
  18. #include <string> // for std::char_traits
  19. #include <boost/utility/base_from_member.hpp>
  20. #include <boost/test/detail/suppress_warnings.hpp>
  21. //____________________________________________________________________________//
  22. namespace boost {
  23. // ************************************************************************** //
  24. // ************** basic_nullbuf ************** //
  25. // ************************************************************************** //
  26. // Class for a buffer that reads nothing and writes to nothing.
  27. // Idea from an Usenet post by Tom <the_wid@my-deja.com> at
  28. // 27 Oct 2000 14:06:21 GMT on comp.lang.c++.
  29. template<typename CharType, class CharTraits = ::std::char_traits<CharType> >
  30. class basic_nullbuf : public ::std::basic_streambuf<CharType, CharTraits> {
  31. typedef ::std::basic_streambuf<CharType, CharTraits> base_type;
  32. public:
  33. // Types
  34. typedef typename base_type::char_type char_type;
  35. typedef typename base_type::traits_type traits_type;
  36. typedef typename base_type::int_type int_type;
  37. typedef typename base_type::pos_type pos_type;
  38. typedef typename base_type::off_type off_type;
  39. // Use automatic default constructor and destructor
  40. protected:
  41. // The default implementations of the miscellaneous virtual
  42. // member functions are sufficient.
  43. // The default implementations of the input & putback virtual
  44. // member functions, being nowhere but EOF, are sufficient.
  45. // The output virtual member functions need to be changed to
  46. // accept anything without any problems, instead of being at EOF.
  47. virtual ::std::streamsize xsputn( char_type const* /*s*/, ::std::streamsize n ) { return n; } // "s" is unused
  48. virtual int_type overflow( int_type c = traits_type::eof() ) { return traits_type::not_eof( c ); }
  49. };
  50. typedef basic_nullbuf<char> nullbuf;
  51. typedef basic_nullbuf<wchar_t> wnullbuf;
  52. // ************************************************************************** //
  53. // ************** basic_onullstream ************** //
  54. // ************************************************************************** //
  55. // Output streams based on basic_nullbuf.
  56. #ifdef BOOST_MSVC
  57. # pragma warning(push)
  58. # pragma warning(disable: 4355) // 'this' : used in base member initializer list
  59. #endif
  60. template< typename CharType, class CharTraits = ::std::char_traits<CharType> >
  61. class basic_onullstream : private boost::base_from_member<basic_nullbuf<CharType, CharTraits> >
  62. , public ::std::basic_ostream<CharType, CharTraits> {
  63. typedef boost::base_from_member<basic_nullbuf<CharType, CharTraits> > pbase_type;
  64. typedef ::std::basic_ostream<CharType, CharTraits> base_type;
  65. public:
  66. // Constructor
  67. basic_onullstream() : pbase_type(), base_type( &this->pbase_type::member ) {}
  68. };
  69. #ifdef BOOST_MSVC
  70. # pragma warning(default: 4355)
  71. #endif
  72. typedef basic_onullstream<char> onullstream;
  73. typedef basic_onullstream<wchar_t> wonullstream;
  74. } // namespace boost
  75. //____________________________________________________________________________//
  76. #include <boost/test/detail/enable_warnings.hpp>
  77. #endif // BOOST_NULLSTREAM_HPP_071894GER