Skip to content

Commit

Permalink
HandleDeleter → GenericDeleter and HandleDeleter
Browse files Browse the repository at this point in the history
  • Loading branch information
extratype committed May 3, 2022
1 parent cff8c8c commit d074d69
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,37 +204,41 @@ class SRWLock : public SRWLockBase<SRWLock>
void on_unlock(bool is_exclusive) {}
};

struct HandleDeleter
template <class T, class Deleter, size_t defval = 0>
struct GenericDeleter
{
void operator()(HANDLE h) noexcept
void operator()(T x) noexcept
{
CloseHandle(h);
Deleter()(x);
}
};

struct FileHandleDeleter : HandleDeleter
{
struct pointer
{
pointer(HANDLE h) : value(h) {}
pointer(T x) : value(x) {}

operator HANDLE() const { return value; }
operator T() const { return value; }

pointer(std::nullptr_t = nullptr) : value(INVALID_HANDLE_VALUE) {}
pointer(std::nullptr_t = nullptr) : value(recast<T>(defval)) {}

explicit operator bool() const { return value != INVALID_HANDLE_VALUE; }
explicit operator bool() const { return value != recast<T>(defval); }

friend bool operator==(pointer lhs, pointer rhs) { return lhs.value == rhs.value; }

HANDLE value;
T value;
};
};

// reset to nullptr
using GenericHandle = std::unique_ptr<void, HandleDeleter>;
struct HandleDeleter
{
void operator()(HANDLE h) noexcept
{
CloseHandle(h);
}
};

using GenericHandle = std::unique_ptr<void, GenericDeleter<HANDLE, HandleDeleter>>;

// reset to INVALID_HANDLE_VALUE
using FileHandle = std::unique_ptr<void, FileHandleDeleter>;
using FileHandle = std::unique_ptr<void, GenericDeleter<HANDLE, HandleDeleter, size_t(-1)>>;

struct PagesDeleter
{
Expand Down

0 comments on commit d074d69

Please sign in to comment.