From 0e439fb2d229808a8d879bfefcf371099b522479 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 14 Oct 2020 01:57:40 +0300 Subject: add Handle class It's derived from various projects of mine. --- include/winapi/handle.hpp | 73 ++++++++++++++++++++++++++++++++++++++++++ include/winapi/workarounds.hpp | 8 +++++ 2 files changed, 81 insertions(+) create mode 100644 include/winapi/handle.hpp create mode 100644 include/winapi/workarounds.hpp (limited to 'include') diff --git a/include/winapi/handle.hpp b/include/winapi/handle.hpp new file mode 100644 index 0000000..9e9661b --- /dev/null +++ b/include/winapi/handle.hpp @@ -0,0 +1,73 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#include "workarounds.hpp" + +#include + +#include + +#include +#include +#include + +namespace winapi { + +class Handle { +public: + Handle() = default; + + explicit Handle(HANDLE raw) + : impl{raw} + { } + + Handle(Handle&& other) BOOST_NOEXCEPT_OR_NOTHROW { + swap(other); + } + + Handle& operator=(Handle other) BOOST_NOEXCEPT_OR_NOTHROW { + swap(other); + return *this; + } + + void swap(Handle& other) BOOST_NOEXCEPT_OR_NOTHROW { + using std::swap; + swap(impl, other.impl); + } + + explicit 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); + WINAPI_UNUSED_PARAMETER(ret); + } + }; + + std::unique_ptr impl; + + Handle(const Handle&) = delete; +}; + +inline void swap(Handle& a, Handle& b) BOOST_NOEXCEPT_OR_NOTHROW { + a.swap(b); +} + +} + +namespace std { + +template <> +inline void swap(winapi::Handle& a, winapi::Handle& b) BOOST_NOEXCEPT_OR_NOTHROW { + a.swap(b); +} + +} // namespace std diff --git a/include/winapi/workarounds.hpp b/include/winapi/workarounds.hpp new file mode 100644 index 0000000..a5a3607 --- /dev/null +++ b/include/winapi/workarounds.hpp @@ -0,0 +1,8 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#define WINAPI_UNUSED_PARAMETER(...) (void)(__VA_ARGS__) -- cgit v1.2.3