Explorar el Código

type_traits: is_applicable[_v] and is_applicable_r[_v] for tuples

Michele Caini hace 5 años
padre
commit
26c816e78c
Se han modificado 2 ficheros con 60 adiciones y 5 borrados
  1. 50 0
      src/entt/core/type_traits.hpp
  2. 10 5
      test/entt/core/type_traits.cpp

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

@@ -253,6 +253,56 @@ template<class Type>
 inline constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
 
 
+/*! @brief Same as std::is_invocable, but with tuples. */
+template<typename, typename>
+struct is_applicable: std::false_type {};
+
+
+/**
+ * @copybrief is_applicable
+ * @tparam Func A valid function type.
+ * @tparam Args The list of arguments to use to probe the function type.
+ */
+template<typename Func, typename... Args>
+struct is_applicable<Func, std::tuple<Args...>>: std::is_invocable<Func, Args...> {};
+
+
+/**
+ * @brief Helper variable template.
+ * @tparam Func A valid function type.
+ * @tparam Args The list of arguments to use to probe the function type.
+ */
+template<typename Func, typename Args>
+inline constexpr auto is_applicable_v = is_applicable<Func, Args>::value;
+
+
+/*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
+template<typename, typename, typename>
+struct is_applicable_r: std::false_type {};
+
+
+/**
+ * @copybrief is_applicable_r
+ * @tparam Ret The type to which the return type of the function should be
+ * convertible.
+ * @tparam Func A valid function type.
+ * @tparam Args The list of arguments to use to probe the function type.
+ */
+template<typename Ret, typename Func, typename... Args>
+struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
+
+
+/**
+ * @brief Helper variable template.
+ * @tparam Ret The type to which the return type of the function should be
+ * convertible.
+ * @tparam Func A valid function type.
+ * @tparam Args The list of arguments to use to probe the function type.
+ */
+template<typename Ret, typename Func, typename Args>
+inline constexpr auto is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
+
+
 /**
  * @brief Provides the member constant `value` to true if a given type is empty
  * and the empty type optimization is enabled, false otherwise.

+ 10 - 5
test/entt/core/type_traits.cpp

@@ -1,9 +1,5 @@
-#include <array>
-#include <map>
-#include <memory>
-#include <set>
+#include <tuple>
 #include <type_traits>
-#include <vector>
 #include <gtest/gtest.h>
 #include <entt/config/config.h>
 #include <entt/core/hashed_string.hpp>
@@ -65,6 +61,15 @@ TEST(TypeTraits, IsEqualityComparable) {
     static_assert(!entt::is_equality_comparable_v<void>);
 }
 
+TEST(TypeTraits, IsApplicable) {
+    static_assert(entt::is_applicable_v<void(int, char), std::tuple<double, char>>);
+    static_assert(!entt::is_applicable_v<void(int, char), std::tuple<int>>);
+
+    static_assert(entt::is_applicable_r_v<float, int(int, char), std::tuple<double, char>>);
+    static_assert(!entt::is_applicable_r_v<float, void(int, char), std::tuple<double, char>>);
+    static_assert(!entt::is_applicable_r_v<int, int(int, char), std::tuple<void>>);
+}
+
 TEST(TypeTraits, MemberClass) {
     struct clazz {
         char foo(int) { return {}; }