refactor(templates): remove the predefined functors, Lambda is a better choice

This commit is contained in:
_Redstone_c_ 2022-04-24 22:54:08 +08:00
parent c2eca8df1e
commit f72d6a53d8
2 changed files with 1 additions and 229 deletions

View File

@ -1158,88 +1158,13 @@ void TestFunction()
} }
{ {
TFunction<bool(bool)> Identity = TIdentity<>(); TFunction<bool(bool)> Identity = [](bool In) { return In; };
TFunction<bool(bool)> NotIdentity = NotFn(Identity); TFunction<bool(bool)> NotIdentity = NotFn(Identity);
always_check(Identity(true)); always_check(Identity(true));
always_check(NotIdentity(false)); always_check(NotIdentity(false));
} }
{
always_check(TPromote <int32>()(4 ) == 4);
always_check(TNegate <int32>()(4 ) == -4);
always_check(TPlus <int32>()(4, 2) == 6);
always_check(TMinus <int32>()(4, 2) == 2);
always_check(TMultiplies<int32>()(4, 2) == 8);
always_check(TDivides <int32>()(4, 2) == 2);
always_check(TModulus <int32>()(4, 2) == 0);
always_check(TBitNot<int32>()(4 ) == -5);
always_check(TBitAnd<int32>()(4, 2) == 0);
always_check(TBitOr <int32>()(4, 2) == 6);
always_check(TBitXor<int32>()(4, 2) == 6);
always_check(TBitLsh<int32>()(4, 2) == 16);
always_check(TBitRsh<int32>()(4, 2) == 1);
always_check(TLogicalAnd<int32>()(4, 2) == true);
always_check(TLogicalOr <int32>()(4, 2) == true);
always_check(TLogicalNot<int32>()(4 ) == false);
always_check(TEqualTo <int32>()(4, 2) == false);
always_check(TNotEqualTo <int32>()(4, 2) == true);
always_check(TGreater <int32>()(4, 2) == true);
always_check(TLess <int32>()(4, 2) == false);
always_check(TGreaterEqual<int32>()(4, 2) == true);
always_check(TLessEqual <int32>()(4, 2) == false);
}
{
TFunction<int32(int32, int32)> TempA = TPlus <>();
TFunction<int32(int32, int32)> TempB = TMinus <>();
TFunction<int32(int32, int32)> TempC = TMultiplies<>();
TFunction<int32(int32, int32)> TempD = TDivides <>();
TFunction<int32(int32, int32)> TempE = TModulus <>();
TFunction<int32(int32 )> 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<bool(int32, int32)> TempG = TEqualTo <>();
TFunction<bool(int32, int32)> TempH = TNotEqualTo <>();
TFunction<bool(int32, int32)> TempI = TGreater <>();
TFunction<bool(int32, int32)> TempJ = TLess <>();
TFunction<bool(int32, int32)> TempK = TGreaterEqual<>();
TFunction<bool(int32, int32)> 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<bool(int32, int32)> TempM = TLogicalAnd<>();
TFunction<bool(int32, int32)> TempN = TLogicalOr <>();
TFunction<bool(int32 )> TempO = TLogicalNot<>();
always_check(TempM(4, 2) == true);
always_check(TempN(4, 2) == true);
always_check(TempO(4 ) == false);
TFunction<int32(int32, int32)> TempP = TBitAnd<>();
TFunction<int32(int32, int32)> TempQ = TBitOr <>();
TFunction<int32(int32, int32)> TempR = TBitXor<>();
TFunction<int32(int32 )> 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() void TestAtomic()

View File

@ -403,29 +403,6 @@ constexpr bool operator==(const TUniqueFunction<F>& LHS, nullptr_t)
static_assert(sizeof(TFunction<void()>) == 64, "The byte size of TFunction is unexpected"); static_assert(sizeof(TFunction<void()>) == 64, "The byte size of TFunction is unexpected");
static_assert(sizeof(TUniqueFunction<void()>) == 64, "The byte size of TUniqueFunction is unexpected"); static_assert(sizeof(TUniqueFunction<void()>) == 64, "The byte size of TUniqueFunction is unexpected");
template <typename T = void>
struct TIdentity
{
using Type = T;
constexpr T&& operator()(T&& InValue) const
{
return Forward<T>(InValue);
}
};
template <>
struct TIdentity<void>
{
using Type = void;
template<typename T>
constexpr T&& operator()(T&& InValue) const
{
return Forward<T>(InValue);
}
};
NAMESPACE_PRIVATE_BEGIN NAMESPACE_PRIVATE_BEGIN
template <typename F> template <typename F>
@ -476,136 +453,6 @@ constexpr NAMESPACE_PRIVATE::NotFunctionType<typename TDecay<F>::Type> NotFn(F&&
return NAMESPACE_PRIVATE::NotFunctionType<typename TDecay<F>::Type>(Forward<F>(Func)); return NAMESPACE_PRIVATE::NotFunctionType<typename TDecay<F>::Type>(Forward<F>(Func));
} }
#define FUNCTOR_UNARY_OPERATOR_IMPL(Name, Operator, ConceptT, ConceptU) \
template <typename T = void> requires (CSameAs<T, void> || ConceptT) \
struct Name \
{ \
constexpr auto operator()(const T& InValue) const \
-> decltype(Operator InValue) \
{ \
return Operator InValue; \
} \
}; \
\
template <> \
struct Name<void> \
{ \
template <typename U> requires ConceptU \
constexpr auto operator()(U&& InValue) const \
-> decltype(Operator Forward<U>(InValue)) \
{ \
return Operator Forward<U>(InValue); \
} \
}
#define FUNCTOR_BINARY_OPERATOR_IMPL(Name, Operator, ConceptT, ConceptTU) \
template <typename T = void> requires (CSameAs<T, void> || ConceptT) \
struct Name \
{ \
constexpr auto operator()(const T& LHS, const T& RHS) const \
-> decltype(LHS Operator RHS) \
{ \
return LHS Operator RHS; \
} \
}; \
\
template <> \
struct Name<void> \
{ \
template <typename T, typename U> requires ConceptTU \
constexpr auto operator()(T&& LHS, U&& RHS) const \
-> decltype(Forward<T>(LHS) Operator Forward<U>(RHS)) \
{ \
return Forward<T>(LHS) Operator Forward<U>(RHS); \
} \
}
#define FUNCTOR_UNARY_OPERATOR_A_IMPL(Name, Operator) \
FUNCTOR_UNARY_OPERATOR_IMPL \
( \
Name, Operator, \
(requires(const T& InValue) { { Operator InValue } -> CConvertibleTo<T>; }), \
(requires(U&& InValue) { Operator Forward<U>(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<T>; }), \
(requires(T&& LHS, U&& RHS) { Forward<T>(LHS) Operator Forward<U>(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<U>(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<T>(LHS) Operator Forward<U>(RHS) } -> CBooleanTestable; }) \
)
#define FUNCTOR_BINARY_OPERATOR_C_IMPL(Name, Operator) \
FUNCTOR_BINARY_OPERATOR_IMPL \
( \
Name, Operator, \
(CEqualityComparable<T>), \
(CEqualityComparableWith<T, U>) \
)
#define FUNCTOR_BINARY_OPERATOR_D_IMPL(Name, Operator) \
FUNCTOR_BINARY_OPERATOR_IMPL \
( \
Name, Operator, \
(CTotallyOrdered<T>), \
(CTotallyOrderedWith<T, U>) \
)
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(Utility)
NAMESPACE_MODULE_END(Redcraft) NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END NAMESPACE_REDCRAFT_END