From 6b347747f2f912721bc076fcd4aabb8a4bfa9e98 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 21 Sep 2019 12:03:22 +0200 Subject: [PATCH] LibCore: Convert CHttpJob to ObjectPtr --- Applications/ChanViewer/BoardListModel.cpp | 6 ++-- Applications/ChanViewer/BoardListModel.h | 2 ++ .../ChanViewer/ThreadCatalogModel.cpp | 6 ++-- Applications/ChanViewer/ThreadCatalogModel.h | 2 ++ Applications/Downloader/main.cpp | 2 +- Libraries/LibCore/CHttpRequest.cpp | 4 +-- Libraries/LibCore/CHttpRequest.h | 3 +- Libraries/LibCore/ObjectPtr.h | 33 +++++++++++++++++-- 8 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Applications/ChanViewer/BoardListModel.cpp b/Applications/ChanViewer/BoardListModel.cpp index d7120947e1..8f8b7dfbdc 100644 --- a/Applications/ChanViewer/BoardListModel.cpp +++ b/Applications/ChanViewer/BoardListModel.cpp @@ -21,10 +21,10 @@ void BoardListModel::update() CHttpRequest request; 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) { - auto* response = job->response(); + m_pending_job->on_finish = [this](bool success) { + auto* response = m_pending_job->response(); dbg() << "Board list download finished, success=" << success << ", response=" << response; if (!success) diff --git a/Applications/ChanViewer/BoardListModel.h b/Applications/ChanViewer/BoardListModel.h index 13a997c85e..405b8f17dd 100644 --- a/Applications/ChanViewer/BoardListModel.h +++ b/Applications/ChanViewer/BoardListModel.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include class BoardListModel final : public GModel { @@ -24,4 +25,5 @@ private: BoardListModel(); JsonArray m_boards; + ObjectPtr m_pending_job; }; diff --git a/Applications/ChanViewer/ThreadCatalogModel.cpp b/Applications/ChanViewer/ThreadCatalogModel.cpp index d864f3cc36..789b5d5f4d 100644 --- a/Applications/ChanViewer/ThreadCatalogModel.cpp +++ b/Applications/ChanViewer/ThreadCatalogModel.cpp @@ -29,13 +29,13 @@ void ThreadCatalogModel::update() CHttpRequest request; 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) on_load_started(); - job->on_finish = [job, this](bool success) { - auto* response = job->response(); + m_pending_job->on_finish = [this](bool success) { + auto* response = m_pending_job->response(); dbg() << "Catalog download finished, success=" << success << ", response=" << response; if (!success) { diff --git a/Applications/ChanViewer/ThreadCatalogModel.h b/Applications/ChanViewer/ThreadCatalogModel.h index 84210c2b13..bdf428f58f 100644 --- a/Applications/ChanViewer/ThreadCatalogModel.h +++ b/Applications/ChanViewer/ThreadCatalogModel.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include class ThreadCatalogModel final : public GModel { @@ -36,4 +37,5 @@ private: String m_board { "g" }; JsonArray m_catalog; + ObjectPtr m_pending_job; }; diff --git a/Applications/Downloader/main.cpp b/Applications/Downloader/main.cpp index ba0f270273..6d8fbffdee 100644 --- a/Applications/Downloader/main.cpp +++ b/Applications/Downloader/main.cpp @@ -18,7 +18,7 @@ int main(int argc, char** argv) return; } auto& response = static_cast(*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("%s", response.payload().pointer()); printf("payload was %d bytes\n", response.payload().size()); diff --git a/Libraries/LibCore/CHttpRequest.cpp b/Libraries/LibCore/CHttpRequest.cpp index bc64f3169d..4092249f0d 100644 --- a/Libraries/LibCore/CHttpRequest.cpp +++ b/Libraries/LibCore/CHttpRequest.cpp @@ -10,9 +10,9 @@ CHttpRequest::~CHttpRequest() { } -CNetworkJob* CHttpRequest::schedule() +ObjectPtr CHttpRequest::schedule() { - auto* job = new CHttpJob(*this); + auto job = CHttpJob::construct(*this); job->start(); return job; } diff --git a/Libraries/LibCore/CHttpRequest.h b/Libraries/LibCore/CHttpRequest.h index 0f9d35f96b..6d0d3a6259 100644 --- a/Libraries/LibCore/CHttpRequest.h +++ b/Libraries/LibCore/CHttpRequest.h @@ -2,6 +2,7 @@ #include #include +#include class CNetworkJob; @@ -26,7 +27,7 @@ public: String method_name() const; ByteBuffer to_raw_request() const; - CNetworkJob* schedule(); + ObjectPtr schedule(); private: URL m_url; diff --git a/Libraries/LibCore/ObjectPtr.h b/Libraries/LibCore/ObjectPtr.h index 67db2749fc..a34c8f8224 100644 --- a/Libraries/LibCore/ObjectPtr.h +++ b/Libraries/LibCore/ObjectPtr.h @@ -8,22 +8,46 @@ template 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 + ObjectPtr(U* ptr) + : m_ptr(static_cast(ptr)) + { + } + ObjectPtr(const ObjectPtr& other) : m_ptr(other.m_ptr) { } + template + ObjectPtr(const ObjectPtr& other) + : m_ptr(static_cast(const_cast&>(other).ptr())) + { + } + ObjectPtr(ObjectPtr&& other) { - m_ptr = exchange(other.m_ptr, nullptr); + m_ptr = other.leak_ptr(); + } + + template + ObjectPtr(const ObjectPtr&& other) + { + m_ptr = static_cast(const_cast&>(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 }; };