From cfeb0223400a0d6ec96b0f913762575dd365c8d9 Mon Sep 17 00:00:00 2001 From: Elad <18193363+elad335@users.noreply.github.com> Date: Wed, 25 Dec 2024 08:42:45 +0200 Subject: [PATCH] shared_ptr.hpp: Rewrite shared_ptr to single_ptr conversion Logic felt non-intuitive and this method should be very explicit. --- rpcs3/util/shared_ptr.hpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rpcs3/util/shared_ptr.hpp b/rpcs3/util/shared_ptr.hpp index 7685cfda91..06f1d2a380 100644 --- a/rpcs3/util/shared_ptr.hpp +++ b/rpcs3/util/shared_ptr.hpp @@ -451,23 +451,18 @@ namespace stx } } - // Converts to unique (single) ptr if reference is 1, otherwise returns null. Nullifies self. + // Converts to unique (single) ptr if reference is 1. Nullifies self on success. template requires PtrSame - explicit operator single_ptr() && noexcept + single_ptr try_convert_to_single_ptr() noexcept { - const auto o = d(); - - if (m_ptr && !--o->refs) + if (const auto o = m_ptr ? d() : nullptr; o && o->refs == 1u) { // Convert last reference to single_ptr instance. - o->refs.release(1); - single_ptr r; + single_ptr r; r.m_ptr = static_cast(std::exchange(m_ptr, nullptr)); return r; } - // Otherwise, both pointers are gone. Didn't seem right to do it in the constructor. - m_ptr = nullptr; return {}; }