-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.h
54 lines (41 loc) · 669 Bytes
/
pipe.h
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
#ifndef PIPE_WRAP_H
#define PIPE_WRAP_H
#include <unistd.h>
#include <fcntl.h>
#include <stdexcept>
namespace mc
{
struct pipe
{
pipe()
{
if (::pipe(fd_) == -1) {
throw std::runtime_error("unable create pipe");
}
set_fcntl(fd_[0], O_NONBLOCK);
}
~pipe()
{
::close(fd_[0]);
::close(fd_[1]);
}
int read_end() const
{
return fd_[0];
}
int write_end() const
{
return fd_[1];
}
private:
int fd_[0];
void set_fcntl(int fd, int flags)
{
int flg = ::fcntl(fd, F_GETFL);
::fcntl(fd, F_SETFL, flg | O_NONBLOCK);
}
pipe(const pipe&) = delete;
pipe& operator=(const pipe&) = delete;
};
}
#endif