#pragma once #include "Assertions.h" #include "RefPtr.h" #include "RefCounted.h" namespace AK { template class Weakable; template class WeakPtr; template class WeakLink : public RefCounted> { friend class Weakable; public: T* ptr() { return static_cast(m_ptr); } const T* ptr() const { return static_cast(m_ptr); } private: explicit WeakLink(T& weakable) : m_ptr(&weakable) { } T* m_ptr; }; template class Weakable { private: class Link; public: WeakPtr make_weak_ptr(); protected: Weakable() {} ~Weakable() { if (m_link) m_link->m_ptr = nullptr; } private: RefPtr> m_link; }; } using AK::Weakable;