|
|
1.1.1.5 ! root 1: /* ! 2: * Copyright (C) 2020 Tetsuya Isaki ! 3: * ! 4: * Permission to use, copy, modify, and/or distribute this software for any ! 5: * purpose with or without fee is hereby granted. ! 6: * ! 7: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES ! 8: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF ! 9: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ! 10: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ! 11: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ! 12: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF ! 13: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ! 14: */ 1.1 root 15: 1.1.1.4 root 16: // 17: // autofd 18: // 19: 1.1.1.2 root 20: #pragma once 21: 1.1 root 22: #include <unistd.h> 23: 24: // 自動変数みたいな生存期間を持つディスクリプタ 25: class autofd 26: { 27: public: 28: autofd() { 29: fd = -1; 30: } 31: autofd(int fd_) { 32: fd = fd_; 33: } 34: 35: ~autofd() { 1.1.1.3 root 36: Close(); 1.1 root 37: } 38: 39: autofd& operator=(int fd_) { 40: fd = fd_; 41: return *this; 42: } 43: operator int() const { 44: return fd; 45: } 46: 47: // 明示的にクローズする 48: int Close() { 49: int rv = 0; 1.1.1.3 root 50: if (Valid()) { 1.1 root 51: rv = close(fd); 52: fd = -1; 53: } 54: return rv; 55: } 56: 1.1.1.2 root 57: // fd が有効なら true を返す 58: bool Valid() const { 59: return (fd >= 0); 60: } 61: 1.1.1.4 root 62: private: 1.1 root 63: int fd; 64: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.