mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-03 21:41:26 +12:00
shared_ptr.hpp: Rewrite shared_ptr to single_ptr conversion
Logic felt non-intuitive and this method should be very explicit.
This commit is contained in:
parent
0cc655074d
commit
cfeb022340
1 changed files with 4 additions and 9 deletions
|
@ -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 <typename U> requires PtrSame<T, U>
|
template <typename U> requires PtrSame<T, U>
|
||||||
explicit operator single_ptr<U>() && noexcept
|
single_ptr<U> try_convert_to_single_ptr() noexcept
|
||||||
{
|
{
|
||||||
const auto o = d();
|
if (const auto o = m_ptr ? d() : nullptr; o && o->refs == 1u)
|
||||||
|
|
||||||
if (m_ptr && !--o->refs)
|
|
||||||
{
|
{
|
||||||
// Convert last reference to single_ptr instance.
|
// Convert last reference to single_ptr instance.
|
||||||
o->refs.release(1);
|
single_ptr<U> r;
|
||||||
single_ptr<T> r;
|
|
||||||
r.m_ptr = static_cast<decltype(r.m_ptr)>(std::exchange(m_ptr, nullptr));
|
r.m_ptr = static_cast<decltype(r.m_ptr)>(std::exchange(m_ptr, nullptr));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, both pointers are gone. Didn't seem right to do it in the constructor.
|
|
||||||
m_ptr = nullptr;
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue