Provide stream that ignores everything written to it

Use this stream when you want a straight code path, but also be able
to disable output at will.
This commit is contained in:
Roland Kaufmann 2013-09-25 14:00:27 +02:00
parent 8added1bc8
commit afdb330d54
3 changed files with 52 additions and 0 deletions

View File

@ -118,6 +118,7 @@ list (APPEND MAIN_SOURCE_FILES
opm/core/utility/WachspressCoord.cpp
opm/core/utility/miscUtilities.cpp
opm/core/utility/miscUtilitiesBlackoil.cpp
opm/core/utility/NullStream.cpp
opm/core/utility/parameters/Parameter.cpp
opm/core/utility/parameters/ParameterGroup.cpp
opm/core/utility/parameters/ParameterTools.cpp
@ -335,6 +336,7 @@ list (APPEND PUBLIC_HEADER_FILES
opm/core/utility/Factory.hpp
opm/core/utility/MonotCubicInterpolator.hpp
opm/core/utility/NonuniformTableLinear.hpp
opm/core/utility/NullStream.hpp
opm/core/utility/PolynomialUtils.hpp
opm/core/utility/RootFinders.hpp
opm/core/utility/SparseTable.hpp

View File

@ -0,0 +1,20 @@
// Copyright (C) 2013 Uni Research AS
// This file is licensed under the GNU General Public License v3.0
#include <opm/core/utility/NullStream.hpp>
#include <ostream>
#include <streambuf>
// buffer that ignores everything
// see <http://forums.codeguru.com/showthread.php?460071-ostream-bit-bucket>
struct NullBuf : public std::streambuf {};
static NullBuf null_buf_impl;
// link the stream up to the black hole buffer
struct NullStream : public std::ostream {
NullStream () : std::ostream (&null_buf_impl) {}
};
// create a singleton and point the reference to it
static NullStream null_stream_impl;
std::ostream& Opm::null_stream = null_stream_impl;

View File

@ -0,0 +1,30 @@
#ifndef OPM_NULLSTREAM_HEADER_INCLUDED
#define OPM_NULLSTREAM_HEADER_INCLUDED
// Copyright (C) 2013 Uni Research AS
// This file is licensed under the GNU General Public License v3.0
#include <iosfwd>
namespace Opm {
/**
* Output stream that ignores everything written to it.
*
* Use this stream if you want to disable output without having a
* lot of conditionals in your code.
*
* Since the null stream has no state, there is no point in
* instantiating your own; simply use this reference instead.
*
* @example
* @code{.cpp}
* std::ostream& outp = (quiet ? Opm::null_stream : std::cerr);
* outp << "Hello, World!" << std::endl;
* @endcode
*/
extern std::ostream& null_stream;
} /* namespace Opm */
#endif /* OPM_NULLSTREAM_HEADER_INCLUDED */