From a52f14634b339ec2c363e6499af1d06dd85f2be2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 28 Dec 2022 07:27:03 +0100 Subject: Assistant: port HelpEngineWrapper from QSharedPointer to unique_ptr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only reason why the code used a shared instead of a unique ptr, apart from possibly predating C++11, is that the container in which the pointer is held, QMap, doesn't support move-only types. Now that QMap is but a wrapper around std::map, use the Real Thing™, and gain move-only type support for free. Replace a QPair with a proper struct with named members. Change-Id: Ic8d5529c5b03fa570553c8f81d7f9a721a167246 Reviewed-by: Jörg Bornemann (cherry picked from commit 2af5aa043da14b4b6b0d064cc59e6bfbe18231f5) Reviewed-by: Qt Cherry-pick Bot --- src/assistant/assistant/helpenginewrapper.cpp | 29 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/assistant/assistant/helpenginewrapper.cpp b/src/assistant/assistant/helpenginewrapper.cpp index 9565680d8..b08bb213e 100644 --- a/src/assistant/assistant/helpenginewrapper.cpp +++ b/src/assistant/assistant/helpenginewrapper.cpp @@ -8,8 +8,6 @@ #include #include #include -#include -#include #include #include #include @@ -18,6 +16,9 @@ #include #include +#include +#include + QT_BEGIN_NAMESPACE namespace { @@ -75,8 +76,11 @@ private: QHelpEngine * const m_helpEngine; QFileSystemWatcher * const m_qchWatcher; - typedef QPair > RecentSignal; - QMap m_recentQchUpdates; + struct RecentSignal { + QDateTime timestamp; + std::unique_ptr forwarder; + }; + std::map m_recentQchUpdates; }; HelpEngineWrapper *HelpEngineWrapper::helpEngineWrapper = nullptr; @@ -727,7 +731,7 @@ void HelpEngineWrapperPrivate::qchFileChanged(const QString &fileName, * signal more than once. */ if (ns.isEmpty()) { - m_recentQchUpdates.remove(fileName); + m_recentQchUpdates.erase(fileName); return; } @@ -742,19 +746,22 @@ void HelpEngineWrapperPrivate::qchFileChanged(const QString &fileName, // Case 1: This is the first recent signal for the file. if (it == m_recentQchUpdates.end()) { - QSharedPointer forwarder(new TimeoutForwarder(fileName)); - m_recentQchUpdates.insert(fileName, RecentSignal(now, forwarder)); - QTimer::singleShot(UpdateGracePeriod, forwarder.data(), + auto forwarder = std::make_unique(fileName); + QTimer::singleShot(UpdateGracePeriod, forwarder.get(), &TimeoutForwarder::forward); + m_recentQchUpdates.emplace(fileName, + RecentSignal{std::move(now), std::move(forwarder)}); return; } + auto &[key, entry] = *it; + // Case 2: The last signal for this file has not expired yet. - if (it.value().first > now.addMSecs(-UpdateGracePeriod)) { + if (entry.timestamp > now.addMSecs(-UpdateGracePeriod)) { if (!fromTimeout) - it.value().first = now; + entry.timestamp = now; else - QTimer::singleShot(UpdateGracePeriod, it.value().second.data(), + QTimer::singleShot(UpdateGracePeriod, entry.forwarder.get(), &TimeoutForwarder::forward); return; } -- cgit v1.2.1