From 93dfa93b7dd6d338539927a43b52f5d5e96b03e4 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 7 Oct 2022 16:16:35 +0200 Subject: [PATCH] VcsCommand: Simplify ProgressParser Get rid of abstract base ProgressParser and replace it with ProgressParser function. The only one former subclass GitProgressParser is now functor object. Pass future interface directly to the ProgressParser function, along with input text to be parsed. Change-Id: Icbcf0b6e55097f8b38eb8a32ceaa4414723116d0 Reviewed-by: Orgad Shaneh --- src/plugins/git/gitclient.cpp | 25 ++++++----------- src/plugins/vcsbase/vcscommand.cpp | 45 ++++-------------------------- src/plugins/vcsbase/vcscommand.h | 21 ++------------ 3 files changed, 16 insertions(+), 75 deletions(-) diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index 002d603a431..fdc1c9a6251 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -791,26 +791,19 @@ private: QStringList m_files; }; -class GitProgressParser : public ProgressParser +class GitProgressParser { public: - static void attachToCommand(VcsCommand *command) - { - command->setProgressParser(new GitProgressParser); + void operator()(QFutureInterface &fi, const QString &inputText) const { + const QRegularExpressionMatch match = m_progressExp.match(inputText); + if (match.hasMatch()) { + fi.setProgressRange(0, match.captured(2).toInt()); + fi.setProgressValue(match.captured(1).toInt()); + } } private: - GitProgressParser() : m_progressExp("\\((\\d+)/(\\d+)\\)") // e.g. Rebasing (7/42) - { } - - void parseProgress(const QString &text) override - { - const QRegularExpressionMatch match = m_progressExp.match(text); - if (match.hasMatch()) - setProgressAndMaximum(match.captured(1).toInt(), match.captured(2).toInt()); - } - - const QRegularExpression m_progressExp; + const QRegularExpression m_progressExp{"\\((\\d+)/(\\d+)\\)"}; // e.g. Rebasing (7/42) }; static inline QString msgRepositoryNotFound(const FilePath &dir) @@ -3404,7 +3397,7 @@ VcsCommand *GitClient::vcsExecAbortable(const FilePath &workingDirectory, command->addJob({vcsBinary(), arguments}, isRebase ? 0 : vcsTimeoutS()); ConflictHandler::attachToCommand(command, workingDirectory, abortCommand); if (isRebase) - GitProgressParser::attachToCommand(command); + command->setProgressParser(GitProgressParser()); command->start(); return command; diff --git a/src/plugins/vcsbase/vcscommand.cpp b/src/plugins/vcsbase/vcscommand.cpp index b7097c873dc..39519e1a838 100644 --- a/src/plugins/vcsbase/vcscommand.cpp +++ b/src/plugins/vcsbase/vcscommand.cpp @@ -13,16 +13,12 @@ #include #include #include -#include +#include #include #include -#include #include #include -#include - -#include /*! \fn void Utils::ProgressParser::parseProgress(const QString &text) @@ -67,8 +63,6 @@ public: m_futureInterface.setProgressRange(0, 1); } - ~VcsCommandPrivate() { delete m_progressParser; } - Environment environment() { if (!(m_flags & RunFlags::ForceCLocale)) @@ -98,7 +92,7 @@ public: const FilePath m_defaultWorkingDirectory; Environment m_environment; QTextCodec *m_codec = nullptr; - ProgressParser *m_progressParser = nullptr; + ProgressParser m_progressParser = {}; QFutureWatcher m_watcher; QList m_jobs; @@ -143,8 +137,6 @@ void VcsCommandPrivate::setup() GlobalFileChangeBlocker::instance()->forceBlocked(true); }); } - if (m_progressParser) - m_progressParser->setFuture(&m_futureInterface); } void VcsCommandPrivate::cleanup() @@ -156,8 +148,6 @@ void VcsCommandPrivate::cleanup() GlobalFileChangeBlocker::instance()->forceBlocked(false); }); } - if (m_progressParser) - m_progressParser->setFuture(nullptr); } void VcsCommandPrivate::setupProcess(QtcProcess *process, const Job &job) @@ -186,7 +176,7 @@ void VcsCommandPrivate::installStdCallbacks(QtcProcess *process) || !(m_flags & RunFlags::SuppressStdErr))) { process->setStdErrCallback([this](const QString &text) { if (m_progressParser) - m_progressParser->parseProgress(text); + m_progressParser(m_futureInterface, text); if (!(m_flags & RunFlags::SuppressStdErr)) emit q->appendError(text); if (m_flags & RunFlags::ProgressiveOutput) @@ -198,7 +188,7 @@ void VcsCommandPrivate::installStdCallbacks(QtcProcess *process) || m_flags & RunFlags::ShowStdOut) { process->setStdOutCallback([this](const QString &text) { if (m_progressParser) - m_progressParser->parseProgress(text); + m_progressParser(m_futureInterface, text); if (m_flags & RunFlags::ShowStdOut) { if (m_flags & RunFlags::SilentOutput) emit q->appendSilently(text); @@ -415,36 +405,11 @@ void VcsCommand::setCodec(QTextCodec *codec) } //! Use \a parser to parse progress data from stdout. Command takes ownership of \a parser -void VcsCommand::setProgressParser(ProgressParser *parser) +void VcsCommand::setProgressParser(const ProgressParser &parser) { - QTC_ASSERT(!d->m_progressParser, return); d->m_progressParser = parser; } -ProgressParser::ProgressParser() : - m_futureMutex(new QMutex) -{ } - -ProgressParser::~ProgressParser() -{ - delete m_futureMutex; -} - -void ProgressParser::setProgressAndMaximum(int value, int maximum) -{ - QMutexLocker lock(m_futureMutex); - if (!m_future) - return; - m_future->setProgressRange(0, maximum); - m_future->setProgressValue(value); -} - -void ProgressParser::setFuture(QFutureInterface *future) -{ - QMutexLocker lock(m_futureMutex); - m_future = future; -} - CommandResult::CommandResult(const QtcProcess &process) : m_result(process.result()) , m_exitCode(process.exitCode()) diff --git a/src/plugins/vcsbase/vcscommand.h b/src/plugins/vcsbase/vcscommand.h index f26cf53f963..a948519dade 100644 --- a/src/plugins/vcsbase/vcscommand.h +++ b/src/plugins/vcsbase/vcscommand.h @@ -12,7 +12,6 @@ #include QT_BEGIN_NAMESPACE -class QMutex; class QVariant; template class QFuture; @@ -31,23 +30,7 @@ namespace VcsBase { namespace Internal { class VcsCommandPrivate; } -class VCSBASE_EXPORT ProgressParser -{ -public: - ProgressParser(); - virtual ~ProgressParser(); - -protected: - virtual void parseProgress(const QString &text) = 0; - void setProgressAndMaximum(int value, int maximum); - -private: - void setFuture(QFutureInterface *future); - - QFutureInterface *m_future; - QMutex *m_futureMutex = nullptr; - friend class Internal::VcsCommandPrivate; -}; +using ProgressParser = std::function &, const QString &)>; class VCSBASE_EXPORT CommandResult { @@ -96,7 +79,7 @@ public: void setCodec(QTextCodec *codec); - void setProgressParser(ProgressParser *parser); + void setProgressParser(const ProgressParser &parser); static CommandResult runBlocking(const Utils::FilePath &workingDirectory, const Utils::Environment &environmentconst,