nullstream.hpp 3.9 KB

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