From b1369582e105f7c07309727cf1cf211dbc2773a6 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 2 Dec 2020 15:49:05 +0100 Subject: [PATCH] type_traits: added value_list --- src/entt/core/type_traits.hpp | 68 ++++++++++++++++++++++++++++++++++ test/entt/core/type_traits.cpp | 13 +++++++ 2 files changed, 81 insertions(+) diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp index de7970be8..b1e5deee8 100644 --- a/src/entt/core/type_traits.hpp +++ b/src/entt/core/type_traits.hpp @@ -229,6 +229,74 @@ template inline constexpr auto type_list_contains_v = type_list_contains::value; +/** + * @brief A class to use to push around lists of constant values, nothing more. + * @tparam Value Values provided by the value list. + */ +template +struct value_list { + /*! @brief Value list type. */ + using type = value_list; + /*! @brief Compile-time number of elements in the value list. */ + static constexpr auto size = sizeof...(Value); +}; + + +/** + * @brief Concatenates multiple value lists. + * @tparam Value Values provided by the first value list. + * @tparam Other Values provided by the second value list. + * @return A value list composed by the values of both the value lists. + */ +template +constexpr value_list operator+(value_list, value_list) { return {}; } + + +/*! @brief Primary template isn't defined on purpose. */ +template +struct value_list_cat; + + +/*! @brief Concatenates multiple value lists. */ +template<> +struct value_list_cat<> { + /*! @brief A value list composed by the values of all the value lists. */ + using type = value_list<>; +}; + + +/** + * @brief Concatenates multiple value lists. + * @tparam Value Values provided by the first value list. + * @tparam Other Values provided by the second value list. + * @tparam List Other value lists, if any. + */ +template +struct value_list_cat, value_list, List...> { + /*! @brief A value list composed by the values of all the value lists. */ + using type = typename value_list_cat, List...>::type; +}; + + +/** + * @brief Concatenates multiple value lists. + * @tparam Value Values provided by the value list. + */ +template +struct value_list_cat> { + /*! @brief A value list composed by the values of all the value lists. */ + using type = value_list; +}; + + +/** + * @brief Helper type. + * @tparam List Value lists to concatenate. + */ +template +using value_list_cat_t = typename value_list_cat::type; + + /** * @brief Provides the member constant `value` to true if a given type is * equality comparable, false otherwise. diff --git a/test/entt/core/type_traits.cpp b/test/entt/core/type_traits.cpp index 7afd49ea7..c3c514e1a 100644 --- a/test/entt/core/type_traits.cpp +++ b/test/entt/core/type_traits.cpp @@ -56,6 +56,19 @@ TEST(TypeTraits, TypeList) { static_assert(!entt::type_list_contains_v); } +TEST(TypeTraits, ValueList) { + using value = entt::value_list<0, 2>; + using other = entt::value_list<1>; + + static_assert(value::size == 2u); + static_assert(other::size == 1u); + + static_assert(std::is_same_v>); + 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>>); +} + TEST(TypeTraits, IsEqualityComparable) { static_assert(entt::is_equality_comparable_v); static_assert(!entt::is_equality_comparable_v);