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

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.2 ! root       23:                if (Valid())
1.1       root       24:                        close(fd);
                     25:        }
                     26: 
                     27:        autofd& operator=(int fd_) {
                     28:                fd = fd_;
                     29:                return *this;
                     30:        }
                     31:        operator int() const {
                     32:                return fd;
                     33:        }
                     34: 
                     35:        // 明示的にクローズする
                     36:        int Close() {
                     37:                int rv = 0;
                     38:                if (fd >= 0) {
                     39:                        rv = close(fd);
                     40:                        fd = -1;
                     41:                }
                     42:                return rv;
                     43:        }
                     44: 
1.1.1.2 ! root       45:        // fd が有効なら true を返す
        !            46:        bool Valid() const {
        !            47:                return (fd >= 0);
        !            48:        }
        !            49: 
1.1       root       50:  public:
                     51:        int fd;
                     52: };

unix.superglobalmegacorp.com

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