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

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

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/Invoke.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 TestInvoke();
void REDCRAFTUTILITY_API TestReferenceWrapper();
void REDCRAFTUTILITY_API TestCompare();
void REDCRAFTUTILITY_API TestMiscellaneous();
NAMESPACE_MODULE_END(Utility)