瀏覽代碼

type_traits: added type_list_diff[_t]

Michele Caini 5 年之前
父節點
當前提交
446c8df3b4
共有 2 個文件被更改,包括 31 次插入0 次删除
  1. 25 0
      src/entt/core/type_traits.hpp
  2. 6 0
      test/entt/core/type_traits.cpp

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

@@ -290,6 +290,31 @@ template<class List, typename Type>
 inline constexpr auto type_list_contains_v = type_list_contains<List, Type>::value;
 
 
+/*! @brief Primary template isn't defined on purpose. */
+template<typename...>
+struct type_list_diff;
+
+
+/**
+ * @brief Computes the difference between two type lists.
+ * @tparam Type Types provided by the first type list.
+ * @tparam Other Types provided by the second type list.
+ */
+template<typename... Type, typename... Other>
+struct type_list_diff<type_list<Type...>, type_list<Other...>> {
+    /*! @brief A type list that is the difference between the two type lists. */
+    using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
+};
+
+
+/**
+ * @brief Helper type.
+ * @tparam List Type lists between which to compute the difference.
+ */
+template<typename... List>
+using type_list_diff_t = typename type_list_diff<List...>::type;
+
+
 /**
  * @brief A class to use to push around lists of constant values, nothing more.
  * @tparam Value Values provided by the value list.

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

@@ -64,6 +64,12 @@ TEST(TypeTraits, TypeList) {
     static_assert(std::is_same_v<entt::type_list_element_t<0u, type>, int>);
     static_assert(std::is_same_v<entt::type_list_element_t<1u, type>, char>);
     static_assert(std::is_same_v<entt::type_list_element_t<0u, other>, double>);
+
+    static_assert(std::is_same_v<entt::type_list_diff_t<entt::type_list<int, char, double>, entt::type_list<float, bool>>, entt::type_list<int, char, double>>);
+    static_assert(std::is_same_v<entt::type_list_diff_t<entt::type_list<int, char, double>, entt::type_list<int, char, double>>, entt::type_list<>>);
+    static_assert(std::is_same_v<entt::type_list_diff_t<entt::type_list<int, char, double>, entt::type_list<int, char>>, entt::type_list<double>>);
+    static_assert(std::is_same_v<entt::type_list_diff_t<entt::type_list<int, char, double>, entt::type_list<char, double>>, entt::type_list<int>>);
+    static_assert(std::is_same_v<entt::type_list_diff_t<entt::type_list<int, char, double>, entt::type_list<char>>, entt::type_list<int, double>>);
 }
 
 TEST(TypeTraits, ValueList) {