File: OStreamRedirector.h

package info (click to toggle)
pbcopper 0.4.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,416 kB
  • sloc: cpp: 47,036; ansic: 1,430; python: 331; sh: 176; makefile: 7
file content (51 lines) | stat: -rw-r--r-- 1,038 bytes parent folder | download
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