mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 10:57:35 +00:00
Add WeakPtr/Weakable templates.
This commit is contained in:
parent
b7efd92937
commit
3e9a45d7f4
4 changed files with 109 additions and 2 deletions
37
AK/WeakPtr.h
Normal file
37
AK/WeakPtr.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include "Weakable.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class WeakPtr {
|
||||
friend class Weakable<T>;
|
||||
public:
|
||||
WeakPtr() { }
|
||||
WeakPtr(std::nullptr_t) { }
|
||||
|
||||
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; }
|
||||
bool isNull() const { return !m_link || !m_link->ptr(); }
|
||||
|
||||
private:
|
||||
WeakPtr(RetainPtr<WeakLink<T>>&& link) : m_link(std::move(link)) { }
|
||||
|
||||
RetainPtr<WeakLink<T>> m_link;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline WeakPtr<T> Weakable<T>::makeWeakPtr()
|
||||
{
|
||||
if (!m_link)
|
||||
m_link = adopt(*new WeakLink<T>(*this));
|
||||
return WeakPtr<T>(m_link.copyRef());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using AK::WeakPtr;
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue