Created
May 22, 2017 12:11
-
-
Save bolry/56d649cd58b2e3fb304e7e83716ec8cb to your computer and use it in GitHub Desktop.
Temporary Capture stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CaptureStream { | |
private: | |
std::basic_ios<char>& m_stream; | |
std::streambuf* m_orig_streambuf; | |
std::stringstream m_buffer; | |
CaptureStream(CaptureStream const&) = delete; | |
CaptureStream& operator=(CaptureStream const&) = delete; | |
public: | |
CaptureStream(std::basic_ios<char>& stream) : | |
m_stream(stream), | |
m_orig_streambuf(stream.rdbuf()) | |
{ | |
// Set new streambuf for stream | |
stream.rdbuf(m_buffer.rdbuf()); | |
} | |
~CaptureStream() | |
{ | |
// Restore old streambuf for stream | |
m_stream.rdbuf(m_orig_streambuf); | |
m_orig_streambuf = nullptr; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment