TaskTree: Qt-ify the code

Make it more conforming to the current Qt style:
1. Remove trailing semicolons after inlined functions.
2. Use Q_SIGNALS / Q_EMIT in headers.
3. Use QString::fromLatin1() where needed (as otherwise the string
   c'tor is deprecated).
4. Use module names in Qt includes.
5. Simplify local asserts in barrier.cpp.

Change-Id: I13cadee6ff61a24d792efcf3b613f7cf5563076b
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-06-04 17:16:05 +02:00
parent dfef293aa4
commit 6621c68ca9
11 changed files with 51 additions and 54 deletions

View File

@@ -6,23 +6,20 @@
namespace Tasking { namespace Tasking {
// That's cut down qtcassert.{c,h} to avoid the dependency. // That's cut down qtcassert.{c,h} to avoid the dependency.
#define QTC_STRINGIFY_HELPER(x) #x #define QT_STRING(cond) qDebug("SOFT ASSERT: \"%s\" in %s: %s", cond, __FILE__, QT_STRINGIFY(__LINE__))
#define QTC_STRINGIFY(x) QTC_STRINGIFY_HELPER(x) #define QT_ASSERT(cond, action) if (Q_LIKELY(cond)) {} else { QT_STRING(#cond); action; } do {} while (0)
#define QTC_STRING(cond) qDebug("SOFT ASSERT: \"%s\" in %s: %s", cond, __FILE__, QTC_STRINGIFY(__LINE__))
#define QTC_ASSERT(cond, action) if (Q_LIKELY(cond)) {} else { QTC_STRING(#cond); action; } do {} while (0)
#define QTC_CHECK(cond) if (cond) {} else { QTC_STRING(#cond); } do {} while (0)
void Barrier::setLimit(int value) void Barrier::setLimit(int value)
{ {
QTC_ASSERT(!isRunning(), return); QT_ASSERT(!isRunning(), return);
QTC_ASSERT(value > 0, return); QT_ASSERT(value > 0, return);
m_limit = value; m_limit = value;
} }
void Barrier::start() void Barrier::start()
{ {
QTC_ASSERT(!isRunning(), return); QT_ASSERT(!isRunning(), return);
m_current = 0; m_current = 0;
m_result = {}; m_result = {};
} }
@@ -30,7 +27,7 @@ void Barrier::start()
void Barrier::advance() void Barrier::advance()
{ {
// Calling advance on finished is OK // Calling advance on finished is OK
QTC_ASSERT(isRunning() || m_result, return); QT_ASSERT(isRunning() || m_result, return);
if (!isRunning()) // no-op if (!isRunning()) // no-op
return; return;
++m_current; ++m_current;
@@ -41,7 +38,7 @@ void Barrier::advance()
void Barrier::stopWithResult(DoneResult result) void Barrier::stopWithResult(DoneResult result)
{ {
// Calling stopWithResult on finished is OK when the same success is passed // Calling stopWithResult on finished is OK when the same success is passed
QTC_ASSERT(isRunning() || (m_result && *m_result == result), return); QT_ASSERT(isRunning() || (m_result && *m_result == result), return);
if (!isRunning()) // no-op if (!isRunning()) // no-op
return; return;
m_current = -1; m_current = -1;

View File

@@ -25,7 +25,7 @@ public:
int current() const { return m_current; } int current() const { return m_current; }
std::optional<DoneResult> result() const { return m_result; } std::optional<DoneResult> result() const { return m_result; }
signals: Q_SIGNALS:
void done(DoneResult success); void done(DoneResult success);
private: private:

View File

@@ -5,7 +5,7 @@
#include "tasktree.h" #include "tasktree.h"
#include <QtConcurrent> #include <QtConcurrent/QtConcurrent>
namespace Tasking { namespace Tasking {
@@ -76,7 +76,7 @@ public:
} }
} }
void start() { void start() final {
if (!this->task()->m_startHandler) { if (!this->task()->m_startHandler) {
emit this->done(DoneResult::Error); // TODO: Add runtime assert emit this->done(DoneResult::Error); // TODO: Add runtime assert
return; return;

View File

@@ -3,7 +3,7 @@
#include "networkquery.h" #include "networkquery.h"
#include <QNetworkAccessManager> #include <QtNetwork/QNetworkAccessManager>
namespace Tasking { namespace Tasking {

View File

@@ -7,8 +7,8 @@
#include "tasktree.h" #include "tasktree.h"
#include <QNetworkReply> #include <QtNetwork/QNetworkReply>
#include <QNetworkRequest> #include <QtNetwork/QNetworkRequest>
#include <memory> #include <memory>
@@ -37,7 +37,7 @@ public:
QNetworkReply *reply() const { return m_reply.get(); } QNetworkReply *reply() const { return m_reply.get(); }
void start(); void start();
signals: Q_SIGNALS:
void started(); void started();
void done(DoneResult result); void done(DoneResult result);

View File

@@ -3,12 +3,12 @@
#include "qprocesstask.h" #include "qprocesstask.h"
#include <QCoreApplication> #include <QtCore/QCoreApplication>
#include <QDebug> #include <QtCore/QDebug>
#include <QMutex> #include <QtCore/QMutex>
#include <QThread> #include <QtCore/QThread>
#include <QTimer> #include <QtCore/QTimer>
#include <QWaitCondition> #include <QtCore/QWaitCondition>
namespace Tasking { namespace Tasking {
@@ -60,7 +60,7 @@ public:
terminate(); terminate();
} }
signals: Q_SIGNALS:
void finished(); void finished();
private: private:

View File

@@ -7,7 +7,7 @@
#include "tasktree.h" #include "tasktree.h"
#include <QProcess> #include <QtCore/QProcess>
namespace Tasking { namespace Tasking {
@@ -45,17 +45,17 @@ public:
class TASKING_EXPORT QProcessAdapter : public TaskAdapter<QProcess, QProcessDeleter> class TASKING_EXPORT QProcessAdapter : public TaskAdapter<QProcess, QProcessDeleter>
{ {
private: private:
void start() override { void start() final {
connect(task(), &QProcess::finished, this, [this] { connect(task(), &QProcess::finished, this, [this] {
const bool success = task()->exitStatus() == QProcess::NormalExit const bool success = task()->exitStatus() == QProcess::NormalExit
&& task()->error() == QProcess::UnknownError && task()->error() == QProcess::UnknownError
&& task()->exitCode() == 0; && task()->exitCode() == 0;
emit done(toDoneResult(success)); Q_EMIT done(toDoneResult(success));
}); });
connect(task(), &QProcess::errorOccurred, this, [this](QProcess::ProcessError error) { connect(task(), &QProcess::errorOccurred, this, [this](QProcess::ProcessError error) {
if (error != QProcess::FailedToStart) if (error != QProcess::FailedToStart)
return; return;
emit done(DoneResult::Error); Q_EMIT done(DoneResult::Error);
}); });
task()->start(); task()->start();
} }

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
#include <qglobal.h> #include <QtCore/qglobal.h>
#if defined(TASKING_LIBRARY) #if defined(TASKING_LIBRARY)
# define TASKING_EXPORT Q_DECL_EXPORT # define TASKING_EXPORT Q_DECL_EXPORT

View File

@@ -5,17 +5,17 @@
#include "barrier.h" #include "barrier.h"
#include <QDebug> #include <QtCore/QDebug>
#include <QEventLoop> #include <QtCore/QEventLoop>
#include <QFutureWatcher> #include <QtCore/QFutureWatcher>
#include <QHash> #include <QtCore/QHash>
#include <QMetaEnum> #include <QtCore/QMetaEnum>
#include <QMutex> #include <QtCore/QMutex>
#include <QPromise> #include <QtCore/QPointer>
#include <QPointer> #include <QtCore/QPromise>
#include <QSet> #include <QtCore/QSet>
#include <QTime> #include <QtCore/QTime>
#include <QTimer> #include <QtCore/QTimer>
using namespace std::chrono; using namespace std::chrono;
@@ -1461,7 +1461,7 @@ static QString currentTime() { return QTime::currentTime().toString(Qt::ISODateW
ExecutableItem ExecutableItem::withLog(const QString &logName) const ExecutableItem ExecutableItem::withLog(const QString &logName) const
{ {
const auto header = [logName] { const auto header = [logName] {
return QString("TASK TREE LOG [%1] \"%2\"").arg(currentTime(), logName); return QString::fromLatin1("TASK TREE LOG [%1] \"%2\"").arg(currentTime(), logName);
}; };
struct LogStorage struct LogStorage
{ {
@@ -1482,8 +1482,8 @@ ExecutableItem ExecutableItem::withLog(const QString &logName) const
const int asyncCountDiff = activeTaskTree()->asyncCount() - storage->asyncCount; const int asyncCountDiff = activeTaskTree()->asyncCount() - storage->asyncCount;
QT_CHECK(asyncCountDiff >= 0); QT_CHECK(asyncCountDiff >= 0);
const QMetaEnum doneWithEnum = QMetaEnum::fromType<DoneWith>(); const QMetaEnum doneWithEnum = QMetaEnum::fromType<DoneWith>();
const QString syncType = asyncCountDiff ? QString("asynchronously") const QString syncType = asyncCountDiff ? QString::fromLatin1("asynchronously")
: QString("synchronously"); : QString::fromLatin1("synchronously");
qDebug().noquote().nospace() << header() << " finished " << syncType << " with " qDebug().noquote().nospace() << header() << " finished " << syncType << " with "
<< doneWithEnum.valueToKey(int(result)) << " within " << elapsed.count() << "ms."; << doneWithEnum.valueToKey(int(result)) << " within " << elapsed.count() << "ms.";
}) })

View File

@@ -5,8 +5,8 @@
#include "tasking_global.h" #include "tasking_global.h"
#include <QObject> #include <QtCore/QList>
#include <QList> #include <QtCore/QObject>
#include <memory> #include <memory>
@@ -84,7 +84,7 @@ class TASKING_EXPORT TaskInterface : public QObject
{ {
Q_OBJECT Q_OBJECT
signals: Q_SIGNALS:
void done(DoneResult result); void done(DoneResult result);
private: private:
@@ -345,7 +345,7 @@ private:
std::invoke(handler); std::invoke(handler);
return SetupResult::Continue; return SetupResult::Continue;
}; };
}; }
template <typename Handler> template <typename Handler>
static GroupDoneHandler wrapGroupDone(Handler &&handler) static GroupDoneHandler wrapGroupDone(Handler &&handler)
{ {
@@ -368,7 +368,7 @@ private:
std::invoke(handler); std::invoke(handler);
return result == DoneWith::Success ? DoneResult::Success : DoneResult::Error; return result == DoneWith::Success ? DoneResult::Success : DoneResult::Error;
}; };
}; }
}; };
template <typename Handler> template <typename Handler>
@@ -433,7 +433,7 @@ private:
std::invoke(handler); std::invoke(handler);
return SetupResult::StopWithSuccess; return SetupResult::StopWithSuccess;
}; };
}; }
}; };
template <typename Task, typename Deleter = std::default_delete<Task>> template <typename Task, typename Deleter = std::default_delete<Task>>
@@ -490,7 +490,7 @@ private:
std::invoke(handler, *adapter.task()); std::invoke(handler, *adapter.task());
return SetupResult::Continue; return SetupResult::Continue;
}; };
}; }
template <typename Handler> template <typename Handler>
static InterfaceDoneHandler wrapDone(Handler &&handler) { static InterfaceDoneHandler wrapDone(Handler &&handler) {
@@ -529,7 +529,7 @@ private:
std::invoke(handler); std::invoke(handler);
return result == DoneWith::Success ? DoneResult::Success : DoneResult::Error; return result == DoneWith::Success ? DoneResult::Success : DoneResult::Error;
}; };
}; }
}; };
class TASKING_EXPORT TaskTree final : public QObject class TASKING_EXPORT TaskTree final : public QObject
@@ -579,7 +579,7 @@ public:
wrapHandler<const StorageStruct>(std::forward<Handler>(handler))); wrapHandler<const StorageStruct>(std::forward<Handler>(handler)));
} }
signals: Q_SIGNALS:
void started(); void started();
void done(DoneWith result); void done(DoneWith result);
void asyncCountChanged(int count); void asyncCountChanged(int count);

View File

@@ -6,7 +6,7 @@
#include "tasking_global.h" #include "tasking_global.h"
#include "tasktree.h" #include "tasktree.h"
#include <QObject> #include <QtCore/QObject>
namespace Tasking { namespace Tasking {
@@ -33,7 +33,7 @@ public:
// No done() signal is emitted. // No done() signal is emitted.
void reset(); void reset();
signals: Q_SIGNALS:
void aboutToStart(TaskTree *taskTree); void aboutToStart(TaskTree *taskTree);
void done(DoneWith result); void done(DoneWith result);