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)
7 // See http://www.boost.org/libs/test for the library home page.
11 // Version : $Revision$
13 // Description : simulate /dev/null stream
14 // ***************************************************************************
16 #ifndef BOOST_TEST_UTILS_NULLSTREAM_HPP
17 #define BOOST_TEST_UTILS_NULLSTREAM_HPP
20 #include <ostream> // for std::basic_ostream
21 #include <streambuf> // for std::basic_streambuf
22 #include <string> // for std::char_traits
25 #include <boost/utility/base_from_member.hpp>
27 #include <boost/test/detail/suppress_warnings.hpp>
29 //____________________________________________________________________________//
33 // ************************************************************************** //
34 // ************** basic_nullbuf ************** //
35 // ************************************************************************** //
36 // Class for a buffer that reads nothing and writes to nothing.
37 // Idea from an Usenet post by Tom <the_wid@my-deja.com> at
38 // 27 Oct 2000 14:06:21 GMT on comp.lang.c++.
40 template<typename CharType, class CharTraits = ::std::char_traits<CharType> >
41 class basic_nullbuf : public ::std::basic_streambuf<CharType, CharTraits> {
42 typedef ::std::basic_streambuf<CharType, CharTraits> base_type;
45 typedef typename base_type::char_type char_type;
46 typedef typename base_type::traits_type traits_type;
47 typedef typename base_type::int_type int_type;
48 typedef typename base_type::pos_type pos_type;
49 typedef typename base_type::off_type off_type;
51 // Use automatic default constructor and destructor
54 // The default implementations of the miscellaneous virtual
55 // member functions are sufficient.
57 // The default implementations of the input & putback virtual
58 // member functions, being nowhere but EOF, are sufficient.
60 // The output virtual member functions need to be changed to
61 // accept anything without any problems, instead of being at EOF.
62 virtual ::std::streamsize xsputn( char_type const* /*s*/, ::std::streamsize n ) { return n; } // "s" is unused
63 virtual int_type overflow( int_type c = traits_type::eof() ) { return traits_type::not_eof( c ); }
66 typedef basic_nullbuf<char> nullbuf;
67 typedef basic_nullbuf<wchar_t> wnullbuf;
69 // ************************************************************************** //
70 // ************** basic_onullstream ************** //
71 // ************************************************************************** //
72 // Output streams based on basic_nullbuf.
75 # pragma warning(push)
76 # pragma warning(disable: 4355) // 'this' : used in base member initializer list
79 template< typename CharType, class CharTraits = ::std::char_traits<CharType> >
80 class basic_onullstream : private boost::base_from_member<basic_nullbuf<CharType, CharTraits> >
81 , public ::std::basic_ostream<CharType, CharTraits> {
82 typedef boost::base_from_member<basic_nullbuf<CharType, CharTraits> > pbase_type;
83 typedef ::std::basic_ostream<CharType, CharTraits> base_type;
86 basic_onullstream() : pbase_type(), base_type( &this->pbase_type::member ) {}
90 # pragma warning(default: 4355)
94 typedef basic_onullstream<char> onullstream;
95 typedef basic_onullstream<wchar_t> wonullstream;
99 //____________________________________________________________________________//
101 #include <boost/test/detail/enable_warnings.hpp>
103 #endif // BOOST_TEST_UTILS_NULLSTREAM_HPP