[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master a102012 6/6: Generally avoid using null_strea
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master a102012 6/6: Generally avoid using null_stream() |
Date: |
Thu, 5 Aug 2021 17:10:00 -0400 (EDT) |
branch: master
commit a10201200bcabb409be97e0ef6265c5d9850b2b9
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Generally avoid using null_stream()
---
null_stream.cpp | 15 +++++++++++++++
system_command_wx.cpp | 4 ++--
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/null_stream.cpp b/null_stream.cpp
index 70eeca3..4df8a92 100644
--- a/null_stream.cpp
+++ b/null_stream.cpp
@@ -69,6 +69,21 @@ std::streambuf& null_streambuf()
return z;
}
+/// Reference to a static null stream--see caveat.
+///
+/// Caveat: The static object is in effect a global variable.
+/// Replacing its streambuf by calling rdbuf(another_streambuf)
+/// therefore has a global effect that is probably unwanted.
+/// Therefore, prefer to create a local object instead, e.g.:
+/// std::ostream local_os(&null_streambuf());
+/// local_os << "written to oblivion";
+/// local_os.rdbuf(std::cout.rdbuf); // effect is only local
+/// local_os << "written to stdout";
+///
+/// This is only intended to be used as a default ostream& argument:
+/// foo(std::ostream& os = null_stream());
+/// for functions that never change the streambuf.
+
std::ostream& null_stream()
{
static std::ostream z(&null_streambuf());
diff --git a/system_command_wx.cpp b/system_command_wx.cpp
index d401c9d..d9c550e 100644
--- a/system_command_wx.cpp
+++ b/system_command_wx.cpp
@@ -25,7 +25,6 @@
#include "alert.hpp"
#include "force_linking.hpp"
-#include "null_stream.hpp"
#include "timer.hpp"
#include <wx/app.h> // wxTheApp
@@ -88,7 +87,8 @@ void concrete_system_command(std::string const& cmd_line)
: nullptr;
;
bool const b = f && f->GetStatusBar();
- std::ostream& statusbar_if_available = b ? status() : null_stream();
+ std::ostream null_output(0);
+ std::ostream& statusbar_if_available = b ? status() : null_output;
statusbar_if_available << "Running..." << std::flush;
wxArrayString output;
- [lmi-commits] [lmi] master updated (7a6262f -> a102012), Greg Chicares, 2021/08/05
- [lmi-commits] [lmi] master 50c1d57 1/6: Don't write solve trace to statusbar, Greg Chicares, 2021/08/05
- [lmi-commits] [lmi] master 440c67d 2/6: Add a null_stream unit test, Greg Chicares, 2021/08/05
- [lmi-commits] [lmi] master 52456de 3/6: Demonstrate the speed advantage of 'badbit', Greg Chicares, 2021/08/05
- [lmi-commits] [lmi] master 048b2cc 4/6: Demonstrate a peril, Greg Chicares, 2021/08/05
- [lmi-commits] [lmi] master 4bc0910 5/6: Test std::ostream(nullptr) as well, Greg Chicares, 2021/08/05
- [lmi-commits] [lmi] master a102012 6/6: Generally avoid using null_stream(),
Greg Chicares <=