diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-06-20 18:16:19 +0200 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-06-22 14:03:40 +0200 |
commit | 610631675826bb572bff97ce7d16d07097f14e3f (patch) | |
tree | 03bcd2b0e02773af0281cc3727c976bd7cc523d0 /lib | |
parent | 04db5b6d853f3084660612c1d2db91d54ec63691 (diff) | |
download | libquotient-610631675826bb572bff97ce7d16d07097f14e3f.tar.gz libquotient-610631675826bb572bff97ce7d16d07097f14e3f.zip |
Clean up RequestData from Sonar warnings
Also: make ImplPtr more flexible.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/jobs/requestdata.cpp | 6 | ||||
-rw-r--r-- | lib/jobs/requestdata.h | 22 | ||||
-rw-r--r-- | 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<QBuffer>(); + auto source = makeImpl<QBuffer, QIODevice>(); 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<QIODevice>(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 <QtCore/QByteArray> - -#include <memory> +#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<QIODevice> _source; + ImplPtr<QIODevice> _source; }; } // namespace Quotient @@ -135,8 +135,8 @@ inline std::pair<InputIt, ForwardIt> 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 <typename ImplType> -using ImplPtr = std::unique_ptr<ImplType, void (*)(ImplType*)>; +template <typename ImplType, typename TypeToDelete = ImplType> +using ImplPtr = std::unique_ptr<ImplType, void (*)(TypeToDelete*)>; // 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<ImplType, void (*)(ImplType*)>; //! //! Since std::make_unique is not compatible with ImplPtr, this should be used //! in constructors of frontend classes to create implementation instances. -template <typename ImplType, typename DeleterType = void (*)(ImplType*), - typename... ArgTs> -inline ImplPtr<ImplType> makeImpl(ArgTs&&... args) +template <typename ImplType, typename TypeToDelete = ImplType, typename... ArgTs> +inline ImplPtr<ImplType, TypeToDelete> makeImpl(ArgTs&&... args) { - return ImplPtr<ImplType> { new ImplType(std::forward<ArgTs>(args)...), - [](ImplType* impl) { delete impl; } }; + return ImplPtr<ImplType, TypeToDelete> { + new ImplType(std::forward<ArgTs>(args)...), + [](TypeToDelete* impl) { delete impl; } + }; } -template <typename ImplType> -constexpr ImplPtr<ImplType> ZeroImpl() +template <typename ImplType, typename TypeToDelete = ImplType> +inline ImplPtr<ImplType, TypeToDelete> acquireImpl(ImplType* from) { - return { nullptr, [](ImplType*) { /* nullptr doesn't need deletion */ } }; + return ImplPtr<ImplType, TypeToDelete> { from, [](TypeToDelete* impl) { + delete impl; + } }; +} + +template <typename ImplType, typename TypeToDelete = ImplType> +constexpr ImplPtr<ImplType, TypeToDelete> ZeroImpl() +{ + return { nullptr, [](TypeToDelete*) { /* nullptr doesn't need deletion */ } }; } //! \brief Multiplex several functors in one |