Przeglądaj źródła

type traits: added type_list_contains[_v]

Michele Caini 5 lat temu
rodzic
commit
a86191f6e0
2 zmienionych plików z 50 dodań i 17 usunięć
  1. 28 0
      src/entt/core/type_traits.hpp
  2. 22 17
      test/entt/core/type_traits.cpp

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

@@ -182,6 +182,34 @@ template<typename Type>
 using type_list_unique_t = typename type_list_unique<Type>::type;
 
 
+/**
+ * @brief Provides the member constant `value` to true if a type list contains a
+ * given type, false otherwise.
+ * @tparam List Type list.
+ * @tparam Type Type to look for.
+ */
+template<typename List, typename Type>
+struct type_list_contains;
+
+
+/**
+* @copybrief type_list_contains
+* @tparam Type Types provided by the type list.
+* @tparam Other Type to look for.
+*/
+template<typename... Type, typename Other>
+struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
+
+
+/**
+* @brief Helper variable template.
+* @tparam List Type list.
+* @tparam Type Type to look for.
+*/
+template<class List, typename Type>
+inline constexpr auto type_list_contains_v = type_list_contains<List, Type>::value;
+
+
 /**
  * @brief Provides the member constant `value` to true if a given type is
  * equality comparable, false otherwise.

+ 22 - 17
test/entt/core/type_traits.cpp

@@ -26,30 +26,35 @@ TEST(TypeTraits, UnpackAsValue) {
 TEST(TypeTraits, IntegralConstant) {
     entt::integral_constant<3> constant;
 
-    ASSERT_TRUE((std::is_same_v<typename entt::integral_constant<3>::value_type, int>));
-    ASSERT_EQ(constant.value, 3);
+    static_assert(std::is_same_v<typename entt::integral_constant<3>::value_type, int>);
+    static_assert(constant.value == 3);
 }
 
 TEST(TypeTraits, Choice) {
-    ASSERT_TRUE((std::is_base_of_v<entt::choice_t<0>, entt::choice_t<1>>));
-    ASSERT_FALSE((std::is_base_of_v<entt::choice_t<1>, entt::choice_t<0>>));
+    static_assert(std::is_base_of_v<entt::choice_t<0>, entt::choice_t<1>>);
+    static_assert(!std::is_base_of_v<entt::choice_t<1>, entt::choice_t<0>>);
 }
 
 TEST(TypeTraits, TypeList) {
     using type = entt::type_list<int, char>;
     using other = entt::type_list<double>;
 
-    ASSERT_EQ(entt::type_list_size_v<type>, 2u);
-    ASSERT_EQ(entt::type_list_size_v<other>, 1u);
-    ASSERT_TRUE((std::is_same_v<entt::type_list_cat_t<type, other, type, other>, entt::type_list<int, char, double, int, char, double>>));
-    ASSERT_TRUE((std::is_same_v<entt::type_list_cat_t<type, other>, entt::type_list<int, char, double>>));
-    ASSERT_TRUE((std::is_same_v<entt::type_list_cat_t<type, type>, entt::type_list<int, char, int, char>>));
-    ASSERT_TRUE((std::is_same_v<entt::type_list_unique_t<entt::type_list_cat_t<type, type>>, entt::type_list<int, char>>));
+    static_assert(entt::type_list_size_v<type> == 2u);
+    static_assert(entt::type_list_size_v<other> == 1u);
+
+    static_assert(std::is_same_v<entt::type_list_cat_t<type, other, type, other>, entt::type_list<int, char, double, int, char, double>>);
+    static_assert(std::is_same_v<entt::type_list_cat_t<type, other>, entt::type_list<int, char, double>>);
+    static_assert(std::is_same_v<entt::type_list_cat_t<type, type>, entt::type_list<int, char, int, char>>);
+    static_assert(std::is_same_v<entt::type_list_unique_t<entt::type_list_cat_t<type, type>>, entt::type_list<int, char>>);
+
+    static_assert(entt::type_list_contains_v<type, int>);
+    static_assert(entt::type_list_contains_v<type, char>);
+    static_assert(!entt::type_list_contains_v<type, double>);
 }
 
 TEST(TypeTraits, IsEqualityComparable) {
-    ASSERT_TRUE(entt::is_equality_comparable_v<int>);
-    ASSERT_FALSE(entt::is_equality_comparable_v<void>);
+    static_assert(entt::is_equality_comparable_v<int>);
+    static_assert(!entt::is_equality_comparable_v<void>);
 }
 
 TEST(TypeTraits, MemberClass) {
@@ -59,12 +64,12 @@ TEST(TypeTraits, MemberClass) {
         bool quux;
     };
 
-    ASSERT_TRUE((std::is_same_v<clazz, entt::member_class_t<decltype(&clazz::foo)>>));
-    ASSERT_TRUE((std::is_same_v<clazz, entt::member_class_t<decltype(&clazz::bar)>>));
-    ASSERT_TRUE((std::is_same_v<clazz, entt::member_class_t<decltype(&clazz::quux)>>));
+    static_assert(std::is_same_v<clazz, entt::member_class_t<decltype(&clazz::foo)>>);
+    static_assert(std::is_same_v<clazz, entt::member_class_t<decltype(&clazz::bar)>>);
+    static_assert(std::is_same_v<clazz, entt::member_class_t<decltype(&clazz::quux)>>);
 }
 
 TEST(TypeTraits, Tag) {
-    ASSERT_EQ(entt::tag<"foobar"_hs>::value, entt::hashed_string::value("foobar"));
-    ASSERT_TRUE((std::is_same_v<typename entt::tag<"foobar"_hs>::value_type, entt::id_type>));
+    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>);
 }