Răsfoiți Sursa

type_traits: value_list_contains[_v]

Michele Caini 3 ani în urmă
părinte
comite
b272e04bab
2 a modificat fișierele cu 30 adăugiri și 0 ștergeri
  1. 26 0
      src/entt/core/type_traits.hpp
  2. 4 0
      test/entt/core/type_traits.cpp

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

@@ -532,6 +532,32 @@ struct value_list_unique<value_list<>> {
 template<typename Type>
 template<typename Type>
 using value_list_unique_t = typename value_list_unique<Type>::type;
 using value_list_unique_t = typename value_list_unique<Type>::type;
 
 
+/**
+ * @brief Provides the member constant `value` to true if a value list contains
+ * a given value, false otherwise.
+ * @tparam List Value list.
+ * @tparam Value Value to look for.
+ */
+template<typename List, auto Value>
+struct value_list_contains;
+
+/**
+ * @copybrief value_list_contains
+ * @tparam Value Values provided by the value list.
+ * @tparam Other Value to look for.
+ */
+template<auto... Value, auto Other>
+struct value_list_contains<value_list<Value...>, Other>
+    : std::bool_constant<((Value == Other) || ...)> {};
+
+/**
+ * @brief Helper variable template.
+ * @tparam List Value list.
+ * @tparam Value Value to look for.
+ */
+template<typename List, auto Value>
+inline constexpr bool value_list_contains_v = value_list_contains<List, Value>::value;
+
 /*! @brief Same as std::is_invocable, but with tuples. */
 /*! @brief Same as std::is_invocable, but with tuples. */
 template<typename, typename>
 template<typename, typename>
 struct is_applicable: std::false_type {};
 struct is_applicable: std::false_type {};

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

@@ -127,6 +127,10 @@ TEST(ValueList, Functionalities) {
     static_assert(std::is_same_v<entt::value_list_cat_t<value, value>, entt::value_list<0, 2, 0, 2>>);
     static_assert(std::is_same_v<entt::value_list_cat_t<value, value>, entt::value_list<0, 2, 0, 2>>);
     static_assert(std::is_same_v<entt::value_list_unique_t<entt::value_list_cat_t<value, value>>, value>);
     static_assert(std::is_same_v<entt::value_list_unique_t<entt::value_list_cat_t<value, value>>, value>);
 
 
+    static_assert(entt::value_list_contains_v<value, 0>);
+    static_assert(entt::value_list_contains_v<value, 2>);
+    static_assert(!entt::value_list_contains_v<value, 1>);
+
     static_assert(entt::value_list_element_v<0u, value> == 0);
     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<1u, value> == 2);
     static_assert(entt::value_list_element_v<0u, other> == 1);
     static_assert(entt::value_list_element_v<0u, other> == 1);