Skip to content

Instantly share code, notes, and snippets.

@bolry
Created May 22, 2017 12:11
Show Gist options
  • Save bolry/56d649cd58b2e3fb304e7e83716ec8cb to your computer and use it in GitHub Desktop.
Save bolry/56d649cd58b2e3fb304e7e83716ec8cb to your computer and use it in GitHub Desktop.
Temporary Capture stream
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