VTK  9.7.20260911
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
64template <typename T, void (*AddRefFn)(T), void (*ReleaseFn)(T)>
65class Handle
66{
67public:
68 Handle() = default;
69
74 Handle(std::nullptr_t) {} // NOLINT(google-explicit-constructor)
75
80 static Handle Acquire(T raw)
81 {
82 Handle handle;
83 handle.Raw = raw;
84 return handle;
85 }
86
90 static Handle Reference(T raw)
91 {
92 if (raw != nullptr)
93 {
94 AddRefFn(raw);
95 }
96 return Handle::Acquire(raw);
97 }
98
99 ~Handle() { this->Reset(); }
100
101 Handle(const Handle& other)
102 : Raw(other.Raw)
103 {
104 if (this->Raw != nullptr)
105 {
106 AddRefFn(this->Raw);
107 }
108 }
109
110 Handle(Handle&& other) noexcept
111 : Raw(std::exchange(other.Raw, nullptr))
112 {
113 }
114
118 Handle& operator=(std::nullptr_t)
119 {
120 this->Reset();
121 return *this;
122 }
123
124 Handle& operator=(Handle other) noexcept
125 {
126 // Copy-and-swap: `other` is built by the copy or move constructor above, so
127 // this handles self-assignment and both value categories in one operator.
128 std::swap(this->Raw, other.Raw);
129 return *this;
130 }
131
135 void Reset()
136 {
137 if (T raw = std::exchange(this->Raw, nullptr))
138 {
139 ReleaseFn(raw);
140 }
141 }
142
147 T Release() { return std::exchange(this->Raw, nullptr); }
148
150
154 T Get() const { return this->Raw; }
155 operator T() const& { return this->Raw; } // NOLINT(google-explicit-constructor)
156
164 operator T() const&& = delete;
166
167 explicit operator bool() const { return this->Raw != nullptr; }
168
169private:
170 T Raw = nullptr;
171};
172
173template <typename T, void (*A)(T), void (*R)(T)>
174bool operator==(const Handle<T, A, R>& handle, std::nullptr_t)
175{
176 return handle.Get() == nullptr;
177}
178template <typename T, void (*A)(T), void (*R)(T)>
179bool operator!=(const Handle<T, A, R>& handle, std::nullptr_t)
180{
181 return handle.Get() != nullptr;
182}
183template <typename T, void (*A)(T), void (*R)(T)>
184bool operator==(std::nullptr_t, const Handle<T, A, R>& handle)
185{
186 return handle.Get() == nullptr;
187}
188template <typename T, void (*A)(T), void (*R)(T)>
189bool operator!=(std::nullptr_t, const Handle<T, A, R>& handle)
190{
191 return handle.Get() != nullptr;
192}
193
202template <typename T, typename ReleaseFn>
203void ReleaseAndNull(T& raw, ReleaseFn release)
204{
205 if (raw != nullptr)
206 {
207 release(raw);
208 raw = nullptr;
209 }
210}
211
212#define vtkWebGPUDeclareHandle(Name) \
213 using Name = Handle<WGPU##Name, &wgpu##Name##AddRef, &wgpu##Name##Release>
214
217vtkWebGPUDeclareHandle(BindGroupLayout);
221vtkWebGPUDeclareHandle(ComputePassEncoder);
222vtkWebGPUDeclareHandle(ComputePipeline);
229vtkWebGPUDeclareHandle(RenderBundleEncoder);
230vtkWebGPUDeclareHandle(RenderPassEncoder);
237
238#undef vtkWebGPUDeclareHandle
239
240VTK_ABI_NAMESPACE_END
241} // namespace vtkWebGPU
242
243#endif
244// VTK-HeaderTest-Exclude: vtkWebGPUHandle.h
Reference-counted owner of a WebGPU C API handle.
Handle(std::nullptr_t)
A null handle.
void Reset()
Release the referenced object, if any, and become null.
T Get() const
Access the underlying raw handle without transferring ownership.
Handle(Handle &&other) noexcept
T Release()
Relinquish ownership to the caller, who becomes responsible for releasing the returned handle.
Handle & operator=(std::nullptr_t)
Release whatever is held and become null.
Handle & operator=(Handle other) noexcept
Handle(const Handle &other)
static Handle Acquire(T raw)
Adopt raw without adding a reference.
static Handle Reference(T raw)
Take a new reference to raw, which stays owned by the caller.
bool operator==(const Handle< T, A, R > &handle, std::nullptr_t)
void ReleaseAndNull(T &raw, ReleaseFn release)
Release raw if it is non-null and set it to null.
bool operator!=(const Handle< T, A, R > &handle, std::nullptr_t)
#define vtkWebGPUDeclareHandle(Name)