Add WeakPtr/Weakable templates.

This commit is contained in:
Andreas Kling 2018-10-13 15:41:24 +02:00
parent b7efd92937
commit 3e9a45d7f4
Notes: sideshowbarker 2024-07-19 18:49:06 +09:00
4 changed files with 109 additions and 2 deletions

View file

@ -7,8 +7,6 @@ namespace AK {
template<typename T>
class Retainable {
public:
Retainable() { }
void retain()
{
ASSERT(m_retainCount);
@ -28,6 +26,7 @@ public:
}
protected:
Retainable() { }
~Retainable()
{
ASSERT(!m_retainCount);

37
AK/WeakPtr.h Normal file
View 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;

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;

View file

@ -7,6 +7,10 @@
#include "HashMap.h"
#include "TemporaryFile.h"
#include "Buffer.h"
#include "Weakable.h"
#include "WeakPtr.h"
static void testWeakPtr();
int main(int, char**)
{
@ -183,5 +187,27 @@ int main(int, char**)
printInts(h);
}
testWeakPtr();
return 0;
}
class TestWeakable : public Weakable<TestWeakable> {
public:
TestWeakable() { }
~TestWeakable() { }
};
void testWeakPtr()
{
auto* weakable = new TestWeakable;
auto weakPtr = weakable->makeWeakPtr();
ASSERT(weakPtr);
ASSERT(weakPtr.ptr() == weakable);
delete weakable;
ASSERT(!weakPtr);
ASSERT(weakPtr.ptr() == nullptr);
}