#pragma once #include "Weakable.h" namespace AK { template class OwnPtr; template class WeakPtr { friend class Weakable; public: WeakPtr() {} WeakPtr(std::nullptr_t) {} template WeakPtr(WeakPtr&& other) : m_link(reinterpret_cast*>(other.leak_link())) { } template WeakPtr& operator=(WeakPtr&& other) { m_link = reinterpret_cast*>(other.leak_link()); return *this; } operator bool() const { return ptr(); } T* ptr() { return m_link ? m_link->ptr() : nullptr; } const T* ptr() const { return m_link ? m_link->ptr() : nullptr; } T* operator->() { return ptr(); } const T* operator->() const { return ptr(); } T& operator*() { return *ptr(); } const T& operator*() const { return *ptr(); } operator const T*() const { return ptr(); } operator T*() { return ptr(); } bool is_null() const { return !m_link || !m_link->ptr(); } void clear() { m_link = nullptr; } WeakLink* leak_link() { return m_link.leak_ref(); } bool operator==(const OwnPtr& other) const { return ptr() == other.ptr(); } private: WeakPtr(RetainPtr>&& link) : m_link(move(link)) { } RetainPtr> m_link; }; template inline WeakPtr Weakable::make_weak_ptr() { if (!m_link) m_link = adopt(*new WeakLink(static_cast(*this))); return WeakPtr(m_link.copy_ref()); } } using AK::WeakPtr;