1 // (C) Copyright Gennadiy Rozental 2002-2003.
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.
9 // File : $RCSfile: nullstream.hpp,v $
11 // Version : $Revision: 1.9 $
13 // Description : simulate /dev/null stream
14 // ***************************************************************************
16 #ifndef BOOST_NULLSTREAM_HPP_071894GER
17 #define BOOST_NULLSTREAM_HPP_071894GER
19 #include <ostream> // for std::basic_ostream
20 #include <streambuf> // for std::basic_streambuf
21 #include <string> // for std::char_traits
23 #include <boost/utility/base_from_member.hpp>
27 // ************************************************************************** //
28 // ************** basic_nullbuf ************** //
29 // ************************************************************************** //
30 // Class for a buffer that reads nothing and writes to nothing.
31 // Idea from an Usenet post by Tom <the_wid@my-deja.com> at
32 // 27 Oct 2000 14:06:21 GMT on comp.lang.c++.
34 template<typename CharType, class CharTraits = ::std::char_traits<CharType> >
35 class basic_nullbuf : public ::std::basic_streambuf<CharType, CharTraits> {
36 typedef ::std::basic_streambuf<CharType, CharTraits> base_type;
39 typedef typename base_type::char_type char_type;
40 typedef typename base_type::traits_type traits_type;
41 typedef typename base_type::int_type int_type;
42 typedef typename base_type::pos_type pos_type;
43 typedef typename base_type::off_type off_type;
45 // Use automatic default constructor and destructor
48 // The default implementations of the miscellaneous virtual
49 // member functions are sufficient.
51 // The default implementations of the input & putback virtual
52 // member functions, being nowhere but EOF, are sufficient.
54 // The output virtual member functions need to be changed to
55 // accept anything without any problems, instead of being at EOF.
56 virtual ::std::streamsize xsputn( char_type const* /*s*/, ::std::streamsize n ) { return n; } // "s" is unused
57 virtual int_type overflow( int_type c = traits_type::eof() ) { return traits_type::not_eof( c ); }
60 typedef basic_nullbuf<char> nullbuf;
61 typedef basic_nullbuf<wchar_t> wnullbuf;
63 // ************************************************************************** //
64 // ************** basic_onullstream ************** //
65 // ************************************************************************** //
66 // Output streams based on basic_nullbuf.
68 template< typename CharType, class CharTraits = ::std::char_traits<CharType> >
69 class basic_onullstream : private boost::base_from_member<basic_nullbuf<CharType, CharTraits> >
70 , public ::std::basic_ostream<CharType, CharTraits> {
71 typedef boost::base_from_member<basic_nullbuf<CharType, CharTraits> > pbase_type;
72 typedef ::std::basic_ostream<CharType, CharTraits> base_type;
75 basic_onullstream() : pbase_type(), base_type( &this->pbase_type::member ) {}
78 typedef basic_onullstream<char> onullstream;
79 typedef basic_onullstream<wchar_t> wonullstream;
83 // ***************************************************************************
86 // $Log: nullstream.hpp,v $
87 // Revision 1.9 2004/07/19 12:21:08 rogeeff
90 // Revision 1.8 2004/05/21 06:19:35 rogeeff
93 // Revision 1.7 2003/12/01 00:41:56 rogeeff
94 // prerelease cleaning
96 // ***************************************************************************
98 #endif // BOOST_NULLSTREAM_HPP_071894GER