1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:37:46 +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

@ -10,9 +10,9 @@ CHttpRequest::~CHttpRequest()
{
}
CNetworkJob* CHttpRequest::schedule()
ObjectPtr<CNetworkJob> CHttpRequest::schedule()
{
auto* job = new CHttpJob(*this);
auto job = CHttpJob::construct(*this);
job->start();
return job;
}

View file

@ -2,6 +2,7 @@
#include <AK/String.h>
#include <AK/URL.h>
#include <LibCore/ObjectPtr.h>
class CNetworkJob;
@ -26,7 +27,7 @@ public:
String method_name() const;
ByteBuffer to_raw_request() const;
CNetworkJob* schedule();
ObjectPtr<CNetworkJob> schedule();
private:
URL m_url;

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 };
};