forked from qt-creator/qt-creator
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:
@@ -2,8 +2,9 @@
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "threadedparser.h"
|
||||
#include "parser.h"
|
||||
|
||||
#include "error.h"
|
||||
#include "parser.h"
|
||||
#include "status.h"
|
||||
|
||||
#include <utils/qtcassert.h>
|
||||
@@ -11,9 +12,8 @@
|
||||
#include <QIODevice>
|
||||
#include <QMetaType>
|
||||
#include <QThread>
|
||||
#include <QPointer>
|
||||
|
||||
namespace {
|
||||
namespace Valgrind::XmlProtocol {
|
||||
|
||||
class Thread : public QThread
|
||||
{
|
||||
@@ -21,95 +21,52 @@ public:
|
||||
void run() override
|
||||
{
|
||||
QTC_ASSERT(QThread::currentThread() == this, return);
|
||||
parser->parse(device);
|
||||
delete parser;
|
||||
parser = nullptr;
|
||||
delete device;
|
||||
device = nullptr;
|
||||
m_parser->parse(m_device);
|
||||
delete m_parser;
|
||||
m_parser = nullptr;
|
||||
delete m_device;
|
||||
m_device = nullptr;
|
||||
}
|
||||
|
||||
Valgrind::XmlProtocol::Parser *parser = nullptr;
|
||||
QIODevice *device = nullptr;
|
||||
Valgrind::XmlProtocol::Parser *m_parser = nullptr;
|
||||
QIODevice *m_device = nullptr;
|
||||
};
|
||||
|
||||
} // namespace anon
|
||||
|
||||
|
||||
namespace Valgrind {
|
||||
namespace XmlProtocol {
|
||||
|
||||
class ThreadedParser::Private
|
||||
{
|
||||
public:
|
||||
QPointer<Thread> parserThread;
|
||||
QString errorString;
|
||||
};
|
||||
|
||||
|
||||
ThreadedParser::ThreadedParser(QObject *parent)
|
||||
: QObject(parent),
|
||||
d(new Private)
|
||||
{
|
||||
}
|
||||
|
||||
ThreadedParser::~ThreadedParser()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
QString ThreadedParser::errorString() const
|
||||
{
|
||||
return d->errorString;
|
||||
}
|
||||
: QObject(parent)
|
||||
{}
|
||||
|
||||
bool ThreadedParser::isRunning() const
|
||||
{
|
||||
return d->parserThread ? d->parserThread.data()->isRunning() : false;
|
||||
return m_parserThread ? m_parserThread->isRunning() : false;
|
||||
}
|
||||
|
||||
void ThreadedParser::parse(QIODevice *device)
|
||||
{
|
||||
QTC_ASSERT(!d->parserThread, return);
|
||||
QTC_ASSERT(!m_parserThread, return);
|
||||
|
||||
auto parser = new Parser;
|
||||
qRegisterMetaType<Valgrind::XmlProtocol::Status>();
|
||||
qRegisterMetaType<Valgrind::XmlProtocol::Error>();
|
||||
connect(parser, &Parser::status,
|
||||
this, &ThreadedParser::status,
|
||||
Qt::QueuedConnection);
|
||||
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,
|
||||
qRegisterMetaType<Status>();
|
||||
qRegisterMetaType<Error>();
|
||||
connect(parser, &Parser::status, this, &ThreadedParser::status, Qt::QueuedConnection);
|
||||
connect(parser, &Parser::error, this, &ThreadedParser::error, Qt::QueuedConnection);
|
||||
connect(parser, &Parser::internalError, this, &ThreadedParser::internalError,
|
||||
Qt::QueuedConnection);
|
||||
connect(parser, &Parser::finished, this, &ThreadedParser::finished, Qt::QueuedConnection);
|
||||
|
||||
|
||||
auto thread = new Thread;
|
||||
d->parserThread = thread;
|
||||
connect(thread, &QThread::finished,
|
||||
thread, &QObject::deleteLater);
|
||||
m_parserThread = new Thread;
|
||||
connect(m_parserThread.get(), &QThread::finished, m_parserThread.get(), &QObject::deleteLater);
|
||||
device->setParent(nullptr);
|
||||
device->moveToThread(thread);
|
||||
parser->moveToThread(thread);
|
||||
thread->device = device;
|
||||
thread->parser = parser;
|
||||
thread->start();
|
||||
}
|
||||
|
||||
void ThreadedParser::slotInternalError(const QString &errorString)
|
||||
{
|
||||
d->errorString = errorString;
|
||||
emit internalError(errorString);
|
||||
device->moveToThread(m_parserThread);
|
||||
parser->moveToThread(m_parserThread);
|
||||
m_parserThread->m_device = device;
|
||||
m_parserThread->m_parser = parser;
|
||||
m_parserThread->start();
|
||||
}
|
||||
|
||||
bool ThreadedParser::waitForFinished()
|
||||
{
|
||||
return d->parserThread ? d->parserThread.data()->wait() : true;
|
||||
return m_parserThread ? m_parserThread->wait() : true;
|
||||
}
|
||||
|
||||
} // namespace XmlProtocol
|
||||
} // namespace Valgrind
|
||||
} // namespace Valgrind::XmlProtocol
|
||||
|
||||
@@ -4,16 +4,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QIODevice;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Valgrind {
|
||||
namespace XmlProtocol {
|
||||
namespace Valgrind::XmlProtocol {
|
||||
|
||||
class Error;
|
||||
class Status;
|
||||
class Thread;
|
||||
|
||||
/**
|
||||
* ThreadedParser for the Valgrind Output XmlProtocol 4
|
||||
@@ -24,9 +25,6 @@ class ThreadedParser : public QObject
|
||||
|
||||
public:
|
||||
explicit ThreadedParser(QObject *parent = nullptr);
|
||||
~ThreadedParser() override;
|
||||
|
||||
QString errorString() const;
|
||||
|
||||
/// interface additions relative to Parser because Parser is synchronous and this
|
||||
/// 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
|
||||
void parse(QIODevice *stream);
|
||||
|
||||
private:
|
||||
void slotInternalError(const QString &errorString);
|
||||
|
||||
signals:
|
||||
void status(const Valgrind::XmlProtocol::Status &status);
|
||||
void error(const Valgrind::XmlProtocol::Error &error);
|
||||
void status(const Status &status);
|
||||
void error(const Error &error);
|
||||
void internalError(const QString &errorString);
|
||||
void finished();
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *const d;
|
||||
QPointer<Thread> m_parserThread;
|
||||
};
|
||||
|
||||
} // XmlProtocol
|
||||
} // Valgrind
|
||||
} // Valgrind::XmlProtocol
|
||||
|
||||
Reference in New Issue
Block a user