From 3483407ddc11ed064915ab12b7b7b6439d6b0e28 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sat, 19 Nov 2022 01:03:48 +0000 Subject: [PATCH] AK: Return non-const types from Ptr class operators Even if the pointer value is const, the value they point to is not necessarily const, so these functions should not add the qualifier. This also removes the redundant non-const implementations of these operators. --- AK/NonnullOwnPtr.h | 17 ++++------------- AK/NonnullRefPtr.h | 33 +++++++-------------------------- AK/OwnPtr.h | 22 ++++------------------ AK/RefPtr.h | 30 ++++++------------------------ AK/WeakPtr.h | 6 ++---- 5 files changed, 23 insertions(+), 85 deletions(-) diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index b10ac3ddc7c..60d46401cef 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -93,26 +93,17 @@ public: return exchange(m_ptr, nullptr); } - ALWAYS_INLINE RETURNS_NONNULL T* ptr() + ALWAYS_INLINE RETURNS_NONNULL T* ptr() const { VERIFY(m_ptr); return m_ptr; } - ALWAYS_INLINE RETURNS_NONNULL const T* ptr() const - { - VERIFY(m_ptr); - return m_ptr; - } + ALWAYS_INLINE RETURNS_NONNULL T* operator->() const { return ptr(); } - ALWAYS_INLINE RETURNS_NONNULL T* operator->() { return ptr(); } - ALWAYS_INLINE RETURNS_NONNULL const T* operator->() const { return ptr(); } + ALWAYS_INLINE T& operator*() const { return *ptr(); } - ALWAYS_INLINE T& operator*() { return *ptr(); } - ALWAYS_INLINE const T& operator*() const { return *ptr(); } - - ALWAYS_INLINE RETURNS_NONNULL operator const T*() const { return ptr(); } - ALWAYS_INLINE RETURNS_NONNULL operator T*() { return ptr(); } + ALWAYS_INLINE RETURNS_NONNULL operator T*() const { return ptr(); } operator bool() const = delete; bool operator!() const = delete; diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 22ac6eb701a..d829ef5c0f5 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -156,47 +156,27 @@ public: return *ptr; } - ALWAYS_INLINE RETURNS_NONNULL T* ptr() - { - return as_nonnull_ptr(); - } - ALWAYS_INLINE RETURNS_NONNULL T const* ptr() const + ALWAYS_INLINE RETURNS_NONNULL T* ptr() const { return as_nonnull_ptr(); } - ALWAYS_INLINE RETURNS_NONNULL T* operator->() - { - return as_nonnull_ptr(); - } - ALWAYS_INLINE RETURNS_NONNULL T const* operator->() const + ALWAYS_INLINE RETURNS_NONNULL T* operator->() const { return as_nonnull_ptr(); } - ALWAYS_INLINE T& operator*() - { - return *as_nonnull_ptr(); - } - ALWAYS_INLINE T const& operator*() const + ALWAYS_INLINE T& operator*() const { return *as_nonnull_ptr(); } - ALWAYS_INLINE RETURNS_NONNULL operator T*() - { - return as_nonnull_ptr(); - } - ALWAYS_INLINE RETURNS_NONNULL operator T const*() const + ALWAYS_INLINE RETURNS_NONNULL operator T*() const { return as_nonnull_ptr(); } - ALWAYS_INLINE operator T&() - { - return *as_nonnull_ptr(); - } - ALWAYS_INLINE operator T const&() const + ALWAYS_INLINE operator T&() const { return *as_nonnull_ptr(); } @@ -217,7 +197,8 @@ public: bool operator==(NonnullRefPtr const& other) const { return m_ptr == other.m_ptr; } - bool operator==(NonnullRefPtr& other) { return m_ptr == other.m_ptr; } + template + bool operator==(RawPtr other) const requires(IsPointer) { return m_ptr == other; } // clang-format off private: diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index 4f6b1fbc64a..ec486ccc406 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -131,35 +131,21 @@ public: return NonnullOwnPtr(NonnullOwnPtr::Adopt, static_cast(*leak_ptr())); } - T* ptr() { return m_ptr; } - const T* ptr() const { return m_ptr; } + T* ptr() const { return m_ptr; } - T* operator->() + T* operator->() const { VERIFY(m_ptr); return m_ptr; } - const T* operator->() const - { - VERIFY(m_ptr); - return m_ptr; - } - - T& operator*() + T& operator*() const { VERIFY(m_ptr); return *m_ptr; } - const T& operator*() const - { - VERIFY(m_ptr); - return *m_ptr; - } - - operator const T*() const { return m_ptr; } - operator T*() { return m_ptr; } + operator T*() const { return m_ptr; } operator bool() { return !!m_ptr; } diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 88b8ec2c697..8ca2346c4b4 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -231,31 +231,19 @@ public: return NonnullRefPtr(NonnullRefPtr::Adopt, *ptr); } - ALWAYS_INLINE T* ptr() { return as_ptr(); } - ALWAYS_INLINE T const* ptr() const { return as_ptr(); } + ALWAYS_INLINE T* ptr() const { return as_ptr(); } - ALWAYS_INLINE T* operator->() + ALWAYS_INLINE T* operator->() const { return as_nonnull_ptr(); } - ALWAYS_INLINE T const* operator->() const - { - return as_nonnull_ptr(); - } - - ALWAYS_INLINE T& operator*() + ALWAYS_INLINE T& operator*() const { return *as_nonnull_ptr(); } - ALWAYS_INLINE T const& operator*() const - { - return *as_nonnull_ptr(); - } - - ALWAYS_INLINE operator T const*() const { return as_ptr(); } - ALWAYS_INLINE operator T*() { return as_ptr(); } + ALWAYS_INLINE operator T*() const { return as_ptr(); } ALWAYS_INLINE operator bool() { return !is_null(); } @@ -263,17 +251,11 @@ public: bool operator==(RefPtr const& other) const { return as_ptr() == other.as_ptr(); } - bool operator==(RefPtr& other) { return as_ptr() == other.as_ptr(); } - template bool operator==(NonnullRefPtr const& other) const { return as_ptr() == other.m_ptr; } - template - bool operator==(NonnullRefPtr& other) { return as_ptr() == other.m_ptr; } - - bool operator==(T const* other) const { return as_ptr() == other; } - - bool operator==(T* other) { return as_ptr() == other; } + template + bool operator==(RawPtr other) const requires(IsPointer) { return as_ptr() == other; } ALWAYS_INLINE bool is_null() const { return !m_ptr; } diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h index 97fc920964d..caf881ce9e2 100644 --- a/AK/WeakPtr.h +++ b/AK/WeakPtr.h @@ -117,10 +117,8 @@ public: } T* ptr() const { return unsafe_ptr(); } - T* operator->() { return unsafe_ptr(); } - const T* operator->() const { return unsafe_ptr(); } - operator const T*() const { return unsafe_ptr(); } - operator T*() { return unsafe_ptr(); } + T* operator->() const { return unsafe_ptr(); } + operator T*() const { return unsafe_ptr(); } [[nodiscard]] T* unsafe_ptr() const {