1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 16:07:46 +00:00

Add WeakPtr/Weakable templates.

This commit is contained in:
Andreas Kling 2018-10-13 15:41:24 +02:00
parent b7efd92937
commit 3e9a45d7f4
4 changed files with 109 additions and 2 deletions

45
AK/Weakable.h Normal file
View file

@ -0,0 +1,45 @@
#pragma once
#include "Assertions.h"
#include "Retainable.h"
namespace AK {
template<typename T> class Weakable;
template<typename T> class WeakPtr;
template<typename T>
class WeakLink : public Retainable<WeakLink<T>> {
friend class Weakable<T>;
public:
T* ptr() { return static_cast<T*>(m_ptr); }
const T* ptr() const { return static_cast<const T*>(m_ptr); }
private:
explicit WeakLink(Weakable<T>& weakable) : m_ptr(&weakable) { }
Weakable<T>* m_ptr;
};
template<typename T>
class Weakable {
private:
class Link;
public:
WeakPtr<T> makeWeakPtr();
protected:
Weakable() { }
~Weakable()
{
if (m_link)
m_link->m_ptr = nullptr;
}
private:
RetainPtr<WeakLink<T>> m_link;
};
}
using AK::Weakable;