VTK  9.7.20261002
vtkWebGPUHandle.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
43
44#ifndef vtkWebGPUHandle_h
45#define vtkWebGPUHandle_h
46
47#include "vtkABINamespace.h" // for VTK_ABI_NAMESPACE_BEGIN
48#include "vtk_wgpu.h" // for the WebGPU C API
49
50#include <cstddef> // for std::nullptr_t
51#include <utility> // for std::swap, std::exchange
52
53namespace vtkWebGPU
54{
55VTK_ABI_NAMESPACE_BEGIN
56
63template <typename T, typename Traits>
64class Handle
65{
66public:
67 Handle() = default;
68
73 Handle(std::nullptr_t) {} // NOLINT(google-explicit-constructor)
74
79 static Handle Acquire(T raw)
80 {
81 Handle handle;
82 handle.Raw = raw;
83 return handle;
84 }
85
89 static Handle Reference(T raw)
90 {
91 if (raw != nullptr)
92 {
93 Traits::AddRef(raw);
94 }
95 return Handle::Acquire(raw);
96 }
97
98 ~Handle() { this->Reset(); }
99
100 Handle(const Handle& other)
101 : Raw(other.Raw)
102 {
103 if (this->Raw != nullptr)
104 {
105 Traits::AddRef(this->Raw);
106 }
107 }
108
109 Handle(Handle&& other) noexcept
110 : Raw(std::exchange(other.Raw, nullptr))
111 {
112 }
113
117 Handle& operator=(std::nullptr_t)
118 {
119 this->Reset();
120 return *this;
121 }
122
123 Handle& operator=(Handle other) noexcept
124 {
125 // Copy-and-swap: `other` is built by the copy or move constructor above, so
126 // this handles self-assignment and both value categories in one operator.
127 std::swap(this->Raw, other.Raw);
128 return *this;
129 }
130
134 void Reset()
135 {
136 if (T raw = std::exchange(this->Raw, nullptr))
137 {
138 Traits::Release(raw);
139 }
140 }
141
146 T Release() { return std::exchange(this->Raw, nullptr); }
147
149
153 T Get() const { return this->Raw; }
154 operator T() const& { return this->Raw; } // NOLINT(google-explicit-constructor)
155
163 operator T() const&& = delete;
165
166 explicit operator bool() const { return this->Raw != nullptr; }
167
168private:
169 T Raw = nullptr;
170};
171
172template <typename T, typename Traits>
173bool operator==(const Handle<T, Traits>& handle, std::nullptr_t)
174{
175 return handle.Get() == nullptr;
176}
177template <typename T, typename Traits>
178bool operator!=(const Handle<T, Traits>& handle, std::nullptr_t)
179{
180 return handle.Get() != nullptr;
181}
182template <typename T, typename Traits>
183bool operator==(std::nullptr_t, const Handle<T, Traits>& handle)
184{
185 return handle.Get() == nullptr;
186}
187template <typename T, typename Traits>
188bool operator!=(std::nullptr_t, const Handle<T, Traits>& handle)
189{
190 return handle.Get() != nullptr;
191}
192
201template <typename T, typename ReleaseFn>
202void ReleaseAndNull(T& raw, ReleaseFn release)
203{
204 if (raw != nullptr)
205 {
206 release(raw);
207 raw = nullptr;
208 }
209}
210
211// The `wgpu*` names redirect to dispatch table entries, whose addresses are not
212// constant expressions and so cannot be template arguments.
213#define vtkWebGPUDeclareHandle(Name) \
214 struct Name##Traits \
215 { \
216 static void AddRef(WGPU##Name handle) \
217 { \
218 wgpu##Name##AddRef(handle); \
219 } \
220 static void Release(WGPU##Name handle) \
221 { \
222 wgpu##Name##Release(handle); \
223 } \
224 }; \
225 using Name = Handle<WGPU##Name, Name##Traits>
226
229vtkWebGPUDeclareHandle(BindGroupLayout);
233vtkWebGPUDeclareHandle(ComputePassEncoder);
234vtkWebGPUDeclareHandle(ComputePipeline);
241vtkWebGPUDeclareHandle(RenderBundleEncoder);
242vtkWebGPUDeclareHandle(RenderPassEncoder);
249
250#undef vtkWebGPUDeclareHandle
251
252VTK_ABI_NAMESPACE_END
253} // namespace vtkWebGPU
254
255#endif
256// VTK-HeaderTest-Exclude: vtkWebGPUHandle.h
Reference-counted owner of a WebGPU C API handle.
void Reset()
Release the referenced object, if any, and become null.
Handle(const Handle &other)
Handle(Handle &&other) noexcept
T Release()
Relinquish ownership to the caller, who becomes responsible for releasing the returned handle.
static Handle Acquire(T raw)
Adopt raw without adding a reference.
T Get() const
Access the underlying raw handle without transferring ownership.
Handle & operator=(std::nullptr_t)
Release whatever is held and become null.
static Handle Reference(T raw)
Take a new reference to raw, which stays owned by the caller.
Handle & operator=(Handle other) noexcept
Handle(std::nullptr_t)
A null handle.
void ReleaseAndNull(T &raw, ReleaseFn release)
Release raw if it is non-null and set it to null.
bool operator!=(const Handle< T, Traits > &handle, std::nullptr_t)
bool operator==(const Handle< T, Traits > &handle, std::nullptr_t)
#define vtkWebGPUDeclareHandle(Name)