• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 //  See http://www.boost.org/libs/test for the library home page.
8 //
9 //  File        : $RCSfile$
10 //
11 //  Version     : $Revision$
12 //
13 //  Description : simulate /dev/null stream
14 // ***************************************************************************
15 
16 #ifndef BOOST_TEST_UTILS_NULLSTREAM_HPP
17 #define BOOST_TEST_UTILS_NULLSTREAM_HPP
18 
19 // STL
20 #include <ostream>    // for std::basic_ostream
21 #include <streambuf>  // for std::basic_streambuf
22 #include <string>     // for std::char_traits
23 
24 // Boost
25 #include <boost/utility/base_from_member.hpp>
26 
27 #include <boost/test/detail/suppress_warnings.hpp>
28 
29 //____________________________________________________________________________//
30 
31 namespace boost {
32 
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++.
39 
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;
43 public:
44     // Types
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;
50 
51     // Use automatic default constructor and destructor
52 
53 protected:
54     // The default implementations of the miscellaneous virtual
55     // member functions are sufficient.
56 
57     // The default implementations of the input & putback virtual
58     // member functions, being nowhere but EOF, are sufficient.
59 
60     // The output virtual member functions need to be changed to
61     // accept anything without any problems, instead of being at EOF.
xsputn(char_type const *,::std::streamsize n)62     virtual  ::std::streamsize  xsputn( char_type const* /*s*/, ::std::streamsize n )   { return n; } // "s" is unused
overflow(int_type c=traits_type::eof ())63     virtual  int_type           overflow( int_type c = traits_type::eof() )         { return traits_type::not_eof( c ); }
64 };
65 
66 typedef basic_nullbuf<char>      nullbuf;
67 typedef basic_nullbuf<wchar_t>  wnullbuf;
68 
69 // ************************************************************************** //
70 // **************               basic_onullstream              ************** //
71 // ************************************************************************** //
72 //  Output streams based on basic_nullbuf.
73 
74 #ifdef BOOST_MSVC
75 # pragma warning(push)
76 # pragma warning(disable: 4355) // 'this' : used in base member initializer list
77 #endif
78 
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;
84 public:
85     // Constructor
basic_onullstream()86     basic_onullstream() : pbase_type(), base_type( &this->pbase_type::member ) {}
87 };
88 
89 #ifdef BOOST_MSVC
90 # pragma warning(default: 4355)
91 # pragma warning(pop)
92 #endif
93 
94 typedef basic_onullstream<char>      onullstream;
95 typedef basic_onullstream<wchar_t>  wonullstream;
96 
97 }  // namespace boost
98 
99 //____________________________________________________________________________//
100 
101 #include <boost/test/detail/enable_warnings.hpp>
102 
103 #endif  // BOOST_TEST_UTILS_NULLSTREAM_HPP
104