1 #ifndef THREAD_SAFE_IOSTREAM
2 #define THREAD_SAFE_IOSTREAM
33 static std::mutex _mutex;
34 std::ostringstream _buffer;
42 std::lock_guard<std::mutex> lock(_mutex);
48 std::cout <<
"Invalid input !" << std::endl;
52 std::cin.ignore(INT32_MAX,
'\n');
67 std::string line = _buffer.str();
68 std::string::size_type pos = 0;
69 std::string::size_type cursor = 0;
70 if (line.find(
'\n') == std::string::npos)
73 std::lock_guard<std::mutex> lock(_mutex);
75 while ((pos = line.find(
'\n', cursor)) != std::string::npos)
78 std::cout << line.substr(cursor, pos - cursor) <<
'\n';
84 if (cursor < line.length())
85 std::cout << line.substr(cursor);
89 void setPrefix(
const std::string& prefix);
92 void prompt(
const std::string& question, T& dest)
95 std::lock_guard<std::mutex> lock(_mutex);
96 std::cout << question;
void setPrefix(const std::string &prefix)
void prompt(const std::string &question, T &dest)
ThreadSafeIOStream & operator<<(std::ostream &(*funct)(std::ostream &))
ThreadSafeIOStream & operator<<(const T &value)
ThreadSafeIOStream & operator>>(T &value)
thread_local ThreadSafeIOStream threadSafeCout