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)
# 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
# for a breakdown of the features that CMake recognizes for each C++ standard
target_compile_features(qbt_common_cfg INTERFACE
cxx_std_17
cxx_std_20
)
target_compile_definitions(qbt_common_cfg INTERFACE

View file

@ -464,8 +464,7 @@ void Application::processMessage(const QString &message)
#ifndef DISABLE_GUI
if (message.isEmpty())
{
// TODO: use [[likely]] in C++20
if (Q_LIKELY(BitTorrent::Session::instance()->isRestored()))
if (BitTorrent::Session::instance()->isRestored()) [[likely]]
{
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);
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 ((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)
{
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)
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)
{
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;
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 PathList originalFileNames = (filePaths.isEmpty() ? torrentInfo.filePaths() : filePaths);
QMetaObject::invokeMethod(m_fileSearcher, [=]()
QMetaObject::invokeMethod(m_fileSearcher, [=, this]
{
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();
Q_ASSERT(count == filesCount());
if (Q_UNLIKELY(count != filesCount()))
if (count != filesCount()) [[unlikely]]
return {};
if (m_completedFiles.count(true) == count)
@ -1613,8 +1613,7 @@ TrackerEntry TorrentImpl::updateTrackerEntry(const lt::announce_entry &announceE
});
Q_ASSERT(it != m_trackerEntries.end());
// TODO: use [[unlikely]] in C++20
if (Q_UNLIKELY(it == m_trackerEntries.end()))
if (it == m_trackerEntries.end()) [[unlikely]]
return {};
#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)
{
Q_ASSERT((index >= 0) && (index < filesCount()));
if (Q_UNLIKELY((index < 0) || (index >= filesCount())))
if ((index < 0) || (index >= filesCount())) [[unlikely]]
return;
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 < nativeIndexes.size());
if (Q_UNLIKELY((index < 0) || (index >= nativeIndexes.size())))
if ((index < 0) || (index >= nativeIndexes.size())) [[unlikely]]
return;
++m_renameCount;
@ -2364,11 +2363,11 @@ void TorrentImpl::updateStatus(const lt::torrent_status &nativeStatus)
void TorrentImpl::updateProgress()
{
Q_ASSERT(hasMetadata());
if (Q_UNLIKELY(!hasMetadata()))
if (!hasMetadata()) [[unlikely]]
return;
Q_ASSERT(!m_filesProgress.isEmpty());
if (Q_UNLIKELY(m_filesProgress.isEmpty()))
if (m_filesProgress.isEmpty()) [[unlikely]]
m_filesProgress.resize(filesCount());
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();
m_receivedData.resize(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();
return;
}
if (Q_UNLIKELY(bytesRead < bytesAvailable))
if (bytesRead < bytesAvailable) [[unlikely]]
m_receivedData.chop(bytesAvailable - bytesRead);
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`
#if __cplusplus < 202002L
bool contains(const key_type &value) const
{
return (BaseType::find(value) != BaseType::cend());
}
#endif
int count() const
{
return static_cast<int>(BaseType::size());

View file

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

View file

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

View file

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

View file

@ -212,7 +212,7 @@ void TorrentContentModel::updateFilesProgress()
const QVector<qreal> &filesProgress = m_contentHandler->filesProgress();
Q_ASSERT(m_filesIndex.size() == filesProgress.size());
// XXX: Why is this necessary?
if (Q_UNLIKELY(m_filesIndex.size() != filesProgress.size()))
if (m_filesIndex.size() != filesProgress.size()) [[unlikely]]
return;
for (int i = 0; i < filesProgress.size(); ++i)
@ -248,7 +248,7 @@ void TorrentContentModel::updateFilesAvailability()
Q_ASSERT(m_filesIndex.size() == availableFileFractions.size());
// XXX: Why is this necessary?
if (Q_UNLIKELY(m_filesIndex.size() != availableFileFractions.size()))
if (m_filesIndex.size() != availableFileFractions.size()) [[unlikely]]
return;
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();
Q_ASSERT(contentHandler && contentHandler->hasMetadata());
if (Q_UNLIKELY(!contentHandler || !contentHandler->hasMetadata()))
if (!contentHandler || !contentHandler->hasMetadata()) [[unlikely]]
return;
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);
Q_ASSERT(!trackerHosts.isEmpty());
if (Q_UNLIKELY(trackerHosts.isEmpty()))
if (trackerHosts.isEmpty()) [[unlikely]]
return;
QIcon icon;
@ -439,7 +439,7 @@ void TrackersFilterWidget::handleFavicoDownloadFinished(const Net::DownloadResul
QListWidgetItem *trackerItem = item(rowFromTracker(trackerHost));
Q_ASSERT(trackerItem);
if (Q_UNLIKELY(!trackerItem))
if (!trackerItem) [[unlikely]]
continue;
trackerItem->setData(Qt::DecorationRole, icon);

View file

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

View file

@ -42,20 +42,6 @@ public:
TestOrderedSet() = default;
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
{
const OrderedSet<QString> set {u"a"_s, u"b"_s, u"c"_s, u"c"_s};