#pragma once #include "Weakable.h" namespace AK { template class WeakPtr { friend class Weakable; public: WeakPtr() { } WeakPtr(std::nullptr_t) { } template WeakPtr(WeakPtr&& other) : m_link(reinterpret_cast*>(other.leakLink())) { } template WeakPtr& operator=(WeakPtr&& other) { m_link = reinterpret_cast*>(other.leakLink()); 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(); } bool is_null() const { return !m_link || !m_link->ptr(); } void clear() { m_link = nullptr; } WeakLink* leakLink() { return m_link.leakRef(); } private: WeakPtr(RetainPtr>&& link) : m_link(move(link)) { } RetainPtr> m_link; }; template inline WeakPtr Weakable::makeWeakPtr() { if (!m_link) m_link = adopt(*new WeakLink(static_cast(*this))); return WeakPtr(m_link.copyRef()); } } using AK::WeakPtr;