ThreadedParser: Simplify implementation

Get rid of unused errorString().
Remove pimpl and keep the QPointer to the Thread.

Change-Id: I6398d35b4df17b548ff06185fe516d0d19ed9bc0
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2023-08-05 15:25:05 +02:00
parent 94f4c21740
commit ec53435e04
2 changed files with 36 additions and 86 deletions

View File

@@ -2,8 +2,9 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "threadedparser.h" #include "threadedparser.h"
#include "parser.h"
#include "error.h" #include "error.h"
#include "parser.h"
#include "status.h" #include "status.h"
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -11,9 +12,8 @@
#include <QIODevice> #include <QIODevice>
#include <QMetaType> #include <QMetaType>
#include <QThread> #include <QThread>
#include <QPointer>
namespace { namespace Valgrind::XmlProtocol {
class Thread : public QThread class Thread : public QThread
{ {
@@ -21,95 +21,52 @@ public:
void run() override void run() override
{ {
QTC_ASSERT(QThread::currentThread() == this, return); QTC_ASSERT(QThread::currentThread() == this, return);
parser->parse(device); m_parser->parse(m_device);
delete parser; delete m_parser;
parser = nullptr; m_parser = nullptr;
delete device; delete m_device;
device = nullptr; m_device = nullptr;
} }
Valgrind::XmlProtocol::Parser *parser = nullptr; Valgrind::XmlProtocol::Parser *m_parser = nullptr;
QIODevice *device = nullptr; QIODevice *m_device = nullptr;
}; };
} // namespace anon
namespace Valgrind {
namespace XmlProtocol {
class ThreadedParser::Private
{
public:
QPointer<Thread> parserThread;
QString errorString;
};
ThreadedParser::ThreadedParser(QObject *parent) ThreadedParser::ThreadedParser(QObject *parent)
: QObject(parent), : QObject(parent)
d(new Private) {}
{
}
ThreadedParser::~ThreadedParser()
{
delete d;
}
QString ThreadedParser::errorString() const
{
return d->errorString;
}
bool ThreadedParser::isRunning() const bool ThreadedParser::isRunning() const
{ {
return d->parserThread ? d->parserThread.data()->isRunning() : false; return m_parserThread ? m_parserThread->isRunning() : false;
} }
void ThreadedParser::parse(QIODevice *device) void ThreadedParser::parse(QIODevice *device)
{ {
QTC_ASSERT(!d->parserThread, return); QTC_ASSERT(!m_parserThread, return);
auto parser = new Parser; auto parser = new Parser;
qRegisterMetaType<Valgrind::XmlProtocol::Status>(); qRegisterMetaType<Status>();
qRegisterMetaType<Valgrind::XmlProtocol::Error>(); qRegisterMetaType<Error>();
connect(parser, &Parser::status, connect(parser, &Parser::status, this, &ThreadedParser::status, Qt::QueuedConnection);
this, &ThreadedParser::status, connect(parser, &Parser::error, this, &ThreadedParser::error, Qt::QueuedConnection);
Qt::QueuedConnection); connect(parser, &Parser::internalError, this, &ThreadedParser::internalError,
connect(parser, &Parser::error,
this, &ThreadedParser::error,
Qt::QueuedConnection);
connect(parser, &Parser::internalError,
this, &ThreadedParser::slotInternalError,
Qt::QueuedConnection);
connect(parser, &Parser::finished,
this, &ThreadedParser::finished,
Qt::QueuedConnection); Qt::QueuedConnection);
connect(parser, &Parser::finished, this, &ThreadedParser::finished, Qt::QueuedConnection);
m_parserThread = new Thread;
auto thread = new Thread; connect(m_parserThread.get(), &QThread::finished, m_parserThread.get(), &QObject::deleteLater);
d->parserThread = thread;
connect(thread, &QThread::finished,
thread, &QObject::deleteLater);
device->setParent(nullptr); device->setParent(nullptr);
device->moveToThread(thread); device->moveToThread(m_parserThread);
parser->moveToThread(thread); parser->moveToThread(m_parserThread);
thread->device = device; m_parserThread->m_device = device;
thread->parser = parser; m_parserThread->m_parser = parser;
thread->start(); m_parserThread->start();
}
void ThreadedParser::slotInternalError(const QString &errorString)
{
d->errorString = errorString;
emit internalError(errorString);
} }
bool ThreadedParser::waitForFinished() bool ThreadedParser::waitForFinished()
{ {
return d->parserThread ? d->parserThread.data()->wait() : true; return m_parserThread ? m_parserThread->wait() : true;
} }
} // namespace XmlProtocol } // namespace Valgrind::XmlProtocol
} // namespace Valgrind

View File

@@ -4,16 +4,17 @@
#pragma once #pragma once
#include <QObject> #include <QObject>
#include <QPointer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QIODevice; class QIODevice;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Valgrind { namespace Valgrind::XmlProtocol {
namespace XmlProtocol {
class Error; class Error;
class Status; class Status;
class Thread;
/** /**
* ThreadedParser for the Valgrind Output XmlProtocol 4 * ThreadedParser for the Valgrind Output XmlProtocol 4
@@ -24,9 +25,6 @@ class ThreadedParser : public QObject
public: public:
explicit ThreadedParser(QObject *parent = nullptr); explicit ThreadedParser(QObject *parent = nullptr);
~ThreadedParser() override;
QString errorString() const;
/// interface additions relative to Parser because Parser is synchronous and this /// interface additions relative to Parser because Parser is synchronous and this
/// class parses asynchronously in a non-public secondary thread. /// class parses asynchronously in a non-public secondary thread.
@@ -36,19 +34,14 @@ public:
///@warning will move @p stream to a different thread and take ownership of it ///@warning will move @p stream to a different thread and take ownership of it
void parse(QIODevice *stream); void parse(QIODevice *stream);
private:
void slotInternalError(const QString &errorString);
signals: signals:
void status(const Valgrind::XmlProtocol::Status &status); void status(const Status &status);
void error(const Valgrind::XmlProtocol::Error &error); void error(const Error &error);
void internalError(const QString &errorString); void internalError(const QString &errorString);
void finished(); void finished();
private: private:
class Private; QPointer<Thread> m_parserThread;
Private *const d;
}; };
} // XmlProtocol } // Valgrind::XmlProtocol
} // Valgrind