libcppwrap
A collection of C++ wrappers for native APIs
Loading...
Searching...
No Matches
handle.hpp
Go to the documentation of this file.
1//
2// libcppwrap - A collection of C++ wrappers for native APIs
3// Copyright (C) 2021-2023 David A. Norris <danorris@gmail.com>
4// Published under the MIT license - https://opensource.org/licenses/MIT
5//
6
7#pragma once
8
9#include <type_traits>
10#include <utility>
11
12namespace w
13{
28 template <
29 typename Resource,
30 Resource Closed,
31 auto CloseFunc,
33 std::is_nothrow_copy_constructible_v<Resource> &&
34 std::is_nothrow_swappable_v<Resource> &&
35 std::is_function_v<std::remove_pointer_t<decltype(CloseFunc)>> && (
36 std::is_invocable_v<decltype(CloseFunc), Resource> ||
37 std::is_invocable_v<decltype(CloseFunc)>)> * = nullptr>
38
39 class handle
40 {
41 public:
42
46 handle() noexcept : handle(Closed) { }
47
53 handle(Resource resource) noexcept : _resource(resource) { }
54
55 handle(const handle&) = delete;
56 handle& operator=(const handle&) = delete;
57
64 handle(handle&& other) noexcept : _resource(Closed)
65 {
66 std::swap(_resource, other._resource);
67 }
68
76 handle& operator=(handle&& other) noexcept
77 {
78 if (&other != this)
79 {
80 close();
81 std::swap(_resource, other._resource);
82 }
83
84 return *this;
85 }
86
90 ~handle() noexcept { close(); }
91
98 Resource get() noexcept { return _resource; }
99
106 const Resource get() const noexcept { return _resource; }
107
114 operator Resource() noexcept { return _resource; }
115
122 operator const Resource() const noexcept { return _resource; }
123
129 operator bool() const noexcept { return Closed != _resource; }
130
136 void close() noexcept
137 {
138 Resource resource(Closed);
139 std::swap(resource, _resource);
140 if (Closed != resource)
141 {
142 if constexpr (std::is_invocable_v<decltype(CloseFunc), Resource>)
143 CloseFunc(resource);
144 else
145 CloseFunc();
146 }
147 }
148
154 Resource release() noexcept
155 {
156 Resource resource = Closed;
157 std::swap(resource, _resource);
158 return resource;
159 }
160
161 private:
162
163 Resource _resource;
164 };
165}
An RAII handle type which owns an opaque resource of a specified type, and calls a designated functio...
Definition: handle.hpp:40
Resource release() noexcept
Releases (without closing) and returns the underlying resource.
Definition: handle.hpp:154
handle() noexcept
Constructs a handle which does not own a resource.
Definition: handle.hpp:46
handle & operator=(const handle &)=delete
handle(const handle &)=delete
handle(handle &&other) noexcept
Constructs a handle which takes ownership of a resource (if any) from another handle.
Definition: handle.hpp:64
const Resource get() const noexcept
Returns the resource (if any) owned by this handle.
Definition: handle.hpp:106
void close() noexcept
Closes the resource (if any) owned by this handle by calling CloseFunc.
Definition: handle.hpp:136
~handle() noexcept
Closes the resource (if any) owned by this handle.
Definition: handle.hpp:90
handle(Resource resource) noexcept
Constructs a handle which owns the specified resource.
Definition: handle.hpp:53
handle & operator=(handle &&other) noexcept
Releases the resource (if any) owned by this handle and takes ownership of a resource (if any) from a...
Definition: handle.hpp:76
Resource get() noexcept
Returns the resource (if any) owned by this handle.
Definition: handle.hpp:98
T is_function_v
T is_invocable_v
Definition: assert.hpp:13
T swap(T... args)