From 25e3d465025a377d5ad8528d7cb23ec3bdc9f285 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 11 Jul 2019 16:43:20 +0200 Subject: [PATCH] AK: Delete bad pointer assignment operators and constructors. We shouldn't allow constructing e.g an OwnPtr from a RefPtr, and similar conversions. Instead just delete those functions so the compiler whines loudly if you try to use them. This patch also deletes constructing OwnPtr from a WeakPtr, even though that *may* be a valid thing to do, it's sufficiently weird that we can make the client jump through some hoops if he really wants it. :^) --- AK/NonnullRefPtr.h | 8 ++++++++ AK/OwnPtr.h | 20 ++++++++++++++++++++ AK/RefPtr.h | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 53ab7aeccd..a055ccc681 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -18,6 +18,9 @@ namespace AK { +template +class OwnPtr; + template inline void ref_if_not_null(T* ptr) { @@ -93,6 +96,11 @@ public: #endif } + template + NonnullRefPtr(const OwnPtr&) = delete; + template + NonnullRefPtr& operator=(const OwnPtr&) = delete; + NonnullRefPtr& operator=(const NonnullRefPtr& other) { if (m_ptr != other.m_ptr) { diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index a10cc5bdb0..ba93fd63b3 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -7,6 +7,13 @@ namespace AK { +template +class RefPtr; +template +class NonnullRefPtr; +template +class WeakPtr; + template class OwnPtr { public: @@ -36,6 +43,19 @@ public: #endif } + template + OwnPtr(const RefPtr&) = delete; + template + OwnPtr(const NonnullRefPtr&) = delete; + template + OwnPtr(const WeakPtr&) = delete; + template + OwnPtr& operator=(const RefPtr&) = delete; + template + OwnPtr& operator=(const NonnullRefPtr&) = delete; + template + OwnPtr& operator=(const WeakPtr&) = delete; + OwnPtr& operator=(OwnPtr&& other) { if (this != &other) { diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 349fb1de1c..99ebca89b9 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -6,6 +6,9 @@ namespace AK { +template +class OwnPtr; + template class RefPtr { public: @@ -86,6 +89,11 @@ public: } RefPtr(std::nullptr_t) {} + template + RefPtr(const OwnPtr&) = delete; + template + RefPtr& operator=(const OwnPtr&) = delete; + RefPtr& operator=(RefPtr&& other) { if (this != &other) {