diff options
| author | Stefan Boberg <[email protected]> | 2021-05-24 16:07:51 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-05-24 16:07:51 +0200 |
| commit | a9c3aa543891fe05ab2debcb2df715645f19c333 (patch) | |
| tree | eeef2cb3029d69b736cdd885cda0b4366cd99787 | |
| parent | Added UniqueBuffer::Reset() (diff) | |
| download | zen-a9c3aa543891fe05ab2debcb2df715645f19c333.tar.xz zen-a9c3aa543891fe05ab2debcb2df715645f19c333.zip | |
RefPtr/Ref cleanup
Fixed self-assignment bug in move assignment operator
| -rw-r--r-- | zencore/include/zencore/refcount.h | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/zencore/include/zencore/refcount.h b/zencore/include/zencore/refcount.h index e6cdbc540..288b649c6 100644 --- a/zencore/include/zencore/refcount.h +++ b/zencore/include/zencore/refcount.h @@ -74,16 +74,22 @@ public: } inline RefPtr& operator=(const RefPtr& Rhs) { - m_Ref && m_Ref->Release(); - auto Ref = m_Ref = Rhs.m_Ref; - Ref && Ref->AddRef(); + if (&Rhs != this) + { + Rhs && Rhs->AddRef(); + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + } return *this; } inline RefPtr& operator=(RefPtr&& Rhs) noexcept { - m_Ref && m_Ref->Release(); - m_Ref = Rhs.m_Ref; - Rhs.m_Ref = nullptr; + if (&Rhs != this) + { + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + Rhs.m_Ref = nullptr; + } return *this; } inline RefPtr(RefPtr&& Rhs) noexcept : m_Ref(Rhs.m_Ref) { Rhs.m_Ref = nullptr; } @@ -108,8 +114,9 @@ public: inline Ref(T* Ptr) : m_Ref(Ptr) { m_Ref && m_Ref->AddRef(); } inline ~Ref() { m_Ref && m_Ref->Release(); } - inline explicit operator bool() const { return m_Ref != nullptr; } - inline T* operator->() const { return m_Ref; } + [[nodiscard]] inline bool IsNull() const { return m_Ref == nullptr; } + inline explicit operator bool() const { return m_Ref != nullptr; } + inline T* operator->() const { return m_Ref; } inline std::strong_ordering operator<=>(const Ref& Rhs) const = default; @@ -122,16 +129,22 @@ public: } inline Ref& operator=(const Ref& Rhs) { - m_Ref && m_Ref->Release(); - auto Ref = m_Ref = Rhs.m_Ref; - Ref && Ref->AddRef(); + if (&Rhs != this) + { + Rhs && Rhs->AddRef(); + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + } return *this; } inline Ref& operator=(Ref&& Rhs) noexcept { - m_Ref && m_Ref->Release(); - m_Ref = Rhs.m_Ref; - Rhs.m_Ref = nullptr; + if (&Rhs != this) + { + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + Rhs.m_Ref = nullptr; + } return *this; } inline Ref(Ref&& Rhs) noexcept : m_Ref(Rhs.m_Ref) { Rhs.m_Ref = nullptr; } |