torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NullOutputStream.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the NullOutputStream class.
18 /// \details Credit for this code goes to Dietmar Kuehl for a July 22, 2005 response to a post on
19 /// http://bytes.com/topic/c/answers/127843-null-output-stream#post444998.
20 
21 #ifndef TORC_COMMON_NULLOUTPUTSTREAM_HPP
22 #define TORC_COMMON_NULLOUTPUTSTREAM_HPP
23 
24 #include <ostream>
25 
26 namespace torc {
27 namespace common {
28 
29  /// \brief Null stream buffer used by NullOutputStream.
30  /// \details Note that this stream buffer is not necessary for the NullOutputStream if we do
31  /// not care about the stream's badbit being set.
32  class NullStreamBuffer : public std::streambuf {
33  /// \brief Responds that we never overflow our buffer, and can thus keep writing
34  /// indefinitely.
35  int overflow(int c) { return traits_type::not_eof(c); }
36  };
37 
38  /// \brief Output stream that discards everything it receives.
39  class NullOutputStream : public std::ostream {
40  protected:
41  // members
42  /// \brief The null stream buffer that supports our operations.
44  public:
45  // constructors
46  /// \brief Public constructor.
47  /// \details Constructs this output stream with a null stream buffer.
48  NullOutputStream(void) : std::ios(&mNullStreamBuf), std::ostream(&mNullStreamBuf) {}
49  };
50 
51  extern NullOutputStream cnull;
52 
53 } // namespace common
54 } // namespace torc
55 
56 #endif // TORC_COMMON_NULLOUTPUTSTREAM_HPP
NullOutputStream(void)
Public constructor.
Output stream that discards everything it receives.
Null stream buffer used by NullOutputStream.
NullStreamBuffer mNullStreamBuf
The null stream buffer that supports our operations.
int overflow(int c)
Responds that we never overflow our buffer, and can thus keep writing indefinitely.
NullOutputStream cnull
Global null output stream.