feat(templates): add three-way comparison operator support

This commit is contained in:
_Redstone_c_ 2022-03-09 23:17:54 +08:00
parent babc7327eb
commit c2aecef3dd
6 changed files with 457 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include "Testing/ConceptsTesting.h" #include "Testing/ConceptsTesting.h"
#include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/AssertionMacros.h"
#include "Templates/Templates.h"
#include "Concepts/Concepts.h" #include "Concepts/Concepts.h"
NAMESPACE_REDCRAFT_BEGIN NAMESPACE_REDCRAFT_BEGIN

View File

@ -10,6 +10,7 @@ void TestTemplates()
{ {
TestInvoke(); TestInvoke();
TestReferenceWrapper(); TestReferenceWrapper();
TestCompare();
TestMiscellaneous(); TestMiscellaneous();
} }
@ -68,6 +69,134 @@ void TestReferenceWrapper()
NAMESPACE_UNNAMED_BEGIN NAMESPACE_UNNAMED_BEGIN
struct FTestPartialOrdering
{
int32 Num;
bool bIsValid;
FTestPartialOrdering(int32 InNum, bool bInIsValid = true) : Num(InNum), bIsValid(bInIsValid) { }
friend bool operator==(FTestPartialOrdering LHS, FTestPartialOrdering RHS) { return LHS.bIsValid && RHS.bIsValid ? LHS.Num == RHS.Num : false; }
friend partial_ordering operator<=>(FTestPartialOrdering LHS, FTestPartialOrdering RHS) { return LHS.bIsValid && RHS.bIsValid ? LHS.Num <=> RHS.Num : partial_ordering::unordered; }
};
struct FTestWeakOrdering
{
int32 Num;
FTestWeakOrdering(int32 InNum) : Num(InNum) { }
friend bool operator==(FTestWeakOrdering LHS, FTestWeakOrdering RHS) { return LHS.Num == RHS.Num; }
friend weak_ordering operator<=>(FTestWeakOrdering LHS, FTestWeakOrdering RHS) { return LHS.Num <=> RHS.Num; }
};
struct FTestStrongOrdering
{
int32 Num;
FTestStrongOrdering(int32 InNum) : Num(InNum) { }
friend bool operator==(FTestStrongOrdering LHS, FTestStrongOrdering RHS) { return LHS.Num == RHS.Num; }
friend strong_ordering operator<=>(FTestStrongOrdering LHS, FTestStrongOrdering RHS) { return LHS.Num <=> RHS.Num; }
};
NAMESPACE_UNNAMED_END
void TestCompare()
{
always_check((-1 <=> 0) == strong_ordering::less);
always_check(( 0 <=> 0) == strong_ordering::equivalent);
always_check(( 0 <=> 0) == strong_ordering::equal);
always_check(( 0 <=> -1) == strong_ordering::greater);
always_check((-1 <=> 0) < 0);
always_check((-1 <=> 0) <= 0);
always_check(( 0 <=> 0) <= 0);
always_check(( 0 <=> 0) == 0);
always_check(( 0 <=> 0) >= 0);
always_check(( 0 <=> -1) >= 0);
always_check(( 0 <=> -1) > 0);
always_check((-1 <=> 1) != 0);
int64 NaNBit = 0xFFF8000000000000;
double NaN = *reinterpret_cast<double*>(&NaNBit);
always_check((-1.0 <=> 0.0) == partial_ordering::less);
always_check(( 0.0 <=> 0.0) == partial_ordering::equivalent);
always_check(( 0.0 <=> -1.0) == partial_ordering::greater);
always_check(( 0.0 <=> NaN) == partial_ordering::unordered);
always_check((-1.0 <=> 0.0) == weak_ordering::less);
always_check(( 0.0 <=> 0.0) == weak_ordering::equivalent);
always_check(( 0.0 <=> -1.0) == weak_ordering::greater);
always_check((-1.0 <=> 0.0) == strong_ordering::less);
always_check(( 0.0 <=> 0.0) == strong_ordering::equivalent);
always_check(( 0.0 <=> 0.0) == strong_ordering::equal);
always_check(( 0.0 <=> -1.0) == strong_ordering::greater);
always_check((-1.0 <=> 0.0) < 0);
always_check((-1.0 <=> 0.0) <= 0);
always_check(( 0.0 <=> 0.0) <= 0);
always_check(( 0.0 <=> 0.0) == 0);
always_check(( 0.0 <=> 0.0) >= 0);
always_check(( 0.0 <=> -1.0) >= 0);
always_check(( 0.0 <=> -1.0) > 0);
always_check((-1.0 <=> 1.0) != 0);
always_check((FTestPartialOrdering(-1) <=> FTestPartialOrdering( 0)) == partial_ordering::less);
always_check((FTestPartialOrdering( 0) <=> FTestPartialOrdering( 0)) == partial_ordering::equivalent);
always_check((FTestPartialOrdering( 0) <=> FTestPartialOrdering(-1)) == partial_ordering::greater);
always_check((FTestPartialOrdering( 0, true) <=> FTestPartialOrdering( 0, false)) == partial_ordering::unordered);
always_check((FTestWeakOrdering(-1) <=> FTestWeakOrdering( 0)) == weak_ordering::less);
always_check((FTestWeakOrdering( 0) <=> FTestWeakOrdering( 0)) == weak_ordering::equivalent);
always_check((FTestWeakOrdering( 0) <=> FTestWeakOrdering(-1)) == weak_ordering::greater);
always_check((FTestStrongOrdering(-1) <=> FTestStrongOrdering( 0)) == strong_ordering::less);
always_check((FTestStrongOrdering( 0) <=> FTestStrongOrdering( 0)) == strong_ordering::equivalent);
always_check((FTestStrongOrdering( 0) <=> FTestStrongOrdering( 0)) == strong_ordering::equal);
always_check((FTestStrongOrdering( 0) <=> FTestStrongOrdering(-1)) == strong_ordering::greater);
always_check((FTestPartialOrdering(-1) < FTestPartialOrdering( 0)));
always_check((FTestPartialOrdering( 0) == FTestPartialOrdering( 0)));
always_check((FTestPartialOrdering( 0) > FTestPartialOrdering(-1)));
always_check((FTestWeakOrdering(-1) < FTestWeakOrdering( 0)));
always_check((FTestWeakOrdering( 0) == FTestWeakOrdering( 0)));
always_check((FTestWeakOrdering( 0) > FTestWeakOrdering(-1)));
always_check((FTestStrongOrdering(-1) < FTestStrongOrdering( 0)));
always_check((FTestStrongOrdering( 0) == FTestStrongOrdering( 0)));
always_check((FTestStrongOrdering( 0) > FTestStrongOrdering(-1)));
always_check((TIsSame<TCommonComparisonCategory<strong_ordering >::Type, strong_ordering >::Value));
always_check((TIsSame<TCommonComparisonCategory<strong_ordering, weak_ordering >::Type, weak_ordering >::Value));
always_check((TIsSame<TCommonComparisonCategory<strong_ordering, weak_ordering, partial_ordering>::Type, partial_ordering>::Value));
always_check(CThreeWayComparable<int32>);
always_check(CThreeWayComparable<FTestPartialOrdering>);
always_check(CThreeWayComparable<FTestWeakOrdering>);
always_check(CThreeWayComparable<FTestStrongOrdering>);
always_check((CThreeWayComparableWith<bool, bool>));
always_check((CThreeWayComparableWith<int16, int32>));
always_check((TIsSame<TCompareThreeWayResult<int32 >::Type, strong_ordering >::Value));
always_check((TIsSame<TCompareThreeWayResult<float >::Type, partial_ordering>::Value));
always_check((TIsSame<TCompareThreeWayResult<FTestPartialOrdering>::Type, partial_ordering>::Value));
always_check((TIsSame<TCompareThreeWayResult<FTestWeakOrdering >::Type, weak_ordering >::Value));
always_check((TIsSame<TCompareThreeWayResult<FTestStrongOrdering >::Type, strong_ordering >::Value));
always_check((TCompareThreeWay<int32>()(0, 0) == strong_ordering::equal));
always_check((TCompareThreeWay<void>() (0, 0.0) == strong_ordering::equal));
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));
}
NAMESPACE_UNNAMED_BEGIN
template <typename T> template <typename T>
struct TTestStructA struct TTestStructA
{ {

View File

@ -0,0 +1,102 @@
#pragma once
#include "CoreTypes.h"
#include "Concepts/Concepts.h"
#include "TypeTraits/TypeTraits.h"
#include <compare>
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
// The result of the three-way comparison operator is the built-in type of the compiler, which is directly introduced here.
typedef NAMESPACE_STD::partial_ordering partial_ordering;
typedef NAMESPACE_STD::weak_ordering weak_ordering;
typedef NAMESPACE_STD::strong_ordering strong_ordering;
NAMESPACE_PRIVATE_BEGIN
template<int32> struct TCommonComparisonCategory { using Type = void; };
template<> struct TCommonComparisonCategory<0> { using Type = strong_ordering; };
template<> struct TCommonComparisonCategory<2> { using Type = partial_ordering; };
template<> struct TCommonComparisonCategory<4> { using Type = weak_ordering; };
template<> struct TCommonComparisonCategory<6> { using Type = partial_ordering; };
NAMESPACE_PRIVATE_END
template <typename... Types>
struct TCommonComparisonCategory
: NAMESPACE_PRIVATE::TCommonComparisonCategory<(0u | ... |
(
TIsSame<Types, strong_ordering >::Value ? 0u :
TIsSame<Types, weak_ordering >::Value ? 4u :
TIsSame<Types, partial_ordering>::Value ? 2u : 1u
)
)>
{ };
template <typename T, typename OrderingType>
concept CThreeWayComparesAs = CSameAs<typename TCommonComparisonCategory<T, OrderingType>::Type, OrderingType>;
template <typename T, typename OrderingType = partial_ordering>
concept CThreeWayComparable = CWeaklyEqualityComparableWith<T, T> && CPartiallyOrderedWith<T, T> &&
requires(const TRemoveReference<T>::Type& A, const TRemoveReference<T>::Type& B)
{
{ A <=> B } -> CThreeWayComparesAs<OrderingType>;
};
template <typename T, typename U, typename OrderingType = partial_ordering>
concept CThreeWayComparableWith = CWeaklyEqualityComparableWith<T, U> && CPartiallyOrderedWith<T, U> &&
CThreeWayComparable<T, OrderingType> && CThreeWayComparable<U, OrderingType> &&
CCommonReferenceWith<const typename TRemoveReference<T>::Type&, const typename TRemoveReference<U>::Type&> &&
CThreeWayComparable<typename TCommonReference<const typename TRemoveReference<T>::Type&, const typename TRemoveReference<U>::Type&>::Type, OrderingType> &&
requires(const TRemoveReference<T>::Type& A, const TRemoveReference<T>::Type& B)
{
{ A <=> B } -> CThreeWayComparesAs<OrderingType>;
{ B <=> A } -> CThreeWayComparesAs<OrderingType>;
};
template <typename T, typename U = T>
struct TCompareThreeWayResult { };
template <typename T, typename U> requires CThreeWayComparableWith<T, U>
struct TCompareThreeWayResult<T, U>
{
using Type = decltype(DeclVal<const typename TRemoveReference<T>::Type&>() <=> DeclVal<const typename TRemoveReference<U>::Type&>());
};
template <typename T = void> requires CSameAs<T, void> || CThreeWayComparable<T>
struct TCompareThreeWay
{
constexpr auto operator()(T&& LHS, T&& RHS) const
{
return Forward<T>(LHS) <=> Forward<T>(RHS);
}
};
template <>
struct TCompareThreeWay<void>
{
template <typename T, typename U> requires CThreeWayComparableWith<T, U>
constexpr auto operator()(T&& LHS, U&& RHS) const
{
return Forward<T>(LHS) <=> Forward<U>(RHS);
}
};
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

View File

@ -0,0 +1,223 @@
#pragma once
#include "CoreTypes.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename T>
struct TOptional
{
public:
using Type = T;
constexpr TOptional() : bIsValid(false) { }
template <typename... Types> requires TIsConstructible<T, Types...>::Value
constexpr explicit TOptional(EInPlace, Types&&... Args)
: bIsValid(true)
{
new(&Value) T(Forward<Types>(Args)...);
}
template <typename U = T> requires TIsConstructible<T, U&&>::Value && !TIsSame<typename TRemoveCVRef<U>::Type, EInPlace>::Value && !TIsSame<typename TRemoveCVRef<U>::Type, TOptional>::Value
constexpr explicit(!TIsConvertible<U&&, T>::Value) TOptional(U&& InValue)
: TOptional(InPlace, Forward<U>(InValue))
{ }
template <typename U = T> requires TIsConstructible<T, const U&>::Value
constexpr explicit(!TIsConvertible<const U&, T>::Value) TOptional(const TOptional<U>& InValue)
: bIsValid(InValue.bIsValid)
{
if (InValue.bIsValid) new(&Value) T(InValue.GetValue());
}
template <typename U = T> requires TIsConstructible<T, U&&>::Value
constexpr explicit(!TIsConvertible<U&&, T>::Value) TOptional(TOptional<U>&& InValue)
: bIsValid(InValue.bIsValid)
{
if (InValue.bIsValid) new(&Value) T(MoveTempIfPossible(InValue).GetValue());
}
constexpr ~TOptional()
{
Reset();
}
template <typename U = T> requires TIsConstructible<T, const U&>::Value
constexpr TOptional& operator=(const TOptional<U>& InValue)
{
if (InValue == this) return *this;
Reset();
if (InValue.bIsValid)
{
new(&Value) T(InValue.GetValue());
bIsValid = true;
}
return *this;
}
template <typename U = T> requires TIsConstructible<T, U&&>::Value
constexpr TOptional& operator=(TOptional<U>&& InValue)
{
if (InValue == this) return *this;
Reset();
if (InValue.bIsValid)
{
new(&Value) T(MoveTempIfPossible(InValue).GetValue());
bIsValid = true;
}
return *this;
}
template <typename U = T> requires TIsConstructible<T, U&&>::Value
constexpr TOptional& operator=(U&& InValue)
{
Reset();
new(&Value) T(MoveTempIfPossible(InValue));
bIsValid = true;
return *this;
}
template <typename... ArgsType>
constexpr T& Emplace(ArgsType&&... Args)
{
Reset();
T* Result = new(&Value) T(Forward<ArgsType>(Args)...);
bIsValid = true;
return *Result;
}
constexpr bool IsValid() const { return bIsValid; }
constexpr explicit operator bool() const { return bIsValid; }
constexpr T& GetValue() & { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; }
constexpr T&& GetValue() && { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; }
constexpr const T& GetValue() const& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; }
constexpr const T&& GetValue() const&& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; }
constexpr const T* operator->() const { return &GetValue(); }
constexpr T* operator->() { return &GetValue(); }
constexpr T& operator*() & { return GetValue(); }
constexpr T&& operator*() && { return GetValue(); }
constexpr const T& operator*() const& { return GetValue(); }
constexpr const T&& operator*() const&& { return GetValue(); }
template <typename U = T>
constexpr T Get(U&& DefaultValue) && { return IsValid() ? GetValue() : DefaultValue; }
template <typename U = T>
constexpr T Get(U&& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; }
constexpr void Reset()
{
if (bIsValid)
{
bIsValid = false;
typedef T DestructOptionalType;
((T*)&Value)->DestructOptionalType::~DestructOptionalType();
}
}
private:
TAlignedStorage<sizeof(T), alignof(T)>::Type Value;
bool bIsValid;
};
template <typename T>
TOptional(T) ->TOptional<T>;
template <typename T, typename U>
constexpr bool operator==(const TOptional<T>& LHS, const TOptional<U>& RHS)
{
if (LHS.IsValid() != LHS.IsValid()) return false;
if (LHS.IsValid() == false) return true;
return *LHS == *RHS;
}
template <typename T, typename U>
constexpr bool operator!=(const TOptional<T>& LHS, const TOptional<U>& RHS)
{
if (LHS.IsValid() != LHS.IsValid()) return true;
if (LHS.IsValid() == false) return false;
return *LHS != *RHS;
}
//template <typename T, typename U>
//constexpr bool operator<(const TOptional<T>&, const TOptional<U>&);
//template <typename T, typename U>
//constexpr bool operator>(const TOptional<T>&, const TOptional<U>&);
//template <typename T, typename U>
//constexpr bool operator<=(const TOptional<T>&, const TOptional<U>&);
//template <typename T, typename U>
//constexpr bool operator>=(const TOptional<T>&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator==(const TOptional<T>&, const U&);
//template <typename T, typename U> constexpr bool operator==(const T&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator!=(const TOptional<T>&, const U&);
//template <typename T, typename U> constexpr bool operator!=(const T&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator<(const TOptional<T>&, const U&);
//template <typename T, typename U> constexpr bool operator<(const T&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator>(const TOptional<T>&, const U&);
//template <typename T, typename U> constexpr bool operator>(const T&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator<=(const TOptional<T>&, const U&);
//template <typename T, typename U> constexpr bool operator<=(const T&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator>=(const TOptional<T>&, const U&);
//template <typename T, typename U> constexpr bool operator>=(const T&, const TOptional<U>&);
template <typename T>
constexpr TOptional<typename TDecay<T>::Type> MakeOptional(T&& InValue)
{
return TOptional<typename TDecay<T>::Type>(Forward<T>(InValue));
}
template <typename T, typename... Types>
constexpr TOptional<T> MakeOptional(Types&&... Args)
{
return TOptional<T>(InPlace, Forward<T>(Args)...);
}
template <typename T>
constexpr void Swap(TOptional<T>& A, TOptional<T>& B)
{
if (!A && !B) return;
if (A && !B)
{
B = A;
A.Reset();
return;
}
if (B && !A)
{
A = B;
B.Reset();
return;
}
Swap(*A, *B);
}
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -6,3 +6,4 @@
#include "Templates/Noncopyable.h" #include "Templates/Noncopyable.h"
#include "Templates/Invoke.h" #include "Templates/Invoke.h"
#include "Templates/ReferenceWrapper.h" #include "Templates/ReferenceWrapper.h"
#include "Templates/Compare.h"

View File

@ -9,6 +9,7 @@ NAMESPACE_MODULE_BEGIN(Utility)
void REDCRAFTUTILITY_API TestTemplates(); void REDCRAFTUTILITY_API TestTemplates();
void REDCRAFTUTILITY_API TestInvoke(); void REDCRAFTUTILITY_API TestInvoke();
void REDCRAFTUTILITY_API TestReferenceWrapper(); void REDCRAFTUTILITY_API TestReferenceWrapper();
void REDCRAFTUTILITY_API TestCompare();
void REDCRAFTUTILITY_API TestMiscellaneous(); void REDCRAFTUTILITY_API TestMiscellaneous();
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)