-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_fd.h
More file actions
65 lines (46 loc) · 1.21 KB
/
Copy pathunique_fd.h
File metadata and controls
65 lines (46 loc) · 1.21 KB
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
/*
* unique_fd.h
* Copyright (C) 2022 youfa.song <vsyfar@gmail.com>
*
* Distributed under terms of the GPLv2 license.
*/
#ifndef UNIQUE_FD_H
#define UNIQUE_FD_H
#include <unistd.h>
#include "base/constructor_magic.h"
namespace ave {
namespace base {
class unique_fd final {
public:
unique_fd() : value_(-1) {}
explicit unique_fd(int value) : value_(value) {}
~unique_fd() { clear(); }
unique_fd(unique_fd&& other) : value_(other.release()) {}
unique_fd& operator=(unique_fd&& s) {
reset(s.release());
return *this;
}
void reset(int new_value) {
if (value_ != -1) {
// Even if close(2) fails with EINTR, the fd will have been closed.
// Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone
// else's fd.
// http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
close(value_);
}
value_ = new_value;
}
void clear() { reset(-1); }
int get() const { return value_; }
int release() __attribute__((warn_unused_result)) {
int ret = value_;
value_ = -1;
return ret;
}
private:
int value_;
AVE_DISALLOW_COPY_AND_ASSIGN(unique_fd);
};
} // namespace base
} /* namespace ave */
#endif /* !UNIQUE_FD_H */