From f72d6a53d8b33d509c91e3a7b1c4cf0270f6d0b1 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Sun, 24 Apr 2022 22:54:08 +0800 Subject: [PATCH] refactor(templates): remove the predefined functors, Lambda is a better choice --- .../Private/Testing/TemplatesTesting.cpp | 77 +-------- .../Source/Public/Templates/Function.h | 153 ------------------ 2 files changed, 1 insertion(+), 229 deletions(-) diff --git a/Redcraft.Utility/Source/Private/Testing/TemplatesTesting.cpp b/Redcraft.Utility/Source/Private/Testing/TemplatesTesting.cpp index 6288480..c855850 100644 --- a/Redcraft.Utility/Source/Private/Testing/TemplatesTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/TemplatesTesting.cpp @@ -1158,88 +1158,13 @@ void TestFunction() } { - TFunction Identity = TIdentity<>(); + TFunction Identity = [](bool In) { return In; }; TFunction NotIdentity = NotFn(Identity); always_check(Identity(true)); always_check(NotIdentity(false)); } - { - always_check(TPromote ()(4 ) == 4); - always_check(TNegate ()(4 ) == -4); - always_check(TPlus ()(4, 2) == 6); - always_check(TMinus ()(4, 2) == 2); - always_check(TMultiplies()(4, 2) == 8); - always_check(TDivides ()(4, 2) == 2); - always_check(TModulus ()(4, 2) == 0); - - always_check(TBitNot()(4 ) == -5); - always_check(TBitAnd()(4, 2) == 0); - always_check(TBitOr ()(4, 2) == 6); - always_check(TBitXor()(4, 2) == 6); - always_check(TBitLsh()(4, 2) == 16); - always_check(TBitRsh()(4, 2) == 1); - - always_check(TLogicalAnd()(4, 2) == true); - always_check(TLogicalOr ()(4, 2) == true); - always_check(TLogicalNot()(4 ) == false); - - always_check(TEqualTo ()(4, 2) == false); - always_check(TNotEqualTo ()(4, 2) == true); - always_check(TGreater ()(4, 2) == true); - always_check(TLess ()(4, 2) == false); - always_check(TGreaterEqual()(4, 2) == true); - always_check(TLessEqual ()(4, 2) == false); - } - - { - TFunction TempA = TPlus <>(); - TFunction TempB = TMinus <>(); - TFunction TempC = TMultiplies<>(); - TFunction TempD = TDivides <>(); - TFunction TempE = TModulus <>(); - TFunction TempF = TNegate <>(); - - always_check(TempA(4, 2) == 6); - always_check(TempB(4, 2) == 2); - always_check(TempC(4, 2) == 8); - always_check(TempD(4, 2) == 2); - always_check(TempE(4, 2) == 0); - always_check(TempF(4 ) == -4); - - TFunction TempG = TEqualTo <>(); - TFunction TempH = TNotEqualTo <>(); - TFunction TempI = TGreater <>(); - TFunction TempJ = TLess <>(); - TFunction TempK = TGreaterEqual<>(); - TFunction TempL = TLessEqual <>(); - - always_check(TempG(4, 2) == false); - always_check(TempH(4, 2) == true); - always_check(TempI(4, 2) == true); - always_check(TempJ(4, 2) == false); - always_check(TempK(4, 2) == true); - always_check(TempL(4, 2) == false); - - TFunction TempM = TLogicalAnd<>(); - TFunction TempN = TLogicalOr <>(); - TFunction TempO = TLogicalNot<>(); - - always_check(TempM(4, 2) == true); - always_check(TempN(4, 2) == true); - always_check(TempO(4 ) == false); - - TFunction TempP = TBitAnd<>(); - TFunction TempQ = TBitOr <>(); - TFunction TempR = TBitXor<>(); - TFunction TempS = TBitNot<>(); - - always_check(TempP(4, 2) == 0); - always_check(TempQ(4, 2) == 6); - always_check(TempR(4, 2) == 6); - always_check(TempS(4 ) == -5); - } } void TestAtomic() diff --git a/Redcraft.Utility/Source/Public/Templates/Function.h b/Redcraft.Utility/Source/Public/Templates/Function.h index ae0f4d6..c0c3e69 100644 --- a/Redcraft.Utility/Source/Public/Templates/Function.h +++ b/Redcraft.Utility/Source/Public/Templates/Function.h @@ -403,29 +403,6 @@ constexpr bool operator==(const TUniqueFunction& LHS, nullptr_t) static_assert(sizeof(TFunction) == 64, "The byte size of TFunction is unexpected"); static_assert(sizeof(TUniqueFunction) == 64, "The byte size of TUniqueFunction is unexpected"); -template -struct TIdentity -{ - using Type = T; - - constexpr T&& operator()(T&& InValue) const - { - return Forward(InValue); - } -}; - -template <> -struct TIdentity -{ - using Type = void; - - template - constexpr T&& operator()(T&& InValue) const - { - return Forward(InValue); - } -}; - NAMESPACE_PRIVATE_BEGIN template @@ -476,136 +453,6 @@ constexpr NAMESPACE_PRIVATE::NotFunctionType::Type> NotFn(F&& return NAMESPACE_PRIVATE::NotFunctionType::Type>(Forward(Func)); } -#define FUNCTOR_UNARY_OPERATOR_IMPL(Name, Operator, ConceptT, ConceptU) \ - template requires (CSameAs || ConceptT) \ - struct Name \ - { \ - constexpr auto operator()(const T& InValue) const \ - -> decltype(Operator InValue) \ - { \ - return Operator InValue; \ - } \ - }; \ - \ - template <> \ - struct Name \ - { \ - template requires ConceptU \ - constexpr auto operator()(U&& InValue) const \ - -> decltype(Operator Forward(InValue)) \ - { \ - return Operator Forward(InValue); \ - } \ - } - -#define FUNCTOR_BINARY_OPERATOR_IMPL(Name, Operator, ConceptT, ConceptTU) \ - template requires (CSameAs || ConceptT) \ - struct Name \ - { \ - constexpr auto operator()(const T& LHS, const T& RHS) const \ - -> decltype(LHS Operator RHS) \ - { \ - return LHS Operator RHS; \ - } \ - }; \ - \ - template <> \ - struct Name \ - { \ - template requires ConceptTU \ - constexpr auto operator()(T&& LHS, U&& RHS) const \ - -> decltype(Forward(LHS) Operator Forward(RHS)) \ - { \ - return Forward(LHS) Operator Forward(RHS); \ - } \ - } - -#define FUNCTOR_UNARY_OPERATOR_A_IMPL(Name, Operator) \ - FUNCTOR_UNARY_OPERATOR_IMPL \ - ( \ - Name, Operator, \ - (requires(const T& InValue) { { Operator InValue } -> CConvertibleTo; }), \ - (requires(U&& InValue) { Operator Forward(InValue); }) \ - ) - -#define FUNCTOR_BINARY_OPERATOR_A_IMPL(Name, Operator) \ - FUNCTOR_BINARY_OPERATOR_IMPL \ - ( \ - Name, Operator, \ - (requires(const T& LHS, const T& RHS) { { LHS Operator RHS } -> CConvertibleTo; }), \ - (requires(T&& LHS, U&& RHS) { Forward(LHS) Operator Forward(RHS); }) \ - ) - -#define FUNCTOR_UNARY_OPERATOR_B_IMPL(Name, Operator) \ - FUNCTOR_UNARY_OPERATOR_IMPL \ - ( \ - Name, Operator, \ - (requires(const T& InValue) { { Operator InValue } -> CBooleanTestable; }), \ - (requires(U&& InValue) { { Operator Forward(InValue) } -> CBooleanTestable; }) \ - ) - -#define FUNCTOR_BINARY_OPERATOR_B_IMPL(Name, Operator) \ - FUNCTOR_BINARY_OPERATOR_IMPL \ - ( \ - Name, Operator, \ - (requires(const T& LHS, const T& RHS) { { LHS Operator RHS } -> CBooleanTestable; }), \ - (requires(T&& LHS, U&& RHS) { { Forward(LHS) Operator Forward(RHS) } -> CBooleanTestable; }) \ - ) - -#define FUNCTOR_BINARY_OPERATOR_C_IMPL(Name, Operator) \ - FUNCTOR_BINARY_OPERATOR_IMPL \ - ( \ - Name, Operator, \ - (CEqualityComparable), \ - (CEqualityComparableWith) \ - ) - -#define FUNCTOR_BINARY_OPERATOR_D_IMPL(Name, Operator) \ - FUNCTOR_BINARY_OPERATOR_IMPL \ - ( \ - Name, Operator, \ - (CTotallyOrdered), \ - (CTotallyOrderedWith) \ - ) - -FUNCTOR_UNARY_OPERATOR_A_IMPL (TPromote, +); -FUNCTOR_UNARY_OPERATOR_A_IMPL (TNegate, -); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TPlus, +); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TMinus, -); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TMultiplies, *); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TDivides, /); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TModulus, %); - -FUNCTOR_UNARY_OPERATOR_A_IMPL (TBitNot, ~ ); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TBitAnd, & ); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TBitOr, | ); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TBitXor, ^ ); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TBitLsh, <<); -FUNCTOR_BINARY_OPERATOR_A_IMPL(TBitRsh, >>); - -FUNCTOR_BINARY_OPERATOR_B_IMPL(TLogicalAnd, &&); -FUNCTOR_BINARY_OPERATOR_B_IMPL(TLogicalOr, ||); -FUNCTOR_UNARY_OPERATOR_B_IMPL (TLogicalNot, ! ); - -FUNCTOR_BINARY_OPERATOR_C_IMPL(TEqualTo, ==); -FUNCTOR_BINARY_OPERATOR_C_IMPL(TNotEqualTo, !=); -FUNCTOR_BINARY_OPERATOR_D_IMPL(TGreater, > ); -FUNCTOR_BINARY_OPERATOR_D_IMPL(TLess, < ); -FUNCTOR_BINARY_OPERATOR_D_IMPL(TGreaterEqual, >=); -FUNCTOR_BINARY_OPERATOR_D_IMPL(TLessEqual, <=); - -#undef FUNCTOR_BINARY_OPERATOR_D_IMPL -#undef FUNCTOR_BINARY_OPERATOR_C_IMPL - -#undef FUNCTOR_BINARY_OPERATOR_B_IMPL -#undef FUNCTOR_UNARY_OPERATOR_B_IMPL - -#undef FUNCTOR_BINARY_OPERATOR_A_IMPL -#undef FUNCTOR_UNARY_OPERATOR_A_IMPL - -#undef FUNCTOR_BINARY_OPERATOR_IMPL -#undef FUNCTOR_UNARY_OPERATOR_IMPL - NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END