From f173dad56e4f72baa103c511a7572396d4590591 Mon Sep 17 00:00:00 2001 From: Roland Kaufmann Date: Wed, 25 Sep 2013 14:00:27 +0200 Subject: [PATCH] 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. --- opm/core/utility/NullStream.cpp | 20 ++++++++++++++++++++ opm/core/utility/NullStream.hpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 opm/core/utility/NullStream.cpp create mode 100644 opm/core/utility/NullStream.hpp diff --git a/opm/core/utility/NullStream.cpp b/opm/core/utility/NullStream.cpp new file mode 100644 index 000000000..3e3dfef94 --- /dev/null +++ b/opm/core/utility/NullStream.cpp @@ -0,0 +1,20 @@ +// Copyright (C) 2013 Uni Research AS +// This file is licensed under the GNU General Public License v3.0 + +#include +#include +#include + +// buffer that ignores everything +// see +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; diff --git a/opm/core/utility/NullStream.hpp b/opm/core/utility/NullStream.hpp new file mode 100644 index 000000000..edbb22217 --- /dev/null +++ b/opm/core/utility/NullStream.hpp @@ -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 + +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 */