mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:17: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
|
@ -7,8 +7,6 @@ namespace AK {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Retainable {
|
class Retainable {
|
||||||
public:
|
public:
|
||||||
Retainable() { }
|
|
||||||
|
|
||||||
void retain()
|
void retain()
|
||||||
{
|
{
|
||||||
ASSERT(m_retainCount);
|
ASSERT(m_retainCount);
|
||||||
|
@ -28,6 +26,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Retainable() { }
|
||||||
~Retainable()
|
~Retainable()
|
||||||
{
|
{
|
||||||
ASSERT(!m_retainCount);
|
ASSERT(!m_retainCount);
|
||||||
|
|
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;
|
||||||
|
|
45
AK/Weakable.h
Normal file
45
AK/Weakable.h
Normal 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;
|
26
AK/test.cpp
26
AK/test.cpp
|
@ -7,6 +7,10 @@
|
||||||
#include "HashMap.h"
|
#include "HashMap.h"
|
||||||
#include "TemporaryFile.h"
|
#include "TemporaryFile.h"
|
||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
|
#include "Weakable.h"
|
||||||
|
#include "WeakPtr.h"
|
||||||
|
|
||||||
|
static void testWeakPtr();
|
||||||
|
|
||||||
int main(int, char**)
|
int main(int, char**)
|
||||||
{
|
{
|
||||||
|
@ -183,5 +187,27 @@ int main(int, char**)
|
||||||
printInts(h);
|
printInts(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testWeakPtr();
|
||||||
|
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue