mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
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:
parent
9193105410
commit
f173dad56e
20
opm/core/utility/NullStream.cpp
Normal file
20
opm/core/utility/NullStream.cpp
Normal 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;
|
30
opm/core/utility/NullStream.hpp
Normal file
30
opm/core/utility/NullStream.hpp
Normal 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 */
|
Loading…
Reference in New Issue
Block a user