Switch to C++20

PR #19336.
This commit is contained in:
Vladimir Golovnev 2023-07-21 15:38:49 +03:00 committed by GitHub
parent f27f2c20e0
commit 10ee1ab7a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 24 additions and 48 deletions

View file

@ -12,11 +12,11 @@ set(CMAKE_AUTORCC_OPTIONS --compress 9 --threshold 5)
add_library(qbt_common_cfg INTERFACE) add_library(qbt_common_cfg INTERFACE)
# Full C++ 17 support is required # Full C++ 20 support is required
# See also https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html # See also https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html
# for a breakdown of the features that CMake recognizes for each C++ standard # for a breakdown of the features that CMake recognizes for each C++ standard
target_compile_features(qbt_common_cfg INTERFACE target_compile_features(qbt_common_cfg INTERFACE
cxx_std_17 cxx_std_20
) )
target_compile_definitions(qbt_common_cfg INTERFACE target_compile_definitions(qbt_common_cfg INTERFACE

View file

@ -464,8 +464,7 @@ void Application::processMessage(const QString &message)
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
if (message.isEmpty()) if (message.isEmpty())
{ {
// TODO: use [[likely]] in C++20 if (BitTorrent::Session::instance()->isRestored()) [[likely]]
if (Q_LIKELY(BitTorrent::Session::instance()->isRestored()))
{ {
m_window->activate(); // show UI m_window->activate(); // show UI
} }

View file

@ -118,7 +118,7 @@ void CustomDiskIOThread::async_move_storage(lt::storage_index_t storage, std::st
handleCompleteFiles(storage, newSavePath); handleCompleteFiles(storage, newSavePath);
m_nativeDiskIO->async_move_storage(storage, path, flags m_nativeDiskIO->async_move_storage(storage, path, flags
, [=, handler = std::move(handler)](lt::status_t status, const std::string &path, const lt::storage_error &error) , [=, this, handler = std::move(handler)](lt::status_t status, const std::string &path, const lt::storage_error &error)
{ {
#if LIBTORRENT_VERSION_NUM < 20100 #if LIBTORRENT_VERSION_NUM < 20100
if ((status != lt::status_t::fatal_disk_error) && (status != lt::status_t::file_exist)) if ((status != lt::status_t::fatal_disk_error) && (status != lt::status_t::file_exist))
@ -153,7 +153,7 @@ void CustomDiskIOThread::async_rename_file(lt::storage_index_t storage, lt::file
, std::function<void (const std::string &, lt::file_index_t, const lt::storage_error &)> handler) , std::function<void (const std::string &, lt::file_index_t, const lt::storage_error &)> handler)
{ {
m_nativeDiskIO->async_rename_file(storage, index, name m_nativeDiskIO->async_rename_file(storage, index, name
, [=, handler = std::move(handler)](const std::string &name, lt::file_index_t index, const lt::storage_error &error) , [=, this, handler = std::move(handler)](const std::string &name, lt::file_index_t index, const lt::storage_error &error)
{ {
if (!error) if (!error)
m_storageData[storage].files.rename_file(index, name); m_storageData[storage].files.rename_file(index, name);
@ -171,7 +171,7 @@ void CustomDiskIOThread::async_set_file_priority(lt::storage_index_t storage, lt
, std::function<void (const lt::storage_error &, lt::aux::vector<lt::download_priority_t, lt::file_index_t>)> handler) , std::function<void (const lt::storage_error &, lt::aux::vector<lt::download_priority_t, lt::file_index_t>)> handler)
{ {
m_nativeDiskIO->async_set_file_priority(storage, std::move(priorities) m_nativeDiskIO->async_set_file_priority(storage, std::move(priorities)
, [=, handler = std::move(handler)](const lt::storage_error &error, const lt::aux::vector<lt::download_priority_t, lt::file_index_t> &priorities) , [=, this, handler = std::move(handler)](const lt::storage_error &error, const lt::aux::vector<lt::download_priority_t, lt::file_index_t> &priorities)
{ {
m_storageData[storage].filePriorities = priorities; m_storageData[storage].filePriorities = priorities;
handler(error, priorities); handler(error, priorities);

View file

@ -2904,7 +2904,7 @@ void SessionImpl::findIncompleteFiles(const TorrentInfo &torrentInfo, const Path
const auto searchId = TorrentID::fromInfoHash(torrentInfo.infoHash()); const auto searchId = TorrentID::fromInfoHash(torrentInfo.infoHash());
const PathList originalFileNames = (filePaths.isEmpty() ? torrentInfo.filePaths() : filePaths); const PathList originalFileNames = (filePaths.isEmpty() ? torrentInfo.filePaths() : filePaths);
QMetaObject::invokeMethod(m_fileSearcher, [=]() QMetaObject::invokeMethod(m_fileSearcher, [=, this]
{ {
m_fileSearcher->search(searchId, originalFileNames, savePath, downloadPath, isAppendExtensionEnabled()); m_fileSearcher->search(searchId, originalFileNames, savePath, downloadPath, isAppendExtensionEnabled());
}); });

View file

@ -1217,7 +1217,7 @@ QVector<qreal> TorrentImpl::filesProgress() const
const int count = m_filesProgress.size(); const int count = m_filesProgress.size();
Q_ASSERT(count == filesCount()); Q_ASSERT(count == filesCount());
if (Q_UNLIKELY(count != filesCount())) if (count != filesCount()) [[unlikely]]
return {}; return {};
if (m_completedFiles.count(true) == count) if (m_completedFiles.count(true) == count)
@ -1613,8 +1613,7 @@ TrackerEntry TorrentImpl::updateTrackerEntry(const lt::announce_entry &announceE
}); });
Q_ASSERT(it != m_trackerEntries.end()); Q_ASSERT(it != m_trackerEntries.end());
// TODO: use [[unlikely]] in C++20 if (it == m_trackerEntries.end()) [[unlikely]]
if (Q_UNLIKELY(it == m_trackerEntries.end()))
return {}; return {};
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
@ -1813,7 +1812,7 @@ void TorrentImpl::moveStorage(const Path &newPath, const MoveStorageContext cont
void TorrentImpl::renameFile(const int index, const Path &path) void TorrentImpl::renameFile(const int index, const Path &path)
{ {
Q_ASSERT((index >= 0) && (index < filesCount())); Q_ASSERT((index >= 0) && (index < filesCount()));
if (Q_UNLIKELY((index < 0) || (index >= filesCount()))) if ((index < 0) || (index >= filesCount())) [[unlikely]]
return; return;
const Path wantedPath = wantedActualPath(index, path); const Path wantedPath = wantedActualPath(index, path);
@ -2276,7 +2275,7 @@ void TorrentImpl::doRenameFile(int index, const Path &path)
Q_ASSERT(index >= 0); Q_ASSERT(index >= 0);
Q_ASSERT(index < nativeIndexes.size()); Q_ASSERT(index < nativeIndexes.size());
if (Q_UNLIKELY((index < 0) || (index >= nativeIndexes.size()))) if ((index < 0) || (index >= nativeIndexes.size())) [[unlikely]]
return; return;
++m_renameCount; ++m_renameCount;
@ -2364,11 +2363,11 @@ void TorrentImpl::updateStatus(const lt::torrent_status &nativeStatus)
void TorrentImpl::updateProgress() void TorrentImpl::updateProgress()
{ {
Q_ASSERT(hasMetadata()); Q_ASSERT(hasMetadata());
if (Q_UNLIKELY(!hasMetadata())) if (!hasMetadata()) [[unlikely]]
return; return;
Q_ASSERT(!m_filesProgress.isEmpty()); Q_ASSERT(!m_filesProgress.isEmpty());
if (Q_UNLIKELY(m_filesProgress.isEmpty())) if (m_filesProgress.isEmpty()) [[unlikely]]
m_filesProgress.resize(filesCount()); m_filesProgress.resize(filesCount());
const QBitArray oldPieces = std::exchange(m_pieces, LT::toQBitArray(m_nativeStatus.pieces)); const QBitArray oldPieces = std::exchange(m_pieces, LT::toQBitArray(m_nativeStatus.pieces));

View file

@ -74,12 +74,12 @@ void Connection::read()
const qint64 bytesAvailable = m_socket->bytesAvailable(); const qint64 bytesAvailable = m_socket->bytesAvailable();
m_receivedData.resize(previousSize + bytesAvailable); m_receivedData.resize(previousSize + bytesAvailable);
const qint64 bytesRead = m_socket->read((m_receivedData.data() + previousSize), bytesAvailable); const qint64 bytesRead = m_socket->read((m_receivedData.data() + previousSize), bytesAvailable);
if (Q_UNLIKELY(bytesRead < 0)) if (bytesRead < 0) [[unlikely]]
{ {
m_socket->close(); m_socket->close();
return; return;
} }
if (Q_UNLIKELY(bytesRead < bytesAvailable)) if (bytesRead < bytesAvailable) [[unlikely]]
m_receivedData.chop(bytesAvailable - bytesRead); m_receivedData.chop(bytesAvailable - bytesRead);
while (!m_receivedData.isEmpty()) while (!m_receivedData.isEmpty())

View file

@ -51,13 +51,6 @@ public:
// The following are custom functions that are in line with Qt API interface, such as `QSet` // The following are custom functions that are in line with Qt API interface, such as `QSet`
#if __cplusplus < 202002L
bool contains(const key_type &value) const
{
return (BaseType::find(value) != BaseType::cend());
}
#endif
int count() const int count() const
{ {
return static_cast<int>(BaseType::size()); return static_cast<int>(BaseType::size());

View file

@ -524,8 +524,7 @@ void Feed::handleArticleLoadFinished(QVector<QVariantHash> articles)
for (const QVariantHash &articleData : articles) for (const QVariantHash &articleData : articles)
{ {
const auto articleID = articleData.value(Article::KeyId).toString(); const auto articleID = articleData.value(Article::KeyId).toString();
// TODO: use [[unlikely]] in C++20 if (m_articles.contains(articleID)) [[unlikely]]
if (Q_UNLIKELY(m_articles.contains(articleID)))
continue; continue;
auto *article = new Article(this, articleData); auto *article = new Article(this, articleData);

View file

@ -898,7 +898,7 @@ void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, co
void AddNewTorrentDialog::setupTreeview() void AddNewTorrentDialog::setupTreeview()
{ {
Q_ASSERT(hasMetadata()); Q_ASSERT(hasMetadata());
if (Q_UNLIKELY(!hasMetadata())) if (!hasMetadata()) [[unlikely]]
return; return;
// Set dialog title // Set dialog title

View file

@ -434,7 +434,7 @@ void RSSWidget::editSelectedRSSFeedURL()
QTreeWidgetItem *item = selectedItems.first(); QTreeWidgetItem *item = selectedItems.first();
RSS::Feed *rssFeed = qobject_cast<RSS::Feed *>(m_feedListWidget->getRSSItem(item)); RSS::Feed *rssFeed = qobject_cast<RSS::Feed *>(m_feedListWidget->getRSSItem(item));
Q_ASSERT(rssFeed); Q_ASSERT(rssFeed);
if (Q_UNLIKELY(!rssFeed)) if (!rssFeed) [[unlikely]]
return; return;
bool ok = false; bool ok = false;

View file

@ -212,7 +212,7 @@ void TorrentContentModel::updateFilesProgress()
const QVector<qreal> &filesProgress = m_contentHandler->filesProgress(); const QVector<qreal> &filesProgress = m_contentHandler->filesProgress();
Q_ASSERT(m_filesIndex.size() == filesProgress.size()); Q_ASSERT(m_filesIndex.size() == filesProgress.size());
// XXX: Why is this necessary? // XXX: Why is this necessary?
if (Q_UNLIKELY(m_filesIndex.size() != filesProgress.size())) if (m_filesIndex.size() != filesProgress.size()) [[unlikely]]
return; return;
for (int i = 0; i < filesProgress.size(); ++i) for (int i = 0; i < filesProgress.size(); ++i)
@ -248,7 +248,7 @@ void TorrentContentModel::updateFilesAvailability()
Q_ASSERT(m_filesIndex.size() == availableFileFractions.size()); Q_ASSERT(m_filesIndex.size() == availableFileFractions.size());
// XXX: Why is this necessary? // XXX: Why is this necessary?
if (Q_UNLIKELY(m_filesIndex.size() != availableFileFractions.size())) if (m_filesIndex.size() != availableFileFractions.size()) [[unlikely]]
return; return;
for (int i = 0; i < m_filesIndex.size(); ++i) for (int i = 0; i < m_filesIndex.size(); ++i)

View file

@ -454,7 +454,7 @@ void TorrentContentWidget::onItemDoubleClicked(const QModelIndex &index)
const auto *contentHandler = m_model->contentHandler(); const auto *contentHandler = m_model->contentHandler();
Q_ASSERT(contentHandler && contentHandler->hasMetadata()); Q_ASSERT(contentHandler && contentHandler->hasMetadata());
if (Q_UNLIKELY(!contentHandler || !contentHandler->hasMetadata())) if (!contentHandler || !contentHandler->hasMetadata()) [[unlikely]]
return; return;
if (m_doubleClickAction == DoubleClickAction::Rename) if (m_doubleClickAction == DoubleClickAction::Rename)

View file

@ -392,7 +392,7 @@ void TrackersFilterWidget::handleFavicoDownloadFinished(const Net::DownloadResul
{ {
const QSet<QString> trackerHosts = m_downloadingFavicons.take(result.url); const QSet<QString> trackerHosts = m_downloadingFavicons.take(result.url);
Q_ASSERT(!trackerHosts.isEmpty()); Q_ASSERT(!trackerHosts.isEmpty());
if (Q_UNLIKELY(trackerHosts.isEmpty())) if (trackerHosts.isEmpty()) [[unlikely]]
return; return;
QIcon icon; QIcon icon;
@ -439,7 +439,7 @@ void TrackersFilterWidget::handleFavicoDownloadFinished(const Net::DownloadResul
QListWidgetItem *trackerItem = item(rowFromTracker(trackerHost)); QListWidgetItem *trackerItem = item(rowFromTracker(trackerHost));
Q_ASSERT(trackerItem); Q_ASSERT(trackerItem);
if (Q_UNLIKELY(!trackerItem)) if (!trackerItem) [[unlikely]]
continue; continue;
trackerItem->setData(Qt::DecorationRole, icon); trackerItem->setData(Qt::DecorationRole, icon);

View file

@ -880,7 +880,7 @@ void SyncController::onTorrentAboutToBeRemoved(BitTorrent::Torrent *torrent)
{ {
auto iter = m_knownTrackers.find(trackerEntry.url); auto iter = m_knownTrackers.find(trackerEntry.url);
Q_ASSERT(iter != m_knownTrackers.end()); Q_ASSERT(iter != m_knownTrackers.end());
if (Q_UNLIKELY(iter == m_knownTrackers.end())) if (iter == m_knownTrackers.end()) [[unlikely]]
continue; continue;
QSet<BitTorrent::TorrentID> &torrentIDs = iter.value(); QSet<BitTorrent::TorrentID> &torrentIDs = iter.value();

View file

@ -42,20 +42,6 @@ public:
TestOrderedSet() = default; TestOrderedSet() = default;
private slots: private slots:
#if __cplusplus < 202002L
void testContains() const
{
const OrderedSet<QString> set {u"a"_s, u"b"_s, u"c"_s};
QVERIFY(set.contains(u"a"_s));
QVERIFY(set.contains(u"b"_s));
QVERIFY(set.contains(u"c"_s));
QVERIFY(!set.contains(u"z"_s));
const OrderedSet<QString> emptySet;
QVERIFY(!emptySet.contains(u"a"_s));
}
#endif
void testCount() const void testCount() const
{ {
const OrderedSet<QString> set {u"a"_s, u"b"_s, u"c"_s, u"c"_s}; const OrderedSet<QString> set {u"a"_s, u"b"_s, u"c"_s, u"c"_s};