/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #ifdef KERNEL # include #else # include # include # include # include # include # include # include namespace AK { template class OwnPtr; template class RefPtr { template friend class RefPtr; template friend class WeakPtr; public: enum AdoptTag { Adopt }; RefPtr() = default; RefPtr(T const* ptr) : m_ptr(const_cast(ptr)) { ref_if_not_null(m_ptr); } RefPtr(T const& object) : m_ptr(const_cast(&object)) { m_ptr->ref(); } RefPtr(AdoptTag, T& object) : m_ptr(&object) { } RefPtr(RefPtr&& other) : m_ptr(other.leak_ref()) { } ALWAYS_INLINE RefPtr(NonnullRefPtr const& other) : m_ptr(const_cast(other.ptr())) { m_ptr->ref(); } template ALWAYS_INLINE RefPtr(NonnullRefPtr const& other) requires(IsConvertible) : m_ptr(const_cast(static_cast(other.ptr()))) { m_ptr->ref(); } template ALWAYS_INLINE RefPtr(NonnullRefPtr&& other) requires(IsConvertible) : m_ptr(static_cast(&other.leak_ref())) { } template> RefPtr(RefPtr&& other) requires(IsConvertible) : m_ptr(static_cast(other.leak_ref())) { } RefPtr(RefPtr const& other) : m_ptr(other.m_ptr) { ref_if_not_null(m_ptr); } template> RefPtr(RefPtr const& other) requires(IsConvertible) : m_ptr(const_cast(static_cast(other.ptr()))) { ref_if_not_null(m_ptr); } ALWAYS_INLINE ~RefPtr() { clear(); # ifdef SANITIZE_PTRS m_ptr = reinterpret_cast(explode_byte(0xe0)); # endif } template RefPtr(OwnPtr const&) = delete; template RefPtr& operator=(OwnPtr const&) = delete; void swap(RefPtr& other) { AK::swap(m_ptr, other.m_ptr); } template> void swap(RefPtr& other) requires(IsConvertible) { AK::swap(m_ptr, other.m_ptr); } ALWAYS_INLINE RefPtr& operator=(RefPtr&& other) { RefPtr tmp { move(other) }; swap(tmp); return *this; } template> ALWAYS_INLINE RefPtr& operator=(RefPtr&& other) requires(IsConvertible) { RefPtr tmp { move(other) }; swap(tmp); return *this; } template ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr&& other) requires(IsConvertible) { RefPtr tmp { move(other) }; swap(tmp); return *this; } ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr const& other) { RefPtr tmp { other }; swap(tmp); return *this; } template ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr const& other) requires(IsConvertible) { RefPtr tmp { other }; swap(tmp); return *this; } ALWAYS_INLINE RefPtr& operator=(RefPtr const& other) { RefPtr tmp { other }; swap(tmp); return *this; } template ALWAYS_INLINE RefPtr& operator=(RefPtr const& other) requires(IsConvertible) { RefPtr tmp { other }; swap(tmp); return *this; } ALWAYS_INLINE RefPtr& operator=(T const* ptr) { RefPtr tmp { ptr }; swap(tmp); return *this; } ALWAYS_INLINE RefPtr& operator=(T const& object) { RefPtr tmp { object }; swap(tmp); return *this; } RefPtr& operator=(std::nullptr_t) { clear(); return *this; } ALWAYS_INLINE bool assign_if_null(RefPtr&& other) { if (this == &other) return is_null(); *this = move(other); return true; } template> ALWAYS_INLINE bool assign_if_null(RefPtr&& other) { if (this == &other) return is_null(); *this = move(other); return true; } ALWAYS_INLINE void clear() { unref_if_not_null(m_ptr); m_ptr = nullptr; } bool operator!() const { return !m_ptr; } [[nodiscard]] T* leak_ref() { return exchange(m_ptr, nullptr); } NonnullRefPtr release_nonnull() { auto* ptr = leak_ref(); VERIFY(ptr); return NonnullRefPtr(NonnullRefPtr::Adopt, *ptr); } ALWAYS_INLINE T* ptr() { return as_ptr(); } ALWAYS_INLINE const T* ptr() const { return as_ptr(); } ALWAYS_INLINE T* operator->() { return as_nonnull_ptr(); } ALWAYS_INLINE const T* operator->() const { return as_nonnull_ptr(); } ALWAYS_INLINE T& operator*() { return *as_nonnull_ptr(); } ALWAYS_INLINE const T& operator*() const { return *as_nonnull_ptr(); } ALWAYS_INLINE operator const T*() const { return as_ptr(); } ALWAYS_INLINE operator T*() { return as_ptr(); } ALWAYS_INLINE operator bool() { return !is_null(); } bool operator==(std::nullptr_t) const { return is_null(); } bool operator!=(std::nullptr_t) const { return !is_null(); } bool operator==(const RefPtr& other) const { return as_ptr() == other.as_ptr(); } bool operator!=(const RefPtr& other) const { return as_ptr() != other.as_ptr(); } bool operator==(RefPtr& other) { return as_ptr() == other.as_ptr(); } bool operator!=(RefPtr& other) { return as_ptr() != other.as_ptr(); } bool operator==(const T* other) const { return as_ptr() == other; } bool operator!=(const T* other) const { return as_ptr() != other; } bool operator==(T* other) { return as_ptr() == other; } bool operator!=(T* other) { return as_ptr() != other; } ALWAYS_INLINE bool is_null() const { return !m_ptr; } private: ALWAYS_INLINE T* as_ptr() const { return m_ptr; } ALWAYS_INLINE T* as_nonnull_ptr() const { VERIFY(m_ptr); return m_ptr; } T* m_ptr { nullptr }; }; template struct Formatter> : Formatter { void format(FormatBuilder& builder, const RefPtr& value) { Formatter::format(builder, value.ptr()); } }; template struct Traits> : public GenericTraits> { using PeekType = T*; using ConstPeekType = const T*; static unsigned hash(const RefPtr& p) { return ptr_hash(p.ptr()); } static bool equals(const RefPtr& a, const RefPtr& b) { return a.ptr() == b.ptr(); } }; template inline NonnullRefPtr static_ptr_cast(const NonnullRefPtr& ptr) { return NonnullRefPtr(static_cast(*ptr)); } template> inline RefPtr static_ptr_cast(const RefPtr& ptr) { return RefPtr(static_cast(ptr.ptr())); } template inline void swap(RefPtr& a, RefPtr& b) requires(IsConvertible) { a.swap(b); } template inline RefPtr adopt_ref_if_nonnull(T* object) { if (object) return RefPtr(RefPtr::Adopt, *object); return {}; } template requires(IsConstructible) inline RefPtr try_make_ref_counted(Args&&... args) { return adopt_ref_if_nonnull(new (nothrow) T(forward(args)...)); } // FIXME: Remove once P0960R3 is available in Clang. template inline RefPtr try_make_ref_counted(Args&&... args) { return adopt_ref_if_nonnull(new (nothrow) T { forward(args)... }); } } using AK::adopt_ref_if_nonnull; using AK::RefPtr; using AK::static_ptr_cast; using AK::try_make_ref_counted; #endif