Some RAII fixes
This commit is contained in:
parent
a58799aca7
commit
0b35e89ab9
1 changed files with 11 additions and 8 deletions
|
@ -27,25 +27,25 @@ public:
|
||||||
VulkanRaii() : handle{}, deleter{}, dispatch{} {}
|
VulkanRaii() : handle{}, deleter{}, dispatch{} {}
|
||||||
|
|
||||||
// Constructor with handle and deleter
|
// Constructor with handle and deleter
|
||||||
VulkanRaii(T handle_, DeleterFunc deleter_, const Dispatch& dispatch_, const std::string& resource_name = "Vulkan resource")
|
VulkanRaii(T handle_, DeleterFunc deleter_, const Dispatch& dispatch_, const char* resource_name = "Vulkan resource")
|
||||||
: handle{handle_}, deleter{std::move(deleter_)}, dispatch{dispatch_} {
|
: handle{handle_}, deleter{std::move(deleter_)}, dispatch{dispatch_} {
|
||||||
LOG_WARNING(Render_Vulkan, "RAII wrapper created for {}", resource_name);
|
LOG_WARNING(Render_Vulkan, "RAII wrapper created for {}", resource_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move constructor
|
// Move constructor
|
||||||
VulkanRaii(VulkanRaii&& other) noexcept
|
VulkanRaii(VulkanRaii&& other) noexcept
|
||||||
: handle{other.handle}, deleter{std::move(other.deleter)}, dispatch{other.dispatch} {
|
: handle{std::exchange(other.handle, VK_NULL_HANDLE)},
|
||||||
other.handle = VK_NULL_HANDLE;
|
deleter{std::move(other.deleter)},
|
||||||
|
dispatch{other.dispatch} {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move assignment
|
// Move assignment
|
||||||
VulkanRaii& operator=(VulkanRaii&& other) noexcept {
|
VulkanRaii& operator=(VulkanRaii&& other) noexcept {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
cleanup();
|
cleanup();
|
||||||
handle = other.handle;
|
handle = std::exchange(other.handle, VK_NULL_HANDLE);
|
||||||
deleter = std::move(other.deleter);
|
deleter = std::move(other.deleter);
|
||||||
dispatch = other.dispatch;
|
dispatch = other.dispatch;
|
||||||
other.handle = VK_NULL_HANDLE;
|
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -97,9 +97,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void cleanup() {
|
// Optimized cleanup function that avoids unnecessary checks in release builds
|
||||||
if (handle != VK_NULL_HANDLE && deleter) {
|
void cleanup() noexcept {
|
||||||
deleter(handle, dispatch);
|
if (handle != VK_NULL_HANDLE) {
|
||||||
|
if (deleter) {
|
||||||
|
deleter(handle, dispatch);
|
||||||
|
}
|
||||||
handle = VK_NULL_HANDLE;
|
handle = VK_NULL_HANDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue