From 610631675826bb572bff97ce7d16d07097f14e3f Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Mon, 20 Jun 2022 18:16:19 +0200 Subject: Clean up RequestData from Sonar warnings Also: make ImplPtr more flexible. --- lib/jobs/requestdata.cpp | 6 ++---- lib/jobs/requestdata.h | 22 +++++++++------------- lib/util.h | 29 +++++++++++++++++++---------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/lib/jobs/requestdata.cpp b/lib/jobs/requestdata.cpp index 2c001ccc..ab249f6d 100644 --- a/lib/jobs/requestdata.cpp +++ b/lib/jobs/requestdata.cpp @@ -14,7 +14,7 @@ using namespace Quotient; auto fromData(const QByteArray& data) { - auto source = std::make_unique(); + auto source = makeImpl(); source->setData(data); source->open(QIODevice::ReadOnly); return source; @@ -33,7 +33,5 @@ RequestData::RequestData(const QJsonObject& jo) : _source(fromJson(jo)) {} RequestData::RequestData(const QJsonArray& ja) : _source(fromJson(ja)) {} RequestData::RequestData(QIODevice* source) - : _source(std::unique_ptr(source)) + : _source(acquireImpl(source)) {} - -RequestData::~RequestData() = default; diff --git a/lib/jobs/requestdata.h b/lib/jobs/requestdata.h index 41ad833a..accc8f71 100644 --- a/lib/jobs/requestdata.h +++ b/lib/jobs/requestdata.h @@ -3,11 +3,7 @@ #pragma once -#include "quotient_export.h" - -#include - -#include +#include "util.h" class QJsonObject; class QJsonArray; @@ -23,17 +19,17 @@ namespace Quotient { */ class QUOTIENT_API RequestData { public: - RequestData(const QByteArray& a = {}); - RequestData(const QJsonObject& jo); - RequestData(const QJsonArray& ja); - RequestData(QIODevice* source); - RequestData(RequestData&&) = default; - RequestData& operator=(RequestData&&) = default; - ~RequestData(); + // NOLINTBEGIN(google-explicit-constructor): that check should learn about + // explicit(false) + QUO_IMPLICIT RequestData(const QByteArray& a = {}); + QUO_IMPLICIT RequestData(const QJsonObject& jo); + QUO_IMPLICIT RequestData(const QJsonArray& ja); + QUO_IMPLICIT RequestData(QIODevice* source); + // NOLINTEND(google-explicit-constructor) QIODevice* source() const { return _source.get(); } private: - std::unique_ptr _source; + ImplPtr _source; }; } // namespace Quotient diff --git a/lib/util.h b/lib/util.h index 8a30f457..46b1767e 100644 --- a/lib/util.h +++ b/lib/util.h @@ -135,8 +135,8 @@ inline std::pair findFirstOf(InputIt first, InputIt last, //! to define default constructors/operator=() out of line. //! Thanks to https://oliora.github.io/2015/12/29/pimpl-and-rule-of-zero.html //! for inspiration -template -using ImplPtr = std::unique_ptr; +template +using ImplPtr = std::unique_ptr; // Why this works (see also the link above): because this defers the moment // of requiring sizeof of ImplType to the place where makeImpl is invoked @@ -156,18 +156,27 @@ using ImplPtr = std::unique_ptr; //! //! Since std::make_unique is not compatible with ImplPtr, this should be used //! in constructors of frontend classes to create implementation instances. -template -inline ImplPtr makeImpl(ArgTs&&... args) +template +inline ImplPtr makeImpl(ArgTs&&... args) { - return ImplPtr { new ImplType(std::forward(args)...), - [](ImplType* impl) { delete impl; } }; + return ImplPtr { + new ImplType(std::forward(args)...), + [](TypeToDelete* impl) { delete impl; } + }; } -template -constexpr ImplPtr ZeroImpl() +template +inline ImplPtr acquireImpl(ImplType* from) { - return { nullptr, [](ImplType*) { /* nullptr doesn't need deletion */ } }; + return ImplPtr { from, [](TypeToDelete* impl) { + delete impl; + } }; +} + +template +constexpr ImplPtr ZeroImpl() +{ + return { nullptr, [](TypeToDelete*) { /* nullptr doesn't need deletion */ } }; } //! \brief Multiplex several functors in one -- cgit v1.2.3