From 9950b2ea348ea6ce8da5abaed4c03ced6cc85a47 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 15 Dec 2020 12:20:20 +0100 Subject: [PATCH] type_traits: added type_list_element[_t] and value_list_element[_v] --- src/entt/core/type_traits.hpp | 76 ++++++++++++++++++++++++++++++++++ test/entt/core/type_traits.cpp | 8 ++++ 2 files changed, 84 insertions(+) diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp index 5caa6994c..635a93354 100644 --- a/src/entt/core/type_traits.hpp +++ b/src/entt/core/type_traits.hpp @@ -132,6 +132,44 @@ struct type_list { }; +/*! @brief Primary template isn't defined on purpose. */ +template +struct type_list_element; + + +/** + * @brief Provides compile-time indexed access to the types of a type list. + * @tparam Index Index of the type to return. + * @tparam Type First type provided by the type list. + * @tparam Other Other types provided by the type list. + */ +template +struct type_list_element> + : type_list_element> +{}; + + +/** + * @brief Provides compile-time indexed access to the types of a type list. + * @tparam Type First type provided by the type list. + * @tparam Other Other types provided by the type list. + */ +template +struct type_list_element<0u, type_list> { + /*! @brief Searched type. */ + using type = Type; +}; + + +/** + * @brief Helper type. + * @tparam Index Index of the type to return. + * @tparam List Type list to search into. + */ +template +using type_list_element_t = typename type_list_element::type; + + /** * @brief Concatenates multiple type lists. * @tparam Type Types provided by the first type list. @@ -265,6 +303,44 @@ struct value_list { }; +/*! @brief Primary template isn't defined on purpose. */ +template +struct value_list_element; + + +/** + * @brief Provides compile-time indexed access to the values of a value list. + * @tparam Index Index of the value to return. + * @tparam Value First value provided by the value list. + * @tparam Other Other values provided by the value list. + */ +template +struct value_list_element> + : value_list_element> +{}; + + +/** + * @brief Provides compile-time indexed access to the types of a type list. + * @tparam Value First value provided by the value list. + * @tparam Other Other values provided by the value list. + */ +template +struct value_list_element<0u, value_list> { + /*! @brief Searched value. */ + static constexpr auto value = Value; +}; + + +/** + * @brief Helper type. + * @tparam Index Index of the value to return. + * @tparam List Value list to search into. + */ +template +inline constexpr auto value_list_element_v = value_list_element::value; + + /** * @brief Concatenates multiple value lists. * @tparam Value Values provided by the first value list. diff --git a/test/entt/core/type_traits.cpp b/test/entt/core/type_traits.cpp index c3c514e1a..fdf69adeb 100644 --- a/test/entt/core/type_traits.cpp +++ b/test/entt/core/type_traits.cpp @@ -54,6 +54,10 @@ TEST(TypeTraits, TypeList) { static_assert(entt::type_list_contains_v); static_assert(entt::type_list_contains_v); static_assert(!entt::type_list_contains_v); + + static_assert(std::is_same_v, int>); + static_assert(std::is_same_v, char>); + static_assert(std::is_same_v, double>); } TEST(TypeTraits, ValueList) { @@ -67,6 +71,10 @@ TEST(TypeTraits, ValueList) { static_assert(std::is_same_v, entt::value_list<0, 2, 1, 0, 2, 1>>); static_assert(std::is_same_v, entt::value_list<0, 2, 1>>); static_assert(std::is_same_v, entt::value_list<0, 2, 0, 2>>); + + static_assert(entt::value_list_element_v<0u, value> == 0); + static_assert(entt::value_list_element_v<1u, value> == 2); + static_assert(entt::value_list_element_v<0u, other> == 1); } TEST(TypeTraits, IsEqualityComparable) {