File: SelfClosingGzFile.hpp

package info (click to toggle)
r-bioc-alabaster.base 1.6.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,652 kB
  • sloc: cpp: 11,377; sh: 29; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,020 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef BYTEME_SELF_CLOSING_GZFILE_HPP
#define BYTEME_SELF_CLOSING_GZFILE_HPP

#include <stdexcept>
#include <string>
#include "zlib.h"

namespace byteme {

struct SelfClosingGzFile {
    SelfClosingGzFile(const char* path, const char* mode) : handle(gzopen(path, mode)) {
        if (!handle) {
            throw std::runtime_error("failed to open file at '" + std::string(path) + "'");
        }
        return;
    }

    ~SelfClosingGzFile() {
        if (!closed) {
            gzclose(handle);
        }
        return;
    }

    SelfClosingGzFile(SelfClosingGzFile&& x) : handle(std::move(x.handle)) {
        x.closed = true;
    }

    SelfClosingGzFile& operator=(SelfClosingGzFile&& x) {
        handle = std::move(x.handle);
        x.closed = true;
        return *this;
    }

    // Delete the remaining constructors.
    SelfClosingGzFile(const SelfClosingGzFile&) = delete;
    SelfClosingGzFile& operator=(const SelfClosingGzFile&) = delete;

    bool closed = false;
    gzFile handle;
};

}

#endif