|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
5: //
6:
1.1.1.4 ! root 7: //
! 8: // autofd
! 9: //
! 10:
1.1.1.2 root 11: #pragma once
12:
1.1 root 13: #include <unistd.h>
14:
15: // 自動変数みたいな生存期間を持つディスクリプタ
16: class autofd
17: {
18: public:
19: autofd() {
20: fd = -1;
21: }
22: autofd(int fd_) {
23: fd = fd_;
24: }
25:
26: ~autofd() {
1.1.1.3 root 27: Close();
1.1 root 28: }
29:
30: autofd& operator=(int fd_) {
31: fd = fd_;
32: return *this;
33: }
34: operator int() const {
35: return fd;
36: }
37:
38: // 明示的にクローズする
39: int Close() {
40: int rv = 0;
1.1.1.3 root 41: if (Valid()) {
1.1 root 42: rv = close(fd);
43: fd = -1;
44: }
45: return rv;
46: }
47:
1.1.1.2 root 48: // fd が有効なら true を返す
49: bool Valid() const {
50: return (fd >= 0);
51: }
52:
1.1.1.4 ! root 53: private:
1.1 root 54: int fd;
55: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.