mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 23:37:43 +00:00
LibCore: Convert CHttpJob to ObjectPtr
This commit is contained in:
parent
953cb4e436
commit
6b347747f2
8 changed files with 45 additions and 13 deletions
|
@ -21,10 +21,10 @@ void BoardListModel::update()
|
||||||
CHttpRequest request;
|
CHttpRequest request;
|
||||||
request.set_url("http://a.4cdn.org/boards.json");
|
request.set_url("http://a.4cdn.org/boards.json");
|
||||||
|
|
||||||
auto* job = request.schedule();
|
m_pending_job = request.schedule();
|
||||||
|
|
||||||
job->on_finish = [job, this](bool success) {
|
m_pending_job->on_finish = [this](bool success) {
|
||||||
auto* response = job->response();
|
auto* response = m_pending_job->response();
|
||||||
dbg() << "Board list download finished, success=" << success << ", response=" << response;
|
dbg() << "Board list download finished, success=" << success << ", response=" << response;
|
||||||
|
|
||||||
if (!success)
|
if (!success)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
|
#include <LibCore/CHttpJob.h>
|
||||||
#include <LibGUI/GModel.h>
|
#include <LibGUI/GModel.h>
|
||||||
|
|
||||||
class BoardListModel final : public GModel {
|
class BoardListModel final : public GModel {
|
||||||
|
@ -24,4 +25,5 @@ private:
|
||||||
BoardListModel();
|
BoardListModel();
|
||||||
|
|
||||||
JsonArray m_boards;
|
JsonArray m_boards;
|
||||||
|
ObjectPtr<CHttpJob> m_pending_job;
|
||||||
};
|
};
|
||||||
|
|
|
@ -29,13 +29,13 @@ void ThreadCatalogModel::update()
|
||||||
CHttpRequest request;
|
CHttpRequest request;
|
||||||
request.set_url(String::format("http://a.4cdn.org/%s/catalog.json", m_board.characters()));
|
request.set_url(String::format("http://a.4cdn.org/%s/catalog.json", m_board.characters()));
|
||||||
|
|
||||||
auto* job = request.schedule();
|
m_pending_job = request.schedule();
|
||||||
|
|
||||||
if (on_load_started)
|
if (on_load_started)
|
||||||
on_load_started();
|
on_load_started();
|
||||||
|
|
||||||
job->on_finish = [job, this](bool success) {
|
m_pending_job->on_finish = [this](bool success) {
|
||||||
auto* response = job->response();
|
auto* response = m_pending_job->response();
|
||||||
dbg() << "Catalog download finished, success=" << success << ", response=" << response;
|
dbg() << "Catalog download finished, success=" << success << ", response=" << response;
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
|
#include <LibCore/CHttpJob.h>
|
||||||
#include <LibGUI/GModel.h>
|
#include <LibGUI/GModel.h>
|
||||||
|
|
||||||
class ThreadCatalogModel final : public GModel {
|
class ThreadCatalogModel final : public GModel {
|
||||||
|
@ -36,4 +37,5 @@ private:
|
||||||
|
|
||||||
String m_board { "g" };
|
String m_board { "g" };
|
||||||
JsonArray m_catalog;
|
JsonArray m_catalog;
|
||||||
|
ObjectPtr<CHttpJob> m_pending_job;
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,7 +18,7 @@ int main(int argc, char** argv)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto& response = static_cast<const CHttpResponse&>(*job->response());
|
auto& response = static_cast<const CHttpResponse&>(*job->response());
|
||||||
printf("%s{%p}: on_receive: code=%d\n", job->class_name(), job, response.code());
|
printf("%s{%p}: on_receive: code=%d\n", job->class_name(), job.ptr(), response.code());
|
||||||
//printf("payload:\n");
|
//printf("payload:\n");
|
||||||
//printf("%s", response.payload().pointer());
|
//printf("%s", response.payload().pointer());
|
||||||
printf("payload was %d bytes\n", response.payload().size());
|
printf("payload was %d bytes\n", response.payload().size());
|
||||||
|
|
|
@ -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();
|
job->start();
|
||||||
return job;
|
return job;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
|
#include <LibCore/ObjectPtr.h>
|
||||||
|
|
||||||
class CNetworkJob;
|
class CNetworkJob;
|
||||||
|
|
||||||
|
@ -26,7 +27,7 @@ public:
|
||||||
String method_name() const;
|
String method_name() const;
|
||||||
ByteBuffer to_raw_request() const;
|
ByteBuffer to_raw_request() const;
|
||||||
|
|
||||||
CNetworkJob* schedule();
|
ObjectPtr<CNetworkJob> schedule();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
URL m_url;
|
URL m_url;
|
||||||
|
|
|
@ -8,22 +8,46 @@ template<typename T>
|
||||||
class ObjectPtr {
|
class ObjectPtr {
|
||||||
public:
|
public:
|
||||||
ObjectPtr() {}
|
ObjectPtr() {}
|
||||||
ObjectPtr(T* ptr) : m_ptr(ptr) {}
|
ObjectPtr(T* ptr)
|
||||||
ObjectPtr(T& ptr) : m_ptr(&ptr) {}
|
: m_ptr(ptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
ObjectPtr(T& ptr)
|
||||||
|
: m_ptr(&ptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
~ObjectPtr()
|
~ObjectPtr()
|
||||||
{
|
{
|
||||||
if (m_ptr && !m_ptr->parent())
|
if (m_ptr && !m_ptr->parent())
|
||||||
delete m_ptr;
|
delete m_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename U>
|
||||||
|
ObjectPtr(U* ptr)
|
||||||
|
: m_ptr(static_cast<T*>(ptr))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
ObjectPtr(const ObjectPtr& other)
|
ObjectPtr(const ObjectPtr& other)
|
||||||
: m_ptr(other.m_ptr)
|
: 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)
|
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)
|
ObjectPtr& operator=(const ObjectPtr& other)
|
||||||
|
@ -49,6 +73,9 @@ public:
|
||||||
T& operator*() { return *m_ptr; }
|
T& operator*() { return *m_ptr; }
|
||||||
const T& operator*() const { 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:
|
private:
|
||||||
T* m_ptr { nullptr };
|
T* m_ptr { nullptr };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue