-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomposeStream.h
More file actions
40 lines (36 loc) · 909 Bytes
/
composeStream.h
File metadata and controls
40 lines (36 loc) · 909 Bytes
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
// ComposeStream
// Ties two std::ostream together.
// Copied from https://stackoverflow.com/a/1761027/58456
//
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <vector>
class ComposeStream : public std::ostream {
struct ComposeBuffer : public std::streambuf {
void addBuffer(std::streambuf* buf)
{
bufs.push_back(buf);
}
virtual int overflow(int c)
{
std::for_each(bufs.begin(), bufs.end(), std::bind2nd(std::mem_fun(&std::streambuf::sputc), c));
return c;
}
private:
std::vector<std::streambuf*> bufs;
};
ComposeBuffer myBuffer;
public:
ComposeStream()
: std::ostream(NULL)
{
std::ostream::rdbuf(&myBuffer);
}
void linkStream(std::ostream& out)
{
out.flush();
myBuffer.addBuffer(out.rdbuf());
}
};