#pragma once #include class UniqueHandle { public: explicit UniqueHandle(HANDLE handle = NULL) : mHandle(handle) {} ~UniqueHandle() { reset(); } UniqueHandle(const UniqueHandle&) = delete; UniqueHandle& operator=(const UniqueHandle&) = delete; UniqueHandle(UniqueHandle&& other) noexcept : mHandle(other.release()) {} UniqueHandle& operator=(UniqueHandle&& other) noexcept { if (this != &other) reset(other.release()); return *this; } HANDLE get() const { return mHandle; } bool valid() const { return mHandle != NULL && mHandle != INVALID_HANDLE_VALUE; } HANDLE release() { HANDLE handle = mHandle; mHandle = NULL; return handle; } void reset(HANDLE handle = NULL) { if (valid()) CloseHandle(mHandle); mHandle = handle; } private: HANDLE mHandle; };