From 560d037c41d97b4956b6e5551c758c01bbe71da2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 11 Jul 2019 15:36:01 +0200 Subject: [PATCH] AK: Make it more more pleasant to copy RefPtr's. I had a silly ambition that we would avoid unnecessary ref count churn by forcing explicit use of "copy_ref()" wherever a copy was actually needed. This was making RefPtr a bit clunky to work with, for no real benefit. This patch adds the missing copy construction/assignment stuff to RefPtr. --- AK/RefPtr.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 866cea0964..bcf04ec328 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -42,6 +42,17 @@ public: : m_ptr(other.leak_ref()) { } + RefPtr(const NonnullRefPtr& other) + : m_ptr(const_cast(other.ptr())) + { + m_ptr->ref(); + } + template + RefPtr(const NonnullRefPtr& other) + : m_ptr(static_cast(const_cast(other.ptr()))) + { + m_ptr->ref(); + } template RefPtr(NonnullRefPtr&& other) : m_ptr(static_cast(&other.leak_ref())) @@ -100,6 +111,16 @@ public: return *this; } + RefPtr& operator=(const NonnullRefPtr& other) + { + if (m_ptr != other.ptr()) + deref_if_not_null(m_ptr); + m_ptr = const_cast(other.ptr()); + ASSERT(m_ptr); + ref_if_not_null(m_ptr); + return *this; + } + template RefPtr& operator=(const NonnullRefPtr& other) { @@ -111,6 +132,15 @@ public: return *this; } + RefPtr& operator=(const RefPtr& other) + { + if (m_ptr != other.ptr()) + deref_if_not_null(m_ptr); + m_ptr = const_cast(other.ptr()); + ref_if_not_null(m_ptr); + return *this; + } + template RefPtr& operator=(const RefPtr& other) {