#pragma once #include #include #include #include namespace AK { template class OwnPtr; template class RefPtr { public: enum AdoptTag { Adopt }; RefPtr() {} RefPtr(const T* ptr) : m_ptr(const_cast(ptr)) { ref_if_not_null(m_ptr); } RefPtr(T* ptr) : m_ptr(ptr) { ref_if_not_null(m_ptr); } RefPtr(T& object) : m_ptr(&object) { m_ptr->ref(); } RefPtr(const T& object) : m_ptr(const_cast(&object)) { m_ptr->ref(); } RefPtr(AdoptTag, T& object) : m_ptr(&object) { } RefPtr(RefPtr&& other) : m_ptr(other.leak_ref()) { } RefPtr(const NonnullRefPtr& other) : m_ptr(const_cast(other.ptr())) { ASSERT(m_ptr); m_ptr->ref(); } template RefPtr(const NonnullRefPtr& other) : m_ptr(static_cast(const_cast(other.ptr()))) { ASSERT(m_ptr); m_ptr->ref(); } template RefPtr(NonnullRefPtr&& other) : m_ptr(static_cast(&other.leak_ref())) { ASSERT(m_ptr); } template RefPtr(RefPtr&& other) : m_ptr(static_cast(other.leak_ref())) { } RefPtr(const RefPtr& other) : m_ptr(const_cast(other.ptr())) { ref_if_not_null(m_ptr); } template RefPtr(const RefPtr& other) : m_ptr(static_cast(const_cast(other.ptr()))) { ref_if_not_null(m_ptr); } ~RefPtr() { clear(); #ifdef SANITIZE_PTRS if constexpr (sizeof(T*) == 8) m_ptr = (T*)(0xe0e0e0e0e0e0e0e0); else m_ptr = (T*)(0xe0e0e0e0); #endif } RefPtr(std::nullptr_t) {} template RefPtr(const OwnPtr&) = delete; template RefPtr& operator=(const OwnPtr&) = delete; template void swap(RefPtr& other) { ::swap(m_ptr, other.m_ptr); } RefPtr& operator=(RefPtr&& other) { RefPtr tmp = move(other); swap(tmp); return *this; } template RefPtr& operator=(RefPtr&& other) { RefPtr tmp = move(other); swap(tmp); return *this; } template RefPtr& operator=(NonnullRefPtr&& other) { RefPtr tmp = move(other); swap(tmp); ASSERT(m_ptr); return *this; } RefPtr& operator=(const NonnullRefPtr& other) { RefPtr tmp = other; swap(tmp); ASSERT(m_ptr); return *this; } template RefPtr& operator=(const NonnullRefPtr& other) { RefPtr tmp = other; swap(tmp); ASSERT(m_ptr); return *this; } RefPtr& operator=(const RefPtr& other) { RefPtr tmp = other; swap(tmp); return *this; } template RefPtr& operator=(const RefPtr& other) { RefPtr tmp = other; swap(tmp); return *this; } RefPtr& operator=(const T* ptr) { RefPtr tmp = ptr; swap(tmp); return *this; } RefPtr& operator=(const T& object) { RefPtr tmp = object; swap(tmp); return *this; } RefPtr& operator=(std::nullptr_t) { clear(); return *this; } void clear() { deref_if_not_null(m_ptr); m_ptr = nullptr; } bool operator!() const { return !m_ptr; } [[nodiscard]] T* leak_ref() { return exchange(m_ptr, nullptr); } T* ptr() { return m_ptr; } const T* ptr() const { return m_ptr; } T* operator->() { ASSERT(m_ptr); return m_ptr; } const T* operator->() const { ASSERT(m_ptr); return m_ptr; } T& operator*() { ASSERT(m_ptr); return *m_ptr; } const T& operator*() const { ASSERT(m_ptr); return *m_ptr; } operator const T*() const { return m_ptr; } operator T*() { return m_ptr; } operator bool() { return !!m_ptr; } bool operator==(std::nullptr_t) const { return !m_ptr; } bool operator!=(std::nullptr_t) const { return m_ptr; } bool operator==(const RefPtr& other) const { return m_ptr == other.m_ptr; } bool operator!=(const RefPtr& other) const { return m_ptr != other.m_ptr; } bool operator==(RefPtr& other) { return m_ptr == other.m_ptr; } bool operator!=(RefPtr& other) { return m_ptr != other.m_ptr; } bool operator==(const T* other) const { return m_ptr == other; } bool operator!=(const T* other) const { return m_ptr != other; } bool operator==(T* other) { return m_ptr == other; } bool operator!=(T* other) { return m_ptr != other; } bool is_null() const { return !m_ptr; } private: T* m_ptr = nullptr; }; template inline const LogStream& operator<<(const LogStream& stream, const RefPtr& value) { return stream << value.ptr(); } } using AK::RefPtr;