summaryrefslogtreecommitdiff
path: root/src/unique_descriptor.hh
blob: 0d925ef58685a3efbe4cefd29856717f8fe2209e (plain)
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
#ifndef fd_hh_INCLUDED
#define fd_hh_INCLUDED

#include <utility>

namespace Kakoune
{

template<auto close_fn>
struct UniqueDescriptor
{
    UniqueDescriptor(int descriptor = -1) : descriptor{descriptor} {}
    UniqueDescriptor(UniqueDescriptor&& other) : descriptor{other.descriptor} { other.descriptor = -1; }
    UniqueDescriptor& operator=(UniqueDescriptor&& other) { std::swap(descriptor, other.descriptor); other.close(); return *this; }
    ~UniqueDescriptor() { close(); }

    explicit operator int() const { return descriptor; }
    explicit operator bool() const { return descriptor != -1; }
    void close() { if (descriptor != -1) { close_fn(descriptor); descriptor = -1; } }
    int descriptor;
};

}

#endif // fd_hh_INCLUDED