Files
biboumi/tests/io_tester.hpp
2016-07-04 17:53:53 +02:00

46 lines
835 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <ostream>
#include <sstream>
/**
* Redirects a stream into a streambuf until the object is destroyed.
*/
template <typename StreamType>
class IoTester
{
public:
IoTester(StreamType& ios):
stream{},
ios(ios),
old_buf(ios.rdbuf())
{
// Redirect the given os into our stringstream’s buf
this->ios.rdbuf(this->stream.rdbuf());
}
~IoTester()
{
this->ios.rdbuf(this->old_buf);
}
IoTester& operator=(const IoTester&) = delete;
IoTester& operator=(IoTester&&) = delete;
IoTester(const IoTester&) = delete;
IoTester(IoTester&&) = delete;
std::string str() const
{
return this->stream.str();
}
void set_string(const std::string& s)
{
this->stream.str(s);
}
private:
std::stringstream stream;
StreamType& ios;
std::streambuf* const old_buf;
};