blob: c39fa2ee2424822d62793d0a5a321c04500c501b (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
#ifndef IO_HH
#define IO_HH
#include <stddef.h>
namespace io {
class pipe {
public:
pipe();
pipe(pipe&& other);
~pipe();
pipe& operator=(pipe&& other) {
reset();
swap(other);
return *this;
}
bool open();
explicit operator bool() const {
return read() != -1;
}
void reset();
void swap(pipe& pipe);
int read() const {
return fd_[0];
}
int write() const {
return fd_[1];
}
private:
pipe(pipe const&) = delete;
pipe& operator=(pipe const&) = delete;
int fd_[2];
};
class unique_fd {
public:
unique_fd();
explicit unique_fd(int fd);
unique_fd(unique_fd&& fd);
~unique_fd();
unique_fd& operator=(unique_fd&& fd) {
reset();
swap(fd);
return *this;
}
int get() const {
return fd_;
}
explicit operator bool() const {
return get() != -1;
}
void reset();
void reset(int fd);
void swap(unique_fd& fd);
int release();
private:
unique_fd(unique_fd const& fd) = delete;
unique_fd& operator=(unique_fd const& fd) = delete;
int fd_;
};
bool read(pipe const& pipe, void* dst, size_t max, size_t* got = nullptr);
bool write(pipe const& pipe, void const* src, size_t size,
size_t* wrote = nullptr);
bool read(unique_fd const& fd, void* dst, size_t max, size_t* got = nullptr);
bool write(unique_fd const& fd, void const* src, size_t size,
size_t* wrote = nullptr);
} // namespace
#endif // IO_HH
|