1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#ifndef PBCOPPER_TESTS_OSTREAMREDIRECTOR_H
#define PBCOPPER_TESTS_OSTREAMREDIRECTOR_H
#include <iostream>
namespace tests {
//
// Redirects specified stream into some newBuffer, restoring the normal stream
// on destruction
//
struct OStreamRedirect
{
public:
OStreamRedirect(std::ostream& o,
std::streambuf* newBuffer)
: s_(o)
, oldBuffer_(o.rdbuf())
{ o.rdbuf(newBuffer); }
virtual ~OStreamRedirect(void)
{ s_.rdbuf(oldBuffer_); }
private:
std::ostream& s_;
std::streambuf* oldBuffer_;
};
// Convenience OStreamRedirect for std::cout
//
struct CoutRedirect : public OStreamRedirect
{
public:
CoutRedirect(std::streambuf* newBuffer)
: OStreamRedirect(std::cout, newBuffer)
{ }
};
// Convenience OStreamRedirect for std::cerr
//
struct CerrRedirect : public OStreamRedirect
{
public:
CerrRedirect(std::streambuf* newBuffer)
: OStreamRedirect(std::cerr, newBuffer)
{ }
};
} // namespace tests
#endif // PBCOPPER_TESTS_OSTREAMREDIRECTOR_H
|