-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPipeStream.hpp
58 lines (51 loc) · 1.4 KB
/
CPipeStream.hpp
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
52
53
54
55
56
57
58
#pragma once
#include <fileapi.h>
#include <iostream>
#include <sstream>
#include <windows.h>
//------------------------------------------------------------------------------
//! @brief This class describes a pipe stream buffer.
//------------------------------------------------------------------------------
class CPipeStreamBuf : public std::stringbuf
{
HANDLE mhWrite;
public:
CPipeStreamBuf(HANDLE h) : mhWrite(h)
{}
virtual ~CPipeStreamBuf()
{
if (pbase() != pptr())
{
putOutput();
}
}
// When we sync the stream with the output.
// 1) Output the buffer
// 2) Reset the buffer
// 3) flush the actual output stream we are using.
int sync() override
{
putOutput();
return 0;
}
void putOutput()
{
// Called by destructor.
// destructor can not call virtual methods.
DWORD written = 0;
WriteFile(mhWrite, str().c_str(), str().size(), &written, nullptr);
str("");
}
};
//------------------------------------------------------------------------------
//! @brief This class describes a pipe stream.
//------------------------------------------------------------------------------
class CPipeStream : public std::ostream
{
CPipeStreamBuf buffer;
public:
CPipeStream() = delete;
CPipeStream(HANDLE h) :std::ostream(&buffer), buffer(h)
{
}
};