blob: d795d6843c878a3770409c29f7049767d46d886d (
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
|
#ifndef UNIQUE_FD_HH
#define UNIQUE_FD_HH
#include <cstddef>
class unique_fd {
public:
constexpr unique_fd() noexcept
: fd_(-1) {}
constexpr unique_fd(std::nullptr_t) noexcept
: fd_(-1) {}
explicit unique_fd(int fd) noexcept
: fd_(fd) {}
unique_fd(unique_fd&& fd) noexcept
: fd_(fd.release()) {}
~unique_fd() { reset(); }
unique_fd& operator=(unique_fd&& fd) noexcept {
reset(fd.release());
return *this;
}
unique_fd& operator=(std::nullptr_t) noexcept {
reset();
return *this;
}
int get() const noexcept { return fd_; }
int operator*() const { return get(); }
explicit operator bool() const noexcept { return fd_ >= 0; }
int release() noexcept {
int ret = fd_;
fd_ = -1;
return ret;
}
void reset(int fd = -1);
unique_fd dup();
private:
unique_fd(unique_fd const&) = delete;
unique_fd& operator=(unique_fd const&) = delete;
int fd_;
};
#endif // UNIQUE_FD_HH
|