aboutsummaryrefslogtreecommitdiff
path: root/lib/jobs
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-01-02 06:03:26 +0100
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-01-02 06:09:38 +0100
commit7d37d296f942ac993d041b4576ed52265170c4a8 (patch)
treebf36eca388b7fdc363ef21f4defa8ac9c449141d /lib/jobs
parentd516280a2b38ccb060e4f7502b873e19b1559ed1 (diff)
downloadlibquotient-7d37d296f942ac993d041b4576ed52265170c4a8.tar.gz
libquotient-7d37d296f942ac993d041b4576ed52265170c4a8.zip
Add ImplPtr and makeImpl
The original (more complex and comprehensive) solution belongs to https://oliora.github.io/2015/12/29/pimpl-and-rule-of-zero.html - this commit only provides a small wrapper for non-copyable Private class implementations common throughout libQuotient. Unlike the original, default initialisation is made explicit - you have to pass ZeroImpl<Private>() instead (and I firmly believe it's a good thing: normally pointers to Private should not remain nullptr). The reason ZeroImpl<> is not a template variable is quite simple: unique_ptr is non-copyable and so cannot be initialised from; while a template function will initialise the value in-place thanks to copy elision.
Diffstat (limited to 'lib/jobs')
-rw-r--r--lib/jobs/basejob.cpp4
-rw-r--r--lib/jobs/basejob.h2
-rw-r--r--lib/jobs/downloadfilejob.cpp5
-rw-r--r--lib/jobs/downloadfilejob.h3
4 files changed, 6 insertions, 8 deletions
diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp
index 971fea7b..f518a1b0 100644
--- a/lib/jobs/basejob.cpp
+++ b/lib/jobs/basejob.cpp
@@ -194,8 +194,8 @@ BaseJob::BaseJob(HttpVerb verb, const QString& name, QByteArray endpoint,
BaseJob::BaseJob(HttpVerb verb, const QString& name, QByteArray endpoint,
const QUrlQuery& query, RequestData&& data, bool needsToken)
- : d(new Private(verb, std::move(endpoint), query, std::move(data),
- needsToken))
+ : d(makeImpl<Private>(verb, std::move(endpoint), query, std::move(data),
+ needsToken))
{
setObjectName(name);
connect(&d->timer, &QTimer::timeout, this, &BaseJob::timeout);
diff --git a/lib/jobs/basejob.h b/lib/jobs/basejob.h
index f41fc63c..c899170d 100644
--- a/lib/jobs/basejob.h
+++ b/lib/jobs/basejob.h
@@ -467,7 +467,7 @@ private:
void finishJob();
class Private;
- QScopedPointer<Private> d;
+ ImplPtr<Private> d;
};
inline bool QUOTIENT_API isJobPending(BaseJob* job)
diff --git a/lib/jobs/downloadfilejob.cpp b/lib/jobs/downloadfilejob.cpp
index 6bf221cb..4a507ebd 100644
--- a/lib/jobs/downloadfilejob.cpp
+++ b/lib/jobs/downloadfilejob.cpp
@@ -32,13 +32,12 @@ DownloadFileJob::DownloadFileJob(const QString& serverName,
const QString& mediaId,
const QString& localFilename)
: GetContentJob(serverName, mediaId)
- , d(localFilename.isEmpty() ? new Private : new Private(localFilename))
+ , d(localFilename.isEmpty() ? makeImpl<Private>()
+ : makeImpl<Private>(localFilename))
{
setObjectName(QStringLiteral("DownloadFileJob"));
}
-DownloadFileJob::~DownloadFileJob() = default;
-
QString DownloadFileJob::targetFileName() const
{
return (d->targetFile ? d->targetFile : d->tempFile)->fileName();
diff --git a/lib/jobs/downloadfilejob.h b/lib/jobs/downloadfilejob.h
index 9e807fe7..f8c62e4b 100644
--- a/lib/jobs/downloadfilejob.h
+++ b/lib/jobs/downloadfilejob.h
@@ -13,13 +13,12 @@ public:
DownloadFileJob(const QString& serverName, const QString& mediaId,
const QString& localFilename = {});
- ~DownloadFileJob() override;
QString targetFileName() const;
private:
class Private;
- QScopedPointer<Private> d;
+ ImplPtr<Private> d;
void doPrepare() override;
void onSentRequest(QNetworkReply* reply) override;