refactor(qt): use spaceship operator (#8704)

This commit is contained in:
Yat Ho
2026-03-23 22:13:30 +08:00
committed by GitHub
parent e87cc06d68
commit 7af380dd5a
3 changed files with 51 additions and 50 deletions

View File

@@ -9,12 +9,9 @@
#include <QApplication>
#include <QString>
#include <QUrl>
#include <libtransmission/transmission.h>
#include <libtransmission/quark.h>
#include <libtransmission/utils.h>
#include <libtransmission/variant.h>
#include "Application.h"
#include "IconCache.h"
@@ -57,7 +54,7 @@ bool Torrent::includesTracker(QString const& sitename) const
return std::ranges::binary_search(sitenames_, sitename);
}
int Torrent::compareSeedProgress(Torrent const& that) const
std::partial_ordering Torrent::compareSeedProgress(Torrent const& that) const
{
auto const a_ratio_limit = getSeedRatioLimit();
auto const b_ratio_limit = that.getSeedRatioLimit();
@@ -72,12 +69,12 @@ int Torrent::compareSeedProgress(Torrent const& that) const
if (!a_ratio_limit)
{
return b_ratio < *b_ratio_limit ? 1 : -1;
return b_ratio < *b_ratio_limit ? std::partial_ordering::greater : std::partial_ordering::less;
}
if (!b_ratio_limit)
{
return a_ratio < *a_ratio_limit ? -1 : 1;
return a_ratio < *a_ratio_limit ? std::partial_ordering::less : std::partial_ordering::greater;
}
if (!(*a_ratio_limit > 0) && !(*b_ratio_limit > 0))
@@ -87,63 +84,63 @@ int Torrent::compareSeedProgress(Torrent const& that) const
if (!(*a_ratio_limit > 0))
{
return 1;
return std::partial_ordering::greater;
}
if (!(*b_ratio_limit > 0))
{
return -1;
return std::partial_ordering::less;
}
double const a_progress = a_ratio / *a_ratio_limit;
double const b_progress = b_ratio / *b_ratio_limit;
return tr_compare_3way(a_progress, b_progress);
return a_progress <=> b_progress;
}
int Torrent::compareRatio(Torrent const& that) const
std::partial_ordering Torrent::compareRatio(Torrent const& that) const
{
double const a = ratio();
double const b = that.ratio();
if (static_cast<int>(a) == TR_RATIO_INF && static_cast<int>(b) == TR_RATIO_INF)
{
return 0;
return std::partial_ordering::equivalent;
}
if (static_cast<int>(a) == TR_RATIO_INF)
{
return 1;
return std::partial_ordering::greater;
}
if (static_cast<int>(b) == TR_RATIO_INF)
{
return -1;
return std::partial_ordering::less;
}
return tr_compare_3way(a, b);
return a <=> b;
}
int Torrent::compareETA(Torrent const& that) const
std::strong_ordering Torrent::compareETA(Torrent const& that) const
{
bool const have_a(hasETA());
bool const have_b(that.hasETA());
if (have_a && have_b)
{
return getETA() - that.getETA();
return getETA() <=> that.getETA();
}
if (have_a)
{
return 1;
return std::strong_ordering::greater;
}
if (have_b)
{
return -1;
return std::strong_ordering::less;
}
return 0;
return std::strong_ordering::equal;
}
/***

View File

@@ -6,6 +6,7 @@
#pragma once
#include <bitset>
#include <compare>
#include <cstddef> // size_t
#include <cstdint> // uint64_t
#include <ctime> // time_t
@@ -18,10 +19,7 @@
#include <QString>
#include <QStringList>
#include <libtransmission/transmission.h>
#include <libtransmission/crypto-utils.h>
#include "libtransmission/tr-macros.h"
#include <libtransmission/quark.h>
#include "IconCache.h"
@@ -128,14 +126,9 @@ public:
return data_ == that.data_;
}
[[nodiscard]] constexpr auto operator!=(TorrentHash const& that) const
[[nodiscard]] constexpr auto operator<=>(TorrentHash const& that) const
{
return !(*this == that);
}
[[nodiscard]] auto operator<(TorrentHash const& that) const
{
return data_ < that.data_;
return data_ <=> that.data_;
}
[[nodiscard]] constexpr auto& toString() const noexcept
@@ -310,9 +303,9 @@ public:
return failed_ever_;
}
int compareSeedProgress(Torrent const& that) const;
int compareRatio(Torrent const& that) const;
int compareETA(Torrent const& that) const;
std::partial_ordering compareSeedProgress(Torrent const& that) const;
std::partial_ordering compareRatio(Torrent const& that) const;
std::strong_ordering compareETA(Torrent const& that) const;
[[nodiscard]] constexpr auto getETA() const noexcept
{

View File

@@ -4,6 +4,7 @@
// License text can be found in the licenses/ folder.
#include <array>
#include <compare>
#include <optional>
#include "libtransmission/utils.h"
@@ -77,7 +78,7 @@ void TorrentFilter::refilter()
bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right) const
{
int val = 0;
auto val = std::partial_ordering::equivalent;
auto const* a = sourceModel()->data(left, TorrentModel::TorrentRole).value<Torrent const*>();
auto const* b = sourceModel()->data(right, TorrentModel::TorrentRole).value<Torrent const*>();
@@ -86,7 +87,7 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortByQueue:
if (val == 0)
{
val = -tr_compare_3way(a->queuePosition(), b->queuePosition());
val = b->queuePosition() <=> a->queuePosition();
}
break;
@@ -94,7 +95,7 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortBySize:
if (val == 0)
{
val = tr_compare_3way(a->sizeWhenDone(), b->sizeWhenDone());
val = a->sizeWhenDone() <=> b->sizeWhenDone();
}
break;
@@ -102,7 +103,7 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortByAge:
if (val == 0)
{
val = tr_compare_3way(a->dateAdded(), b->dateAdded());
val = a->dateAdded() <=> b->dateAdded();
}
break;
@@ -110,7 +111,7 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortById:
if (val == 0)
{
val = tr_compare_3way(a->id(), b->id());
val = a->id() <=> b->id();
}
break;
@@ -118,14 +119,13 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortByActivity:
if (val == 0)
{
val = tr_compare_3way(a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed());
val = a->downloadSpeed() + a->uploadSpeed() <=> b->downloadSpeed() + b->uploadSpeed();
}
if (val == 0)
{
val = tr_compare_3way(
a->peersWeAreUploadingTo() + a->webseedsWeAreDownloadingFrom(),
b->peersWeAreUploadingTo() + b->webseedsWeAreDownloadingFrom());
val = a->peersWeAreUploadingTo() + a->webseedsWeAreDownloadingFrom() <=>
b->peersWeAreUploadingTo() + b->webseedsWeAreDownloadingFrom();
}
[[fallthrough]];
@@ -133,22 +133,22 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortByState:
if (val == 0)
{
val = -tr_compare_3way(a->isPaused(), b->isPaused());
val = static_cast<int>(b->isPaused()) <=> static_cast<int>(a->isPaused());
}
if (val == 0)
{
val = tr_compare_3way(a->getActivity(), b->getActivity());
val = a->getActivity() <=> b->getActivity();
}
if (val == 0)
{
val = -tr_compare_3way(a->queuePosition(), b->queuePosition());
val = b->queuePosition() <=> a->queuePosition();
}
if (val == 0)
{
val = tr_compare_3way(a->hasError(), b->hasError());
val = static_cast<int>(a->hasError()) <=> static_cast<int>(b->hasError());
}
[[fallthrough]];
@@ -156,12 +156,12 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
case SortMode::SortByProgress:
if (val == 0)
{
val = tr_compare_3way(a->metadataPercentDone(), b->metadataPercentDone());
val = a->metadataPercentDone() <=> b->metadataPercentDone();
}
if (val == 0)
{
val = tr_compare_3way(a->percentComplete(), b->percentComplete());
val = a->percentComplete() <=> b->percentComplete();
}
if (val == 0)
@@ -171,7 +171,7 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
if (val == 0)
{
val = -tr_compare_3way(a->queuePosition(), b->queuePosition());
val = b->queuePosition() <=> a->queuePosition();
}
[[fallthrough]];
@@ -198,12 +198,23 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right)
if (val == 0)
{
val = -a->name().compare(b->name(), Qt::CaseInsensitive);
if (auto const tmp = a->name().compare(b->name(), Qt::CaseInsensitive); tmp < 0)
{
val = std::strong_ordering::greater;
}
else if (tmp > 0)
{
val = std::strong_ordering::less;
}
else
{
val = std::strong_ordering::equal;
}
}
if (val == 0)
{
val = tr_compare_3way(a->hash(), b->hash());
val = a->hash() <=> b->hash();
}
return val < 0;