Annotation of nono/lib/autofd.h, revision 1.1.1.3

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.