Skip to content

Commit

Permalink
BaseJob: use string views where appropriate
Browse files Browse the repository at this point in the history
Basically replicating QJson* API capabilities for specific calls.
  • Loading branch information
KitsuneRal committed Aug 25, 2024
1 parent ba5e93b commit 9e1e449
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 11 additions & 1 deletion Quotient/jobs/basejob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,17 @@ BaseJob::Status BaseJob::prepareError(Status currentStatus)
return currentStatus; // The error payload is not recognised
}

QJsonValue BaseJob::takeValueFromJson(const QString& key)
QJsonValue BaseJob::takeValueFromJson(QStringView key)
{
if (!d->jsonResponse.isObject())
return QJsonValue::Undefined;
auto o = d->jsonResponse.object();
auto v = o.take(key);
d->jsonResponse.setObject(o);
return v;
}

QJsonValue BaseJob::takeValueFromJson(QLatin1StringView key)
{
if (!d->jsonResponse.isObject())
return QJsonValue::Undefined;
Expand Down
10 changes: 5 additions & 5 deletions Quotient/jobs/basejob.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,19 @@ class QUOTIENT_API BaseJob : public QObject {
//!
//! If there's no top-level JSON object in the response or if there's
//! no node with the key \p keyName, \p defaultValue is returned.
template <typename T, typename StrT>
T loadFromJson(const StrT& keyName, T&& defaultValue = {}) const
template <typename T>
T loadFromJson(auto keyName, T&& defaultValue = {}) const
{
const auto& jv = jsonData().value(keyName);
return jv.isUndefined() ? std::forward<T>(defaultValue)
: fromJson<T>(jv);
return jv.isUndefined() ? std::forward<T>(defaultValue) : fromJson<T>(jv);
}

//! \brief Load the property from the JSON response and delete it from JSON
//!
//! If there's no top-level JSON object in the response or if there's
//! no node with the key \p keyName, \p defaultValue is returned.
template <typename T>
T takeFromJson(const QString& key, T&& defaultValue = {})
T takeFromJson(auto key, T&& defaultValue = {})
{
if (const auto& jv = takeValueFromJson(key); !jv.isUndefined())
return fromJson<T>(jv);
Expand Down Expand Up @@ -432,6 +431,7 @@ private Q_SLOTS:
template <class JobT>
friend class JobHandle;

private:
void stop();
void finishJob();
QFuture<void> future();
Expand Down

0 comments on commit 9e1e449

Please sign in to comment.