type_traits: std::tuple traits specialization for entt::type_list and entt::value_list (#1011)

This commit is contained in:
Dominic Koepke
2023-05-23 13:56:29 +02:00
committed by GitHub
parent 325ca310d3
commit 85bffb7143
2 changed files with 48 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
#include <cstddef>
#include <iterator>
#include <tuple>
#include <type_traits>
#include <utility>
#include "../config/config.h"
@@ -387,8 +388,19 @@ template<auto Value, auto... Other>
struct value_list_element<0u, value_list<Value, Other...>> {
/*! @brief Searched value. */
static constexpr auto value = Value;
/*! @brief Searched type. */
using type = decltype(Value);
};
/**
* @brief Helper type.
* @tparam Index Index of the type to return.
* @tparam List Value list to search into.
*/
template<std::size_t Index, typename List>
using value_list_element_t = typename value_list_element<Index, List>::type;
/**
* @brief Helper type.
* @tparam Index Index of the value to return.
@@ -894,4 +906,16 @@ using nth_argument_t = typename nth_argument<Index, Candidate>::type;
} // namespace entt
template <typename... Type>
struct std::tuple_size<entt::type_list<Type...>>: std::integral_constant<std::size_t, entt::type_list<Type...>::size> {};
template <std::size_t Index, typename... Type>
struct std::tuple_element<Index, entt::type_list<Type...>>: entt::type_list_element<Index, entt::type_list<Type...>> {};
template <auto... Value>
struct std::tuple_size<entt::value_list<Value...>>: std::integral_constant<std::size_t, entt::value_list<Value...>::size> {};
template <std::size_t Index, auto... Value>
struct std::tuple_element<Index, entt::value_list<Value...>>: entt::value_list_element<Index, entt::value_list<Value...>> {};
#endif

View File

@@ -238,3 +238,27 @@ TEST(Tag, Functionalities) {
static_assert(entt::tag<"foobar"_hs>::value == entt::hashed_string::value("foobar"));
static_assert(std::is_same_v<typename entt::tag<"foobar"_hs>::value_type, entt::id_type>);
}
TEST(TypeList, TupleSize) {
ASSERT_EQ(std::tuple_size_v<entt::type_list<>>, 0u);
ASSERT_EQ(std::tuple_size_v<entt::type_list<int>>, 1u);
ASSERT_EQ((std::tuple_size_v<entt::type_list<int, float>>), 2u);
}
TEST(TypeList, TupleElement) {
ASSERT_TRUE((std::is_same_v<int, std::tuple_element_t<0, entt::type_list<int>>>));
ASSERT_TRUE((std::is_same_v<int, std::tuple_element_t<0, entt::type_list<int, float>>>));
ASSERT_TRUE((std::is_same_v<float, std::tuple_element_t<1, entt::type_list<int, float>>>));
}
TEST(ValueList, TupleSize) {
ASSERT_EQ(std::tuple_size_v<entt::value_list<>>, 0u);
ASSERT_EQ(std::tuple_size_v<entt::value_list<42>>, 1u);
ASSERT_EQ((std::tuple_size_v<entt::value_list<42, 'a'>>), 2u);
}
TEST(ValueList, TupleElement) {
ASSERT_TRUE((std::is_same_v<int, std::tuple_element_t<0, entt::value_list<42>>>));
ASSERT_TRUE((std::is_same_v<int, std::tuple_element_t<0, entt::value_list<42, 'a'>>>));
ASSERT_TRUE((std::is_same_v<char, std::tuple_element_t<1, entt::value_list<42, 'a'>>>));
}