Browse Source

type_traits: value_list_index[_v]

Michele Caini 3 years ago
parent
commit
8c60faa1d0
2 changed files with 56 additions and 0 deletions
  1. 52 0
      src/entt/core/type_traits.hpp
  2. 4 0
      test/entt/core/type_traits.cpp

+ 52 - 0
src/entt/core/type_traits.hpp

@@ -397,6 +397,58 @@ struct value_list_element<0u, value_list<Value, Other...>> {
 template<std::size_t Index, typename List>
 inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
 
+/*! @brief Primary template isn't defined on purpose. */
+template<auto, typename>
+struct value_list_index;
+
+/**
+ * @brief Provides compile-time type access to the values of a value list.
+ * @tparam Value Value to look for and for which to return the index.
+ * @tparam First First value provided by the value list.
+ * @tparam Other Other values provided by the value list.
+ */
+template<auto Value, auto First, auto... Other>
+struct value_list_index<Value, value_list<First, Other...>> {
+    /*! @brief Unsigned integer type. */
+    using value_type = std::size_t;
+    /*! @brief Compile-time position of the given value in the sublist. */
+    static constexpr value_type value = 1u + value_list_index<Value, value_list<Other...>>::value;
+};
+
+/**
+ * @brief Provides compile-time type access to the values of a value list.
+ * @tparam Value Value to look for and for which to return the index.
+ * @tparam Other Other values provided by the value list.
+ */
+template<auto Value, auto... Other>
+struct value_list_index<Value, value_list<Value, Other...>> {
+    static_assert(value_list_index<Value, value_list<Other...>>::value == sizeof...(Other), "Non-unique type");
+    /*! @brief Unsigned integer type. */
+    using value_type = std::size_t;
+    /*! @brief Compile-time position of the given value in the sublist. */
+    static constexpr value_type value = 0u;
+};
+
+/**
+ * @brief Provides compile-time type access to the values of a value list.
+ * @tparam Value Value to look for and for which to return the index.
+ */
+template<auto Value>
+struct value_list_index<Value, value_list<>> {
+    /*! @brief Unsigned integer type. */
+    using value_type = std::size_t;
+    /*! @brief Compile-time position of the given type in the sublist. */
+    static constexpr value_type value = 0u;
+};
+
+/**
+ * @brief Helper variable template.
+ * @tparam List Value list.
+ * @tparam Value Value to look for and for which to return the index.
+ */
+template<auto Value, typename List>
+inline constexpr std::size_t value_list_index_v = value_list_index<Value, List>::value;
+
 /**
  * @brief Concatenates multiple value lists.
  * @tparam Value Values provided by the first value list.

+ 4 - 0
test/entt/core/type_traits.cpp

@@ -129,6 +129,10 @@ TEST(ValueList, Functionalities) {
     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);
+
+    static_assert(entt::value_list_index_v<0, value> == 0u);
+    static_assert(entt::value_list_index_v<2, value> == 1u);
+    static_assert(entt::value_list_index_v<1, other> == 0u);
 }
 
 TEST(IsApplicable, Functionalities) {