From bf97b9589d8008833091a04a1adbf80cb92275d4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 24 Jun 2019 09:58:21 +0200 Subject: [PATCH] NonnullRefPtr: Some improvements. - Delete the default constructor instead of just making it private. It's never valid to create an empty NonnullRefPtr. - Add copy assignment operators. I originally omitted these to force use of .copy_ref() at call sites, but the hassle/gain ratio is minuscule. - Allow calling all the assignment operators in all consumable states. This codifies that it's okay to overwrite a moved-from NonnullRefPtr. --- AK/NonnullRefPtr.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index ca8f22b1ef..c3c3becff9 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -95,7 +95,27 @@ public: #endif } - CALLABLE_WHEN(unconsumed) + NonnullRefPtr& operator=(const NonnullRefPtr& other) + { + if (m_ptr != other.m_ptr) { + deref_if_not_null(m_ptr); + m_ptr = const_cast(other.ptr()); + m_ptr->ref(); + } + return *this; + } + + template + NonnullRefPtr& operator=(const NonnullRefPtr& other) + { + if (m_ptr != other.m_ptr) { + deref_if_not_null(m_ptr); + m_ptr = const_cast(static_cast(other.ptr())); + m_ptr->ref(); + } + return *this; + } + NonnullRefPtr& operator=(NonnullRefPtr&& other) { if (this != &other) { @@ -106,7 +126,6 @@ public: } template - CALLABLE_WHEN(unconsumed) NonnullRefPtr& operator=(NonnullRefPtr&& other) { if (this != static_cast(&other)) { @@ -116,7 +135,6 @@ public: return *this; } - CALLABLE_WHEN(unconsumed) NonnullRefPtr& operator=(T& object) { if (m_ptr != &object) @@ -208,7 +226,7 @@ public: } private: - NonnullRefPtr() {} + NonnullRefPtr() = delete; T* m_ptr { nullptr }; };