aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/winapi
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-10-14 01:57:40 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-10-14 01:57:40 +0300
commit0e439fb2d229808a8d879bfefcf371099b522479 (patch)
treef2fc08d91f11cfae1dbd618be317b8d041f48903 /include/winapi
parentinitial commit (diff)
downloadwinapi-common-0e439fb2d229808a8d879bfefcf371099b522479.tar.gz
winapi-common-0e439fb2d229808a8d879bfefcf371099b522479.zip
add Handle class
It's derived from various projects of mine.
Diffstat (limited to '')
-rw-r--r--include/winapi/handle.hpp73
-rw-r--r--include/winapi/workarounds.hpp8
2 files changed, 81 insertions, 0 deletions
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 <Egor.Tensin@gmail.com>
+// 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 <boost/config.hpp>
+
+#include <windows.h>
+
+#include <cassert>
+#include <memory>
+#include <utility>
+
+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<void, Close> 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 <Egor.Tensin@gmail.com>
+// 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__)