1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 03:08:13 +00:00

LibCore: Convert CHttpJob to ObjectPtr

This commit is contained in:
Andreas Kling 2019-09-21 12:03:22 +02:00
parent 953cb4e436
commit 6b347747f2
8 changed files with 45 additions and 13 deletions

View file

@ -8,22 +8,46 @@ template<typename T>
class ObjectPtr {
public:
ObjectPtr() {}
ObjectPtr(T* ptr) : m_ptr(ptr) {}
ObjectPtr(T& ptr) : m_ptr(&ptr) {}
ObjectPtr(T* ptr)
: m_ptr(ptr)
{
}
ObjectPtr(T& ptr)
: m_ptr(&ptr)
{
}
~ObjectPtr()
{
if (m_ptr && !m_ptr->parent())
delete m_ptr;
}
template<typename U>
ObjectPtr(U* ptr)
: m_ptr(static_cast<T*>(ptr))
{
}
ObjectPtr(const ObjectPtr& other)
: m_ptr(other.m_ptr)
{
}
template<typename U>
ObjectPtr(const ObjectPtr<U>& other)
: m_ptr(static_cast<T*>(const_cast<ObjectPtr<U>&>(other).ptr()))
{
}
ObjectPtr(ObjectPtr&& other)
{
m_ptr = exchange(other.m_ptr, nullptr);
m_ptr = other.leak_ptr();
}
template<typename U>
ObjectPtr(const ObjectPtr<U>&& other)
{
m_ptr = static_cast<T*>(const_cast<ObjectPtr<U>&>(other).leak_ptr());
}
ObjectPtr& operator=(const ObjectPtr& other)
@ -49,6 +73,9 @@ public:
T& operator*() { return *m_ptr; }
const T& operator*() const { return *m_ptr; }
T* ptr() const { return m_ptr; }
T* leak_ptr() { return exchange(m_ptr, nullptr); }
private:
T* m_ptr { nullptr };
};