--- nono/lib/autofd.h 2026/04/29 17:04:48 1.1.1.1 +++ nono/lib/autofd.h 2026/04/29 17:05:08 1.1.1.4 @@ -4,6 +4,12 @@ // Licensed under nono-license.txt // +// +// autofd +// + +#pragma once + #include // 自動変数みたいな生存期間を持つディスクリプタ @@ -18,8 +24,7 @@ class autofd } ~autofd() { - if (fd >= 0) - close(fd); + Close(); } autofd& operator=(int fd_) { @@ -33,13 +38,18 @@ class autofd // 明示的にクローズする int Close() { int rv = 0; - if (fd >= 0) { + if (Valid()) { rv = close(fd); fd = -1; } return rv; } - public: + // fd が有効なら true を返す + bool Valid() const { + return (fd >= 0); + } + + private: int fd; };