Browse Source

meta:
* use different function names to disambiguate utility calls
* drop base class meta_policy for policies, no longer required

Michele Caini 3 years ago
parent
commit
7e093c951f
4 changed files with 36 additions and 44 deletions
  1. 0 6
      src/entt/meta/factory.hpp
  2. 4 7
      src/entt/meta/policy.hpp
  3. 17 16
      src/entt/meta/utility.hpp
  4. 15 15
      test/entt/meta/meta_utility.cpp

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

@@ -239,7 +239,6 @@ public:
     template<auto Candidate, typename Policy = as_is_t>
     auto ctor() noexcept {
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
-        static_assert(std::is_base_of_v<meta_policy, Policy>, "Unknown policy type");
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
         static_assert(std::is_same_v<std::remove_cv_t<std::remove_reference_t<typename descriptor::return_type>>, Type>, "The function doesn't return an object of the required type");
 
@@ -327,8 +326,6 @@ public:
      */
     template<auto Data, typename Policy = as_is_t>
     auto data(const id_type id) noexcept {
-        static_assert(std::is_base_of_v<meta_policy, Policy>, "Unknown policy type");
-
         if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
             using data_type = std::remove_reference_t<std::invoke_result_t<decltype(Data), Type &>>;
 
@@ -388,7 +385,6 @@ public:
     template<auto Setter, auto Getter, typename Policy = as_is_t>
     auto data(const id_type id) noexcept {
         using data_type = std::invoke_result_t<decltype(Getter), Type &>;
-        static_assert(std::is_base_of_v<meta_policy, Policy>, "Unknown policy type");
         static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
 
         if constexpr(std::is_same_v<decltype(Setter), std::nullptr_t>) {
@@ -445,7 +441,6 @@ public:
      */
     template<typename Setter, auto Getter, typename Policy = as_is_t>
     auto data(const id_type id) noexcept {
-        static_assert(std::is_base_of_v<meta_policy, Policy>, "Unknown policy type");
         data<Setter, Getter, Policy>(id, std::make_index_sequence<Setter::size>{});
         return *this;
     }
@@ -466,7 +461,6 @@ public:
     template<auto Candidate, typename Policy = as_is_t>
     auto func(const id_type id) noexcept {
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
-        static_assert(std::is_base_of_v<meta_policy, Policy>, "Unknown policy type");
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
 
         auto &&elem = internal::meta_extend(

+ 4 - 7
src/entt/meta/policy.hpp

@@ -5,11 +5,8 @@
 
 namespace entt {
 
-/*! @brief Basic common class for meta policies. */
-struct meta_policy {};
-
 /*! @brief Empty class type used to request the _as ref_ policy. */
-struct as_ref_t final: meta_policy {
+struct as_ref_t final {
     /**
      * @cond TURN_OFF_DOXYGEN
      * Internal details not to be documented.
@@ -23,7 +20,7 @@ struct as_ref_t final: meta_policy {
 };
 
 /*! @brief Empty class type used to request the _as cref_ policy. */
-struct as_cref_t final: meta_policy {
+struct as_cref_t final {
     /**
      * @cond TURN_OFF_DOXYGEN
      * Internal details not to be documented.
@@ -37,7 +34,7 @@ struct as_cref_t final: meta_policy {
 };
 
 /*! @brief Empty class type used to request the _as-is_ policy. */
-struct as_is_t final: meta_policy {
+struct as_is_t final {
     /**
      * @cond TURN_OFF_DOXYGEN
      * Internal details not to be documented.
@@ -51,7 +48,7 @@ struct as_is_t final: meta_policy {
 };
 
 /*! @brief Empty class type used to request the _as void_ policy. */
-struct as_void_t final: meta_policy {
+struct as_void_t final {
     /**
      * @cond TURN_OFF_DOXYGEN
      * Internal details not to be documented.

+ 17 - 16
src/entt/meta/utility.hpp

@@ -156,7 +156,7 @@ using meta_function_helper_t = typename meta_function_helper<Type, Candidate>::t
  * @return A meta any containing the returned value, if any.
  */
 template<typename Policy = as_is_t, typename Type>
-std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_dispatch([[maybe_unused]] Type &&value) {
+meta_any meta_dispatch([[maybe_unused]] Type &&value) {
     if constexpr(std::is_same_v<Policy, as_void_t>) {
         return meta_any{std::in_place_type<void>};
     } else if constexpr(std::is_same_v<Policy, as_ref_t>) {
@@ -165,6 +165,7 @@ std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_dispatch
         static_assert(std::is_lvalue_reference_v<Type>, "Invalid type");
         return meta_any{std::in_place_type<const std::remove_reference_t<Type> &>, std::as_const(value)};
     } else {
+        static_assert(std::is_same_v<Policy, as_is_t>, "Policy not supported");
         return meta_any{std::forward<Type>(value)};
     }
 }
@@ -260,7 +261,7 @@ template<typename Type, auto Data>
  * @return A meta any containing the value of the underlying variable.
  */
 template<typename Type, auto Data, typename Policy = as_is_t>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_getter(const meta_ctx &ctx /*_TODO*/, [[maybe_unused]] meta_handle instance) {
+[[nodiscard]] meta_any meta_getter(const meta_ctx &ctx /*_TODO*/, [[maybe_unused]] meta_handle instance) {
     if constexpr(std::is_member_pointer_v<decltype(Data)> || std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
         if constexpr(!std::is_array_v<std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<decltype(Data), Type &>>>>) {
             if constexpr(std::is_invocable_v<decltype(Data), Type &>) {
@@ -297,7 +298,7 @@ template<typename Type, auto Data, typename Policy = as_is_t>
  * @return A meta any containing the value of the underlying variable.
  */
 template<typename Type, auto Data, typename Policy = as_is_t>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_getter([[maybe_unused]] meta_handle instance) {
+[[nodiscard]] meta_any meta_getter([[maybe_unused]] meta_handle instance) {
     return meta_getter<Type, Data, Policy>(locator<meta_ctx>::value_or(), std::move(instance));
 }
 
@@ -359,15 +360,15 @@ template<typename Type, typename... Args, std::size_t... Index>
  * @brief Tries to _invoke_ an object given a list of erased parameters.
  * @tparam Type Reflected type to which the object to _invoke_ is associated.
  * @tparam Policy Optional policy (no policy set by default).
- * @tparam Candidate The type of the actual object to _invoke_.
  * @param ctx The context from which to search for meta types.
+ * @tparam Candidate The type of the actual object to _invoke_.
  * @param instance An opaque instance of the underlying type, if required.
  * @param candidate The actual object to _invoke_.
  * @param args Parameters to use to _invoke_ the object.
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, typename Policy = as_is_t, typename Candidate>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, Candidate &&candidate, meta_any *const args) {
+[[nodiscard]] meta_any meta_invoke_with(const meta_ctx &ctx, Candidate &&candidate, meta_handle instance, meta_any *const args) {
     return internal::meta_invoke<Type, Policy>(std::move(instance), std::forward<Candidate>(candidate), args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
 }
 
@@ -376,14 +377,14 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @tparam Type Reflected type to which the object to _invoke_ is associated.
  * @tparam Policy Optional policy (no policy set by default).
  * @tparam Candidate The type of the actual object to _invoke_.
- * @param instance An opaque instance of the underlying type, if required.
  * @param candidate The actual object to _invoke_.
+ * @param instance An opaque instance of the underlying type, if required.
  * @param args Parameters to use to _invoke_ the object.
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, typename Policy = as_is_t, typename Candidate>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_invoke(meta_handle instance, Candidate &&candidate, meta_any *const args) {
-    return meta_invoke<Type, Policy>(locator<meta_ctx>::value_or(), std::move(instance), std::forward<Candidate>(candidate), args);
+[[nodiscard]] meta_any meta_invoke_with(Candidate &&candidate, meta_handle instance, meta_any *const args) {
+    return meta_invoke_with<Type, Policy>(locator<meta_ctx>::value_or(), std::forward<Candidate>(candidate), std::move(instance), args);
 }
 
 /**
@@ -397,7 +398,7 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, auto Candidate, typename Policy = as_is_t>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_invoke(const meta_ctx &ctx /*_TODO*/, meta_handle instance, meta_any *const args) {
+[[nodiscard]] meta_any meta_invoke(const meta_ctx &ctx /*_TODO*/, meta_handle instance, meta_any *const args) {
     return internal::meta_invoke<Type, Policy>(std::move(instance), Candidate, args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<decltype(Candidate)>>::args_type::size>{});
 }
 
@@ -411,7 +412,7 @@ template<typename Type, auto Candidate, typename Policy = as_is_t>
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, auto Candidate, typename Policy = as_is_t>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_invoke(meta_handle instance, meta_any *const args) {
+[[nodiscard]] meta_any meta_invoke(meta_handle instance, meta_any *const args) {
     return meta_invoke<Type, Candidate, Policy>(locator<meta_ctx>::value_or(), std::move(instance), args);
 }
 
@@ -451,7 +452,7 @@ template<typename Type, typename... Args>
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, typename Policy = as_is_t, typename Candidate>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_construct(const meta_ctx &ctx /*_TODO*/, Candidate &&candidate, meta_any *const args) {
+[[nodiscard]] meta_any meta_construct_with(const meta_ctx &ctx /*_TODO*/, Candidate &&candidate, meta_any *const args) {
     if constexpr(meta_function_helper_t<Type, Candidate>::is_static || std::is_class_v<std::remove_cv_t<std::remove_reference_t<Candidate>>>) {
         return internal::meta_invoke<Type, Policy>({}, std::forward<Candidate>(candidate), args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
     } else {
@@ -469,8 +470,8 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, typename Policy = as_is_t, typename Candidate>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_construct(Candidate &&candidate, meta_any *const args) {
-    return meta_construct<Type, Policy>(locator<meta_ctx>::value_or(), std::forward<Candidate>(candidate), args);
+[[nodiscard]] meta_any meta_construct_with(Candidate &&candidate, meta_any *const args) {
+    return meta_construct_with<Type, Policy>(locator<meta_ctx>::value_or(), std::forward<Candidate>(candidate), args);
 }
 
 /**
@@ -483,8 +484,8 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, auto Candidate, typename Policy = as_is_t>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_construct(const meta_ctx &ctx /*_TODO*/, meta_any *const args) {
-    return meta_construct<Type, Policy>(ctx, Candidate, args);
+[[nodiscard]] meta_any meta_construct(const meta_ctx &ctx, meta_any *const args) {
+    return meta_construct_with<Type, Policy>(ctx, Candidate, args);
 }
 
 /**
@@ -496,7 +497,7 @@ template<typename Type, auto Candidate, typename Policy = as_is_t>
  * @return A meta any containing the returned value, if any.
  */
 template<typename Type, auto Candidate, typename Policy = as_is_t>
-[[nodiscard]] std::enable_if_t<std::is_base_of_v<meta_policy, Policy>, meta_any> meta_construct(meta_any *const args) {
+[[nodiscard]] meta_any meta_construct(meta_any *const args) {
     return meta_construct<Type, Candidate, Policy>(locator<meta_ctx>::value_or(), args);
 }
 

+ 15 - 15
test/entt/meta/meta_utility.cpp

@@ -157,23 +157,23 @@ TEST_F(MetaUtility, MetaInvokeWithCandidate) {
     entt::meta_any args[2u]{clazz{}, 42};
     args[0u].cast<clazz &>().value = 99;
 
-    ASSERT_FALSE((entt::meta_invoke<clazz>({}, &clazz::setter, nullptr)));
-    ASSERT_FALSE((entt::meta_invoke<clazz>({}, &clazz::getter, nullptr)));
+    ASSERT_FALSE((entt::meta_invoke_with<clazz>(&clazz::setter, {}, nullptr)));
+    ASSERT_FALSE((entt::meta_invoke_with<clazz>(&clazz::getter, {}, nullptr)));
 
-    ASSERT_TRUE((entt::meta_invoke<clazz>(args[0u], &clazz::setter, args + 1u)));
-    ASSERT_FALSE((entt::meta_invoke<clazz>(args[0u], &clazz::setter, args)));
-    ASSERT_EQ((entt::meta_invoke<clazz>(args[0u], &clazz::getter, nullptr)).cast<int>(), 42);
-    ASSERT_FALSE((entt::meta_invoke<clazz>(args[1u], &clazz::getter, nullptr)));
+    ASSERT_TRUE((entt::meta_invoke_with<clazz>(&clazz::setter, args[0u], args + 1u)));
+    ASSERT_FALSE((entt::meta_invoke_with<clazz>(&clazz::setter, args[0u], args)));
+    ASSERT_EQ((entt::meta_invoke_with<clazz>(&clazz::getter, args[0u], nullptr)).cast<int>(), 42);
+    ASSERT_FALSE((entt::meta_invoke_with<clazz>(&clazz::getter, args[1u], nullptr)));
 
-    ASSERT_EQ((entt::meta_invoke<clazz>({}, &clazz::get_value, nullptr)).cast<int>(), 99);
-    ASSERT_TRUE((entt::meta_invoke<clazz>({}, &clazz::reset_value, nullptr)));
+    ASSERT_EQ((entt::meta_invoke_with<clazz>(&clazz::get_value, {}, nullptr)).cast<int>(), 99);
+    ASSERT_TRUE((entt::meta_invoke_with<clazz>(&clazz::reset_value, {}, nullptr)));
     ASSERT_EQ(args[0u].cast<clazz &>().value, 0);
 
     const auto setter = [](int &value) { value = 3; };
     const auto getter = [](int value) { return value * 2; };
 
-    ASSERT_TRUE(entt::meta_invoke<dummy>({}, setter, args + 1u));
-    ASSERT_EQ(entt::meta_invoke<dummy>({}, getter, args + 1u).cast<int>(), 6);
+    ASSERT_TRUE(entt::meta_invoke_with<dummy>(setter, {}, args + 1u));
+    ASSERT_EQ(entt::meta_invoke_with<dummy>(getter, {}, args + 1u).cast<int>(), 6);
 }
 
 TEST_F(MetaUtility, MetaInvoke) {
@@ -204,21 +204,21 @@ TEST_F(MetaUtility, MetaConstructArgsOnly) {
 
 TEST_F(MetaUtility, MetaConstructWithCandidate) {
     entt::meta_any args[2u]{clazz{}, 42};
-    const auto any = entt::meta_construct<clazz>(&clazz::factory, args + 1u);
+    const auto any = entt::meta_construct_with<clazz>(&clazz::factory, args + 1u);
 
     ASSERT_TRUE(any);
-    ASSERT_FALSE((entt::meta_construct<clazz>(&clazz::factory, args)));
+    ASSERT_FALSE((entt::meta_construct_with<clazz>(&clazz::factory, args)));
     ASSERT_EQ(any.cast<const clazz &>().member, 42);
 
     ASSERT_EQ(args[0u].cast<const clazz &>().member, 0);
-    ASSERT_TRUE((entt::meta_construct<clazz>(&clazz::static_setter, args)));
+    ASSERT_TRUE((entt::meta_construct_with<clazz>(&clazz::static_setter, args)));
     ASSERT_EQ(args[0u].cast<const clazz &>().member, 42);
 
     const auto setter = [](int &value) { value = 3; };
     const auto builder = [](int value) { return value * 2; };
 
-    ASSERT_TRUE(entt::meta_construct<dummy>(setter, args + 1u));
-    ASSERT_EQ(entt::meta_construct<dummy>(builder, args + 1u).cast<int>(), 6);
+    ASSERT_TRUE(entt::meta_construct_with<dummy>(setter, args + 1u));
+    ASSERT_EQ(entt::meta_construct_with<dummy>(builder, args + 1u).cast<int>(), 6);
 }
 
 TEST_F(MetaUtility, MetaConstruct) {