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.
This commit is contained in:
MacDue 2022-11-19 01:03:48 +00:00 committed by Linus Groh
parent 24caacfe28
commit 3483407ddc
Notes: sideshowbarker 2024-07-17 04:21:06 +09:00
5 changed files with 23 additions and 85 deletions

View File

@ -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;

View File

@ -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<typename RawPtr>
bool operator==(RawPtr other) const requires(IsPointer<RawPtr>) { return m_ptr == other; }
// clang-format off
private:

View File

@ -131,35 +131,21 @@ public:
return NonnullOwnPtr<U>(NonnullOwnPtr<U>::Adopt, static_cast<U&>(*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; }

View File

@ -231,31 +231,19 @@ public:
return NonnullRefPtr<T>(NonnullRefPtr<T>::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<typename U>
bool operator==(NonnullRefPtr<U> const& other) const { return as_ptr() == other.m_ptr; }
template<typename U>
bool operator==(NonnullRefPtr<U>& 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<typename RawPtr>
bool operator==(RawPtr other) const requires(IsPointer<RawPtr>) { return as_ptr() == other; }
ALWAYS_INLINE bool is_null() const { return !m_ptr; }

View File

@ -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
{