From 07ed8acdf98cd6c058fb9f5d62953e863c1f7886 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 29 Nov 2023 20:11:46 +0100 Subject: [PATCH] QPartialOrdering: add missing conversions to/from Qt::_ordering types Since QPartialOrdering is supposed to be a drop-in-replacement for Qt::partial_ordering, it need to have the same conversions from Qt::_ordering types that Qt::partial_ordering has. Fix by adding the necessary conversion and relational operators and conversion constructors. Change-Id: Ib8e78c850b43c8bcb3bb15c5f7d25be9d0da7339 Reviewed-by: Ivan Solovev --- src/corelib/global/qcompare.h | 48 +++++++++++++++++++ .../corelib/global/qcompare/tst_qcompare.cpp | 1 + 2 files changed, 49 insertions(+) diff --git a/src/corelib/global/qcompare.h b/src/corelib/global/qcompare.h index 529f79c797a..f7fa7c65a7b 100644 --- a/src/corelib/global/qcompare.h +++ b/src/corelib/global/qcompare.h @@ -700,6 +700,54 @@ public: friend constexpr bool operator!=(QPartialOrdering lhs, QPartialOrdering rhs) noexcept { return lhs.m_order != rhs.m_order; } + constexpr Q_IMPLICIT QPartialOrdering(Qt::partial_ordering order) noexcept + : m_order{} // == equivalent + { + if (order == Qt::partial_ordering::less) + m_order = static_cast(QtPrivate::Ordering::Less); + else if (order == Qt::partial_ordering::greater) + m_order = static_cast(QtPrivate::Ordering::Greater); + else if (order == Qt::partial_ordering::unordered) + m_order = static_cast(QtPrivate::LegacyUncomparable::Unordered); + } + + constexpr Q_IMPLICIT QPartialOrdering(Qt::weak_ordering stdorder) noexcept + : QPartialOrdering(Qt::partial_ordering{stdorder}) {} + + constexpr Q_IMPLICIT QPartialOrdering(Qt::strong_ordering stdorder) noexcept + : QPartialOrdering(Qt::partial_ordering{stdorder}) {} + + constexpr Q_IMPLICIT operator Qt::partial_ordering() const noexcept + { + using O = QtPrivate::Ordering; + using U = QtPrivate::LegacyUncomparable; + using R = Qt::partial_ordering; + switch (m_order) { + case qToUnderlying(O::Less): return R::less; + case qToUnderlying(O::Greater): return R::greater; + case qToUnderlying(O::Equivalent): return R::equivalent; + case qToUnderlying(U::Unordered): return R::unordered; + } + // GCC 8.x does not treat __builtin_unreachable() as constexpr +#if !defined(Q_CC_GNU_ONLY) || (Q_CC_GNU >= 900) + // NOLINTNEXTLINE(qt-use-unreachable-return): Triggers on Clang, breaking GCC 8 + Q_UNREACHABLE(); +#endif + return R::unordered; + } + + friend constexpr bool operator==(QPartialOrdering lhs, Qt::partial_ordering rhs) noexcept + { Qt::partial_ordering qt = lhs; return qt == rhs; } + + friend constexpr bool operator!=(QPartialOrdering lhs, Qt::partial_ordering rhs) noexcept + { Qt::partial_ordering qt = lhs; return qt != rhs; } + + friend constexpr bool operator==(Qt::partial_ordering lhs, QPartialOrdering rhs) noexcept + { Qt::partial_ordering qt = rhs; return lhs == qt; } + + friend constexpr bool operator!=(Qt::partial_ordering lhs, QPartialOrdering rhs) noexcept + { Qt::partial_ordering qt = rhs; return lhs != qt; } + #ifdef __cpp_lib_three_way_comparison constexpr Q_IMPLICIT QPartialOrdering(std::partial_ordering stdorder) noexcept { diff --git a/tests/auto/corelib/global/qcompare/tst_qcompare.cpp b/tests/auto/corelib/global/qcompare/tst_qcompare.cpp index f473c1706d9..e02eb61fded 100644 --- a/tests/auto/corelib/global/qcompare/tst_qcompare.cpp +++ b/tests/auto/corelib/global/qcompare/tst_qcompare.cpp @@ -148,6 +148,7 @@ void tst_QCompare::legacyConversions() CHECK_CONVERTS(NS ::strong_ordering, QPartialOrdering); \ } while (false) + CHECK_ALL(Qt); #ifdef __cpp_lib_three_way_comparison CHECK_ALL(std); #endif // __cpp_lib_three_way_comparison