unique_descriptor.hh (742B)
1 #ifndef fd_hh_INCLUDED 2 #define fd_hh_INCLUDED 3 4 #include <utility> 5 6 namespace Kakoune 7 { 8 9 template<auto close_fn> 10 struct UniqueDescriptor 11 { 12 UniqueDescriptor(int descriptor = -1) : descriptor{descriptor} {} 13 UniqueDescriptor(UniqueDescriptor&& other) : descriptor{other.descriptor} { other.descriptor = -1; } 14 UniqueDescriptor& operator=(UniqueDescriptor&& other) { std::swap(descriptor, other.descriptor); other.close(); return *this; } 15 ~UniqueDescriptor() { close(); } 16 17 explicit operator int() const { return descriptor; } 18 explicit operator bool() const { return descriptor != -1; } 19 void close() { if (descriptor != -1) { close_fn(descriptor); descriptor = -1; } } 20 int descriptor; 21 }; 22 23 } 24 25 #endif // fd_hh_INCLUDED