type_traits: type based nth_argument[_t]

This commit is contained in:
Michele Caini
2023-11-09 14:50:00 +01:00
parent 6aa4c770e6
commit f4a54cc0da
3 changed files with 20 additions and 15 deletions

View File

@@ -796,12 +796,12 @@ template<typename Type>
} else if constexpr(is_complete_v<std::tuple_size<std::remove_cv_t<Type>>>) {
if constexpr(has_tuple_size_value<Type>::value) {
return maybe_equality_comparable<Type>(0) && unpack_maybe_equality_comparable<Type>(std::make_index_sequence<std::tuple_size<Type>::value>{});
} else {
return maybe_equality_comparable<Type>(0);
}
} else {
return maybe_equality_comparable<Type>(0);
}
} else {
return maybe_equality_comparable<Type>(0);
}
}
} // namespace internal
@@ -888,9 +888,9 @@ using member_class_t = typename member_class<Member>::type;
/**
* @brief Extracts the n-th argument of a given function or member function.
* @tparam Index The index of the argument to extract.
* @tparam Candidate A valid function, member function or data member.
* @tparam Candidate A valid function, member function or data member type.
*/
template<std::size_t Index, auto Candidate>
template<std::size_t Index, typename Candidate>
class nth_argument {
template<typename Ret, typename... Args>
static constexpr type_list<Args...> pick_up(Ret (*)(Args...));
@@ -906,15 +906,15 @@ class nth_argument {
public:
/*! @brief N-th argument of the given function or member function. */
using type = type_list_element_t<Index, decltype(pick_up(Candidate))>;
using type = type_list_element_t<Index, decltype(pick_up(std::declval<Candidate>()))>;
};
/**
* @brief Helper type.
* @tparam Index The index of the argument to extract.
* @tparam Candidate A valid function, member function or data member.
* @tparam Candidate A valid function, member function or data member type.
*/
template<std::size_t Index, auto Candidate>
template<std::size_t Index, typename Candidate>
using nth_argument_t = typename nth_argument<Index, Candidate>::type;
} // namespace entt

View File

@@ -104,7 +104,7 @@ private:
* @param reg A registry that contains the given entity and its components.
* @param entt Entity from which to get the component.
*/
template<auto Member, typename Registry = std::decay_t<nth_argument_t<0u, Member>>>
template<auto Member, typename Registry = std::decay_t<nth_argument_t<0u, decltype(Member)>>>
void invoke(Registry &reg, const typename Registry::entity_type entt) {
static_assert(std::is_member_function_pointer_v<decltype(Member)>, "Invalid pointer to non-static member function");
delegate<void(Registry &, const typename Registry::entity_type)> func;

View File

@@ -240,13 +240,18 @@ TEST(MemberClass, Functionalities) {
}
TEST(NthArgument, Functionalities) {
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, &free_function>, int>();
testing::StaticAssertTypeEq<entt::nth_argument_t<1u, &free_function>, const double &>();
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, &clazz::bar>, double>();
testing::StaticAssertTypeEq<entt::nth_argument_t<1u, &clazz::bar>, float>();
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, &clazz::quux>, bool>();
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, void(int, char, bool)>, int>();
testing::StaticAssertTypeEq<entt::nth_argument_t<1u, void(int, char, bool)>, char>();
testing::StaticAssertTypeEq<entt::nth_argument_t<2u, void(int, char, bool)>, bool>();
ASSERT_EQ(free_function(entt::nth_argument_t<0u, &free_function>{}, entt::nth_argument_t<1u, &free_function>{}), 42);
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, decltype(&free_function)>, int>();
testing::StaticAssertTypeEq<entt::nth_argument_t<1u, decltype(&free_function)>, const double &>();
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, decltype(&clazz::bar)>, double>();
testing::StaticAssertTypeEq<entt::nth_argument_t<1u, decltype(&clazz::bar)>, float>();
testing::StaticAssertTypeEq<entt::nth_argument_t<0u, decltype(&clazz::quux)>, bool>();
ASSERT_EQ(free_function(entt::nth_argument_t<0u, decltype(&free_function)>{}, entt::nth_argument_t<1u, decltype(&free_function)>{}), 42);
}
TEST(Tag, Functionalities) {