CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 03 Dec 2018 16:11:40 +0100
branchv_0
changeset 7 95e647bf1922
parent 6 114521cca04a
child 8 ff115d10f901
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
include/relpipe/cli/CLI.h
--- a/include/relpipe/cli/CLI.h	Thu Nov 29 22:30:11 2018 +0100
+++ b/include/relpipe/cli/CLI.h	Mon Dec 03 16:11:40 2018 +0100
@@ -23,6 +23,7 @@
 #include <sstream>
 #include <locale>
 #include <codecvt>
+#include <iostream>
 
 using namespace std;
 
@@ -69,6 +70,14 @@
 	static const int EXIT_CODE_UNKNOWN_COMMAND = 4;
 	static const int EXIT_CODE_DATA_ERROR = 5;
 
+	/**
+	 * std::cin and std::cout are "tied" by default i.e. reading from std::cin causes flush of the std::cout.
+	 * Call this to "untie" them and have less write() calls (better buffering).
+	 */
+	static void untieStdIO() {
+		std::cin.tie(nullptr);
+	}
+
 private:
 	int* argc;
 	char*** argv;