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

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

unix.superglobalmegacorp.com

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