From 7aadebba70cab7704d9d808fe392ba0a01d61ee0 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Sat, 30 Apr 2022 19:27:25 +0800 Subject: [PATCH] refactor(miscellaneous): replace TSynthThreeWay with the function version --- .../Private/Testing/MiscellaneousTesting.cpp | 27 +++----- .../Source/Public/Miscellaneous/Compare.h | 66 +++---------------- .../Source/Public/Templates/Tuple.h | 2 +- 3 files changed, 18 insertions(+), 77 deletions(-) diff --git a/Redcraft.Utility/Source/Private/Testing/MiscellaneousTesting.cpp b/Redcraft.Utility/Source/Private/Testing/MiscellaneousTesting.cpp index 4b9c303..59b559e 100644 --- a/Redcraft.Utility/Source/Private/Testing/MiscellaneousTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/MiscellaneousTesting.cpp @@ -205,27 +205,18 @@ void TestCompare() always_check((TIsSame::Type, weak_ordering >::Value)); always_check((TIsSame::Type, strong_ordering >::Value)); - always_check((TCompareThreeWay()(0, 0) == strong_ordering::equal)); - always_check((TCompareThreeWay() (0, 0.0) == strong_ordering::equal)); + always_check((SynthThreeWayCompare(0, 0) == strong_ordering::equal)); + always_check((SynthThreeWayCompare(0, 0.0) == strong_ordering::equal)); - - always_check(TSynthThreeWay{}(FTestPartialOrdering(-1), FTestPartialOrdering( 0)) == partial_ordering::less); - always_check(TSynthThreeWay{}(FTestPartialOrdering( 0), FTestPartialOrdering( 0)) == partial_ordering::equivalent); - always_check(TSynthThreeWay{}(FTestPartialOrdering( 0), FTestPartialOrdering(-1)) == partial_ordering::greater); + always_check(SynthThreeWayCompare(FTestPartialOrdering(-1), FTestPartialOrdering( 0)) == partial_ordering::less); + always_check(SynthThreeWayCompare(FTestPartialOrdering( 0), FTestPartialOrdering( 0)) == partial_ordering::equivalent); + always_check(SynthThreeWayCompare(FTestPartialOrdering( 0), FTestPartialOrdering(-1)) == partial_ordering::greater); - always_check(TSynthThreeWay{}(FTestPartialOrdering( 0, true), FTestPartialOrdering( 0, false)) == partial_ordering::unordered); + always_check(SynthThreeWayCompare(FTestPartialOrdering( 0, true), FTestPartialOrdering( 0, false)) == partial_ordering::unordered); - always_check(TSynthThreeWay{}(FTestSynth(-1), FTestSynth( 0)) == weak_ordering::less); - always_check(TSynthThreeWay{}(FTestSynth( 0), FTestSynth( 0)) == weak_ordering::equivalent); - always_check(TSynthThreeWay{}(FTestSynth( 0), FTestSynth(-1)) == weak_ordering::greater); - - always_check((StrongOrder(0, 0) == strong_ordering::equal)); - always_check((WeakOrder(0, 0) == strong_ordering::equal)); - always_check((PartialOrder(0, 0) == strong_ordering::equal)); - always_check((CompareStrongOrderFallback(0, 0) == strong_ordering::equal)); - always_check((CompareWeakOrderFallback(0, 0) == strong_ordering::equal)); - always_check((ComparePartialOrderFallback(0, 0) == strong_ordering::equal)); - + always_check(SynthThreeWayCompare(FTestSynth(-1), FTestSynth( 0)) == weak_ordering::less); + always_check(SynthThreeWayCompare(FTestSynth( 0), FTestSynth( 0)) == weak_ordering::equivalent); + always_check(SynthThreeWayCompare(FTestSynth( 0), FTestSynth(-1)) == weak_ordering::greater); } NAMESPACE_UNNAMED_BEGIN diff --git a/Redcraft.Utility/Source/Public/Miscellaneous/Compare.h b/Redcraft.Utility/Source/Public/Miscellaneous/Compare.h index 326036f..a8bcaaa 100644 --- a/Redcraft.Utility/Source/Public/Miscellaneous/Compare.h +++ b/Redcraft.Utility/Source/Public/Miscellaneous/Compare.h @@ -68,25 +68,6 @@ struct TCompareThreeWayResult using Type = decltype(DeclVal::Type&>() <=> DeclVal::Type&>()); }; -template requires (CSameAs || CThreeWayComparable) -struct TCompareThreeWay -{ - constexpr auto operator()(T&& LHS, T&& RHS) const - { - return Forward(LHS) <=> Forward(RHS); - } -}; - -template <> -struct TCompareThreeWay -{ - template requires CThreeWayComparableWith - constexpr auto operator()(T&& LHS, U&& RHS) const - { - return Forward(LHS) <=> Forward(RHS); - } -}; - template concept CSynthThreeWayComparable = CThreeWayComparable || requires(const TRemoveReference::Type& A, const TRemoveReference::Type& B) @@ -103,56 +84,25 @@ concept CSynthThreeWayComparableWith = CThreeWayComparableWith || { B < A } -> CBooleanTestable; }; -template requires (CSameAs || CSynthThreeWayComparable) -struct TSynthThreeWay +template requires CSynthThreeWayComparableWith +constexpr decltype(auto) SynthThreeWayCompare(T&& LHS, U&& RHS) { - constexpr auto operator()(T&& LHS, T&& RHS) const + if constexpr (CThreeWayComparableWith) { - if constexpr (CThreeWayComparable) - { - return Forward(LHS) <=> Forward(RHS); - } - else - { - return Forward(LHS) < Forward(RHS) ? weak_ordering::less : Forward(RHS) < Forward(LHS) ? weak_ordering::greater : weak_ordering::equivalent; - } + return Forward(LHS) <=> Forward(RHS); } -}; - -template <> -struct TSynthThreeWay -{ - template requires CSynthThreeWayComparableWith - constexpr auto operator()(T&& LHS, U&& RHS) const + else { - if constexpr (CThreeWayComparableWith) - { - return Forward(LHS) <=> Forward(RHS); - } - else - { - return Forward(LHS) < Forward(RHS) ? weak_ordering::less : Forward(RHS) < Forward(LHS) ? weak_ordering::greater : weak_ordering::equivalent; - } + return Forward(LHS) < Forward(RHS) ? weak_ordering::less : Forward(RHS) < Forward(LHS) ? weak_ordering::greater : weak_ordering::equivalent; } -}; +} template struct TSynthThreeWayResult { - using Type = decltype(TSynthThreeWay{}(DeclVal::Type&>(), DeclVal::Type&>())); + using Type = decltype(SynthThreeWayCompare(DeclVal::Type&>(), DeclVal::Type&>())); }; -NAMESPACE_UNNAMED_BEGIN - -inline constexpr decltype(NAMESPACE_STD::strong_order) StrongOrder; -inline constexpr decltype(NAMESPACE_STD::weak_order) WeakOrder; -inline constexpr decltype(NAMESPACE_STD::partial_order) PartialOrder; -inline constexpr decltype(NAMESPACE_STD::compare_strong_order_fallback) CompareStrongOrderFallback; -inline constexpr decltype(NAMESPACE_STD::compare_weak_order_fallback) CompareWeakOrderFallback; -inline constexpr decltype(NAMESPACE_STD::compare_partial_order_fallback) ComparePartialOrderFallback; - -NAMESPACE_UNNAMED_END - NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Templates/Tuple.h b/Redcraft.Utility/Source/Public/Templates/Tuple.h index cbd004d..97ceadb 100644 --- a/Redcraft.Utility/Source/Public/Templates/Tuple.h +++ b/Redcraft.Utility/Source/Public/Templates/Tuple.h @@ -501,7 +501,7 @@ struct TTupleThreeWay> template static constexpr R F(const LHSTupleType& LHS, const RHSTupleType& RHS) { - auto Result = TSynthThreeWay{}(LHS.template GetValue(), RHS.template GetValue()); + auto Result = SynthThreeWayCompare(LHS.template GetValue(), RHS.template GetValue()); if (Result != 0) return Result; return TTupleThreeWay>::F(LHS, RHS); }