diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-09-15 19:32:24 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-09-15 19:32:24 +0300 |
commit | 94ae6a34b0cb90ee387c6e9a86ccd1998f8864d6 (patch) | |
tree | 67e27c98fb4399f65aeee74b116dfed1565a6e8d /handle.h | |
download | privilege-check-94ae6a34b0cb90ee387c6e9a86ccd1998f8864d6.tar.gz privilege-check-94ae6a34b0cb90ee387c6e9a86ccd1998f8864d6.zip |
initial commit
Diffstat (limited to '')
-rw-r--r-- | handle.h | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/handle.h b/handle.h new file mode 100644 index 0000000..5f83f24 --- /dev/null +++ b/handle.h @@ -0,0 +1,64 @@ +#pragma once + +#include <Windows.h> + +#include <cassert> + +#include <memory> +#include <utility> + +class Handle +{ +public: + Handle() = default; + + explicit Handle(HANDLE raw) + : impl{raw} + { } + + Handle(Handle&& other) noexcept + { + swap(other); + } + + Handle& operator=(Handle other) noexcept + { + swap(other); + return *this; + } + + void swap(Handle& other) noexcept + { + using std::swap; + swap(impl, other.impl); + } + + operator HANDLE() const + { + return impl.get(); + } + +private: + struct Close + { + void operator()(HANDLE raw) const + { + if (raw == NULL || raw == INVALID_HANDLE_VALUE) + return; + const auto ret = CloseHandle(raw); + assert(ret); + } + }; + + std::unique_ptr<void, Close> impl; + + Handle(const Handle&) = delete; +}; + +namespace std +{ + void swap(Handle& a, Handle& b) noexcept + { + a.swap(b); + } +} |