Browse Source

meta: cleanup/review

Michele Caini 4 năm trước cách đây
mục cha
commit
61c1e359e9
2 tập tin đã thay đổi với 99 bổ sung70 xóa
  1. 6 18
      src/entt/meta/factory.hpp
  2. 93 52
      src/entt/meta/utility.hpp

+ 6 - 18
src/entt/meta/factory.hpp

@@ -344,12 +344,8 @@ struct meta_factory<Type> {
             nullptr,
             nullptr,
             descriptor::args_type::size,
-            [](const typename internal::meta_ctor_node::size_type index) ENTT_NOEXCEPT {
-                return meta_arg(typename descriptor::args_type{}, index);
-            },
-            [](meta_any * const args) {
-                return meta_invoke<Type, Candidate, Policy>({}, args, std::make_index_sequence<descriptor::args_type::size>{});
-            }
+            &meta_arg<typename descriptor::args_type>,
+            &meta_construct<Type, Candidate, Policy>
         };
 
         if(!internal::find_if(&node, type->ctor)) {
@@ -380,12 +376,8 @@ struct meta_factory<Type> {
             nullptr,
             nullptr,
             descriptor::args_type::size,
-            [](const typename internal::meta_ctor_node::size_type index) ENTT_NOEXCEPT {
-                return meta_arg(typename descriptor::args_type{}, index);
-            },
-            [](meta_any * const args) {
-                return meta_construct<Type, Args...>(args, std::make_index_sequence<descriptor::args_type::size>{});
-            }
+            &meta_arg<typename descriptor::args_type>,
+            &meta_construct<Type, Args...>
         };
 
         if(!internal::find_if(&node, type->ctor)) {
@@ -544,12 +536,8 @@ struct meta_factory<Type> {
             descriptor::is_const,
             descriptor::is_static,
             &internal::meta_info<std::conditional_t<std::is_same_v<Policy, as_void_t>, void, typename descriptor::return_type>>::resolve,
-            [](const typename internal::meta_func_node::size_type index) ENTT_NOEXCEPT {
-                return meta_arg(typename descriptor::args_type{}, index);
-            },
-            [](meta_handle instance, meta_any * const args) {
-                return meta_invoke<Type, Candidate, Policy>(std::move(instance), args, std::make_index_sequence<descriptor::args_type::size>{});
-            }
+            &meta_arg<typename descriptor::args_type>,
+            &meta_invoke<Type, Candidate, Policy>
         };
 
         for(auto *it = &type->func; *it; it = &(*it)->next) {

+ 93 - 52
src/entt/meta/utility.hpp

@@ -16,6 +16,74 @@
 namespace entt {
 
 
+/**
+ * @cond TURN_OFF_DOXYGEN
+ * Internal details not to be documented.
+ */
+
+
+namespace internal {
+
+
+template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
+[[nodiscard]] std::enable_if_t<!std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke([[maybe_unused]] meta_handle instance, meta_any *args, std::index_sequence<Index...>) {
+    using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
+
+    const auto invoke = [](auto &&maybe_clazz, auto &&... other) {
+        if constexpr(std::is_member_function_pointer_v<decltype(Candidate)>) {
+            if constexpr(std::is_void_v<typename descriptor::return_type>) {
+                (std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...);
+                return meta_any{std::in_place_type<void>};
+            } else {
+                return meta_dispatch<Policy>((std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...));
+            }
+        } else {
+            if constexpr(std::is_void_v<typename descriptor::return_type>) {
+                Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...);
+                return meta_any{std::in_place_type<void>};
+            } else {
+                return meta_dispatch<Policy>(Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...));
+            }
+        }
+    };
+
+    if constexpr(std::is_invocable_v<decltype(Candidate), const Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
+        if(const auto * const clazz = instance->try_cast<const Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
+            return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
+        }
+    } else if constexpr(std::is_invocable_v<decltype(Candidate), Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
+        if(auto * const clazz = instance->try_cast<Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
+            return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
+        }
+    } else {
+        if(((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
+            return invoke((args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
+        }
+    }
+
+    return meta_any{};
+}
+
+
+template<typename Type, typename... Args, std::size_t... Index>
+[[nodiscard]] meta_any meta_construct(meta_any * const args, std::index_sequence<Index...>) {
+    if(((args+Index)->allow_cast<Args>() && ...)) {
+        return Type{(args+Index)->cast<Args>()...};
+    }
+
+    return {};
+}
+
+
+}
+
+
+/**
+ * Internal details not to be documented.
+ * @endcond
+ */
+
+
 /*! @brief Primary template isn't defined on purpose. */
 template<typename, typename>
 struct meta_function_descriptor;
@@ -120,12 +188,12 @@ using meta_function_helper_t = typename meta_function_helper<Type, Candidate>::t
 
 /**
  * @brief Returns the meta type of the i-th element of a list of arguments.
- * @tparam Args Actual types of arguments.
+ * @tparam Type Type list of the actual types of arguments.
  * @return The meta type of the i-th element of the list of arguments.
  */
-template<typename... Args>
-[[nodiscard]] static meta_type meta_arg(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT {
-    return internal::meta_arg_node(type_list<Args...>{}, index);
+template<typename Type>
+[[nodiscard]] static meta_type meta_arg(const std::size_t index) ENTT_NOEXCEPT {
+    return internal::meta_arg_node(Type{}, index);
 }
 
 
@@ -133,17 +201,26 @@ template<typename... Args>
  * @brief Constructs an instance given a list of erased parameters, if possible.
  * @tparam Type Actual type of the instance to construct.
  * @tparam Args Types of arguments expected.
- * @tparam Index Indexes to use to extract erased arguments from their list.
  * @param args Parameters to use to construct the instance.
  * @return A meta any containing the new instance, if any.
  */
-template<typename Type, typename... Args, std::size_t... Index>
-[[nodiscard]] meta_any meta_construct(meta_any * const args, std::index_sequence<Index...>) {
-    if(((args+Index)->allow_cast<Args>() && ...)) {
-        return Type{(args+Index)->cast<Args>()...};
-    }
+template<typename Type, typename... Args>
+[[nodiscard]] meta_any meta_construct(meta_any * const args) {
+    return internal::meta_construct<Type, Args...>(args, std::index_sequence_for<Args...>{});
+}
 
-    return {};
+
+/**
+ * @brief Constructs an instance given a list of erased parameters, if possible.
+ * @tparam Type Reflected type to which the function is associated.
+ * @tparam Candidate The actual function to invoke.
+ * @tparam Policy Optional policy (no policy set by default).
+ * @param args Parameters to use to invoke the function.
+ * @return A meta any containing the returned value, if any.
+ */
+template<typename Type, auto Candidate, typename Policy = as_is_t>
+[[nodiscard]] meta_any meta_construct(meta_any * const args) {
+    return meta_invoke<Type, Candidate, Policy>({}, args);
 }
 
 
@@ -263,48 +340,13 @@ template<typename Type, auto Data, typename Policy = as_is_t>
  * @tparam Type Reflected type to which the function is associated.
  * @tparam Candidate The actual function to invoke.
  * @tparam Policy Optional policy (no policy set by default).
- * @tparam Index Indexes to use to extract erased arguments from their list.
  * @param instance An opaque instance of the underlying type, if required.
  * @param args Parameters to use to invoke the function.
  * @return A meta any containing the returned value, if any.
  */
-template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
-[[nodiscard]] std::enable_if_t<!std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke([[maybe_unused]] meta_handle instance, meta_any *args, std::index_sequence<Index...>) {
-    using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
-
-    const auto invoke = [](auto &&maybe_clazz, auto &&... other) {
-        if constexpr(std::is_member_function_pointer_v<decltype(Candidate)>) {
-            if constexpr(std::is_void_v<typename descriptor::return_type>) {
-                (std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...);
-                return meta_any{std::in_place_type<void>};
-            } else {
-                return meta_dispatch<Policy>((std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...));
-            }
-        } else {
-            if constexpr(std::is_void_v<typename descriptor::return_type>) {
-                Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...);
-                return meta_any{std::in_place_type<void>};
-            } else {
-                return meta_dispatch<Policy>(Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...));
-            }
-        }
-    };
-
-    if constexpr(std::is_invocable_v<decltype(Candidate), const Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
-        if(const auto * const clazz = instance->try_cast<const Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
-            return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
-        }
-    } else if constexpr(std::is_invocable_v<decltype(Candidate), Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
-        if(auto * const clazz = instance->try_cast<Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
-            return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
-        }
-    } else {
-        if(((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
-            return invoke((args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
-        }
-    }
-
-    return meta_any{};
+template<typename Type, auto Candidate, typename Policy = as_is_t>
+[[nodiscard]] std::enable_if_t<!std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke(meta_handle instance, meta_any * const args) {
+    return internal::meta_invoke<Type, Candidate, Policy>(std::move(instance), args, std::make_index_sequence<meta_function_helper_t<Type, decltype(Candidate)>::args_type::size>{});
 }
 
 
@@ -313,11 +355,10 @@ template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t..
  * @tparam Type Reflected type to which the function is associated.
  * @tparam Candidate The actual function to invoke.
  * @tparam Policy Optional policy (no policy set by default).
- * @tparam Index Indexes to use to extract erased arguments from their list.
  * @return A meta any containing the returned value, if any.
  */
-template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
-[[nodiscard]] std::enable_if_t<std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke(meta_handle, meta_any *, std::index_sequence<Index...>) {
+template<typename Type, auto Candidate, typename Policy = as_is_t>
+[[nodiscard]] std::enable_if_t<std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke(meta_handle, meta_any * const) {
     if constexpr(std::is_void_v<decltype(Candidate())>) {
         Candidate();
         return meta_any{std::in_place_type<void>};