00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/common/NullOutputStream.hpp $ 00003 // $Id: NullOutputStream.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the NullOutputStream class. 00018 /// \details Credit for this code goes to Dietmar Kuehl for a July 22, 2005 response to a post on 00019 /// http://bytes.com/topic/c/answers/127843-null-output-stream#post444998. 00020 00021 #ifndef TORC_COMMON_NULLOUTPUTSTREAM_HPP 00022 #define TORC_COMMON_NULLOUTPUTSTREAM_HPP 00023 00024 #include <ostream> 00025 00026 namespace torc { 00027 namespace common { 00028 00029 /// \brief Null stream buffer used by NullOutputStream. 00030 /// \details Note that this stream buffer is not necessary for the NullOutputStream if we do 00031 /// not care about the stream's badbit being set. 00032 class NullStreamBuffer : public std::streambuf { 00033 /// \brief Responds that we never overflow our buffer, and can thus keep writing 00034 /// indefinitely. 00035 int overflow(int c) { return traits_type::not_eof(c); } 00036 }; 00037 00038 /// \brief Output stream that discards everything it receives. 00039 class NullOutputStream : public std::ostream { 00040 protected: 00041 // members 00042 /// \brief The null stream buffer that supports our operations. 00043 NullStreamBuffer mNullStreamBuf; 00044 public: 00045 // constructors 00046 /// \brief Public constructor. 00047 /// \details Constructs this output stream with a null stream buffer. 00048 NullOutputStream(void) : std::ios(&mNullStreamBuf), std::ostream(&mNullStreamBuf) {} 00049 }; 00050 00051 extern NullOutputStream cnull; 00052 00053 } // namespace common 00054 } // namespace torc 00055 00056 #endif // TORC_COMMON_NULLOUTPUTSTREAM_HPP