Procházet zdrojové kódy

meta: as_is -> as_value / as_auto -> as_is (breaking)

skypjack před 4 měsíci
rodič
revize
cba31e00ea

+ 2 - 1
TODO

@@ -35,4 +35,5 @@ TODO:
 * introduce compile-time flag to skip dll-only checks in basic_any
 * find a way to test deleter and the absence of it in basic_any
 * archetype-like a-là EnTT support (see my own notes)
-* rename meta policy as_is in as_copy
+* meta_factory: add a replace flag to traits to get around the |-only mechanism
+* support searching meta data and func only on top level types, no bases

+ 3 - 3
docs/md/meta.md

@@ -787,7 +787,7 @@ other problems.
 
 There are a few alternatives available at the moment:
 
-* The _as-is_ policy, associated with the type `entt::as_is_t`.<br/>
+* The _as-value_ policy, associated with the type `entt::as_value_t`.<br/>
   This is the default policy. In general, it should never be used explicitly,
   since it is implicitly selected if no other policy is specified.<br/>
   In this case, the return values of the functions as well as the properties
@@ -825,12 +825,12 @@ There are a few alternatives available at the moment:
   `as_ref_t` _adapts_ to the constness of the passed object and to that of the
   return type if any.
 
-* The _as-auto_ policy, associated with the type `entt::as_auto_t`.<br/>
+* The _as-is_ policy, associated with the type `entt::as_is_t`.<br/>
   Useful for decoupling meta type creation code from calling code while still
   preserving the behavior of data members and member functions as defined:
 
   ```cpp
-  entt::meta_factory<my_type>{}.func<&my_type::any_member, entt::as_auto_t>("member"_hs);
+  entt::meta_factory<my_type>{}.func<&my_type::any_member, entt::as_is_t>("member"_hs);
   ```
 
   For data members or member functions that return a reference type, the value

+ 7 - 7
src/entt/meta/factory.hpp

@@ -255,7 +255,7 @@ public:
      * @tparam Policy Optional policy (no policy set by default).
      * @return A meta factory for the parent type.
      */
-    template<auto Candidate, typename Policy = as_is_t>
+    template<auto Candidate, typename Policy = as_value_t>
     meta_factory ctor() noexcept {
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
@@ -292,7 +292,7 @@ public:
      * @param name A custom unique identifier as a **string literal**.
      * @return A meta factory for the given type.
      */
-    template<auto Data, typename Policy = as_is_t>
+    template<auto Data, typename Policy = as_value_t>
     meta_factory data(const char *name) noexcept {
         return data<Data, Policy>(hashed_string::value(name), name);
     }
@@ -311,7 +311,7 @@ public:
      * @param name An optional name for the meta data as a **string literal**.
      * @return A meta factory for the parent type.
      */
-    template<auto Data, typename Policy = as_is_t>
+    template<auto Data, typename Policy = as_value_t>
     meta_factory data(const id_type id, const char *name = nullptr) noexcept {
         if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
             using data_type = std::invoke_result_t<decltype(Data), Type &>;
@@ -361,7 +361,7 @@ public:
      * @param name A custom unique identifier as a **string literal**.
      * @return A meta factory for the given type.
      */
-    template<auto Setter, auto Getter, typename Policy = as_is_t>
+    template<auto Setter, auto Getter, typename Policy = as_value_t>
     meta_factory data(const char *name) noexcept {
         return data<Setter, Getter, Policy>(hashed_string::value(name), name);
     }
@@ -387,7 +387,7 @@ public:
      * @param name An optional name for the meta data as a **string literal**.
      * @return A meta factory for the parent type.
      */
-    template<auto Setter, auto Getter, typename Policy = as_is_t>
+    template<auto Setter, auto Getter, typename Policy = as_value_t>
     meta_factory data(const id_type id, const char *name = nullptr) noexcept {
         using descriptor = meta_function_helper_t<Type, decltype(Getter)>;
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
@@ -430,7 +430,7 @@ public:
      * @param name A custom unique identifier as a **string literal**.
      * @return A meta factory for the given type.
      */
-    template<auto Candidate, typename Policy = as_is_t>
+    template<auto Candidate, typename Policy = as_value_t>
     meta_factory func(const char *name) noexcept {
         return func<Candidate, Policy>(hashed_string::value(name), name);
     }
@@ -449,7 +449,7 @@ public:
      * @param name An optional name for the function as a **string literal**.
      * @return A meta factory for the parent type.
      */
-    template<auto Candidate, typename Policy = as_is_t>
+    template<auto Candidate, typename Policy = as_value_t>
     meta_factory func(const id_type id, const char *name = nullptr) noexcept {
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");

+ 2 - 2
src/entt/meta/policy.hpp

@@ -14,7 +14,7 @@ struct meta_policy {};
 /*! @endcond */
 
 /*! @brief Empty class type used to request the _as-is_ policy. */
-struct as_is_t final: private internal::meta_policy {
+struct as_value_t final: private internal::meta_policy {
     /*! @cond TURN_OFF_DOXYGEN */
     template<typename>
     static constexpr bool value = true;
@@ -46,7 +46,7 @@ struct as_cref_t final: private internal::meta_policy {
 };
 
 /*! @brief Empty class type used to request the _as auto_ policy. */
-struct as_auto_t final: private internal::meta_policy {
+struct as_is_t final: private internal::meta_policy {
     /*! @cond TURN_OFF_DOXYGEN */
     template<typename>
     static constexpr bool value = true;

+ 10 - 10
src/entt/meta/utility.hpp

@@ -164,12 +164,12 @@ using meta_function_helper_t = typename meta_function_helper<Type, Candidate>::t
  * @param value Value to wrap.
  * @return A meta any containing the returned value, if any.
  */
-template<typename Policy = as_is_t, typename Type>
+template<typename Policy = as_value_t, typename Type>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_dispatch(const meta_ctx &ctx, [[maybe_unused]] Type &&value) {
     if constexpr(std::is_same_v<Policy, as_cref_t>) {
         static_assert(std::is_lvalue_reference_v<Type>, "Invalid type");
         return meta_any{ctx, std::in_place_type<const std::remove_reference_t<Type> &>, std::as_const(value)};
-    } else if constexpr(std::is_same_v<Policy, as_ref_t> || (std::is_same_v<Policy, as_auto_t> && std::is_lvalue_reference_v<Type>)) {
+    } else if constexpr(std::is_same_v<Policy, as_ref_t> || (std::is_same_v<Policy, as_is_t> && std::is_lvalue_reference_v<Type>)) {
         return meta_any{ctx, std::in_place_type<Type>, value};
     } else if constexpr(std::is_same_v<Policy, as_void_t>) {
         return meta_any{ctx, std::in_place_type<void>};
@@ -185,7 +185,7 @@ template<typename Policy = as_is_t, typename Type>
  * @param value Value to wrap.
  * @return A meta any containing the returned value, if any.
  */
-template<typename Policy = as_is_t, typename Type>
+template<typename Policy = as_value_t, typename Type>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_dispatch(Type &&value) {
     return meta_dispatch<Policy, Type>(locator<meta_ctx>::value_or(), std::forward<Type>(value));
 }
@@ -313,7 +313,7 @@ template<typename Type, auto Data>
  * @param instance An opaque instance of the underlying type, if required.
  * @return A meta any containing the value of the underlying variable.
  */
-template<typename Type, auto Data, typename Policy = as_is_t>
+template<typename Type, auto Data, typename Policy = as_value_t>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_getter(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_const_t<std::remove_reference_t<std::invoke_result_t<decltype(Data), Type &>>>>) {
@@ -352,7 +352,7 @@ template<typename Type, auto Data, typename Policy = as_is_t>
  * @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>
+template<typename Type, typename Policy = as_value_t, typename Candidate>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(meta_handle instance, Candidate &&candidate, meta_any *const args) {
     return internal::meta_invoke<Type, Policy>(*instance.operator->(), std::forward<Candidate>(candidate), args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
 }
@@ -366,7 +366,7 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @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>
+template<typename Type, auto Candidate, typename Policy = as_value_t>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(meta_handle instance, meta_any *const args) {
     return internal::meta_invoke<Type, Policy>(*instance.operator->(), Candidate, args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<decltype(Candidate)>>::args_type::size>{});
 }
@@ -416,7 +416,7 @@ template<typename Type, typename... Args>
  * @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>
+template<typename Type, typename Policy = as_value_t, typename Candidate>
 [[nodiscard]] meta_any meta_construct(const meta_ctx &ctx, Candidate &&candidate, meta_any *const args) {
     if constexpr(meta_function_helper_t<Type, Candidate>::is_static || std::is_class_v<std::remove_const_t<std::remove_reference_t<Candidate>>>) {
         meta_any placeholder{meta_ctx_arg, ctx};
@@ -436,7 +436,7 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @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>
+template<typename Type, typename Policy = as_value_t, typename Candidate>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<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);
 }
@@ -455,7 +455,7 @@ template<typename Type, typename Policy = as_is_t, typename Candidate>
  * @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>
+template<typename Type, auto Candidate, typename Policy = as_value_t>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(const meta_ctx &ctx, meta_any *const args) {
     return meta_construct<Type, Policy>(ctx, Candidate, args);
 }
@@ -468,7 +468,7 @@ template<typename Type, auto Candidate, typename Policy = as_is_t>
  * @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>
+template<typename Type, auto Candidate, typename Policy = as_value_t>
 [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(meta_any *const args) {
     return meta_construct<Type, Candidate, Policy>(locator<meta_ctx>::value_or(), args);
 }

+ 11 - 11
test/entt/meta/meta_data.cpp

@@ -98,11 +98,11 @@ struct MetaData: ::testing::Test {
             .conv<int>();
 
         entt::meta_factory<clazz>{}
-            .data<&clazz::i, entt::as_auto_t>("ai"_hs)
-            .data<&clazz::j, entt::as_auto_t>("aj"_hs)
-            .data<&clazz::h, entt::as_auto_t>("ah"_hs)
-            .data<&clazz::k, entt::as_auto_t>("ak"_hs)
-            .data<nullptr, &clazz::operator int, entt::as_auto_t>("ao"_hs);
+            .data<&clazz::i, entt::as_is_t>("ir"_hs)
+            .data<&clazz::j, entt::as_is_t>("jc"_hs)
+            .data<&clazz::h, entt::as_is_t>("hr"_hs)
+            .data<&clazz::k, entt::as_is_t>("kc"_hs)
+            .data<nullptr, &clazz::operator int, entt::as_is_t>("ov"_hs);
 
         entt::meta_factory<setter_getter>{}
             .type("setter_getter"_hs)
@@ -548,38 +548,38 @@ TEST_F(MetaData, AsVoid) {
     ASSERT_EQ(data.get(instance), entt::meta_any{std::in_place_type<void>});
 }
 
-TEST_F(MetaData, AsAuto) {
+TEST_F(MetaData, AsIs) {
     using namespace entt::literals;
 
     auto type = entt::resolve<clazz>();
     entt::meta_data data{};
     clazz instance{};
 
-    data = type.data("ai"_hs);
+    data = type.data("ir"_hs);
 
     ASSERT_TRUE(data);
     ASSERT_EQ(data.type(), entt::resolve<int>());
     ASSERT_EQ(data.get(instance).base().policy(), entt::any_policy::ref);
 
-    data = type.data("aj"_hs);
+    data = type.data("jc"_hs);
 
     ASSERT_TRUE(data);
     ASSERT_EQ(data.type(), entt::resolve<int>());
     ASSERT_EQ(data.get(instance).base().policy(), entt::any_policy::cref);
 
-    data = type.data("ah"_hs);
+    data = type.data("hr"_hs);
 
     ASSERT_TRUE(data);
     ASSERT_EQ(data.type(), entt::resolve<int>());
     ASSERT_EQ(data.get(instance).base().policy(), entt::any_policy::ref);
 
-    data = type.data("ak"_hs);
+    data = type.data("kc"_hs);
 
     ASSERT_TRUE(data);
     ASSERT_EQ(data.type(), entt::resolve<int>());
     ASSERT_EQ(data.get(instance).base().policy(), entt::any_policy::cref);
 
-    data = type.data("ao"_hs);
+    data = type.data("ov"_hs);
 
     ASSERT_TRUE(data);
     ASSERT_EQ(data.type(), entt::resolve<int>());

+ 9 - 9
test/entt/meta/meta_func.cpp

@@ -139,10 +139,10 @@ struct MetaFunc: ::testing::Test {
             .conv<int>();
 
         entt::meta_factory<function>{}
-            .func<&function::a, entt::as_auto_t>("aa"_hs)
-            .func<&function::v, entt::as_auto_t>("av"_hs)
-            .func<&function::operator int, entt::as_auto_t>("ao"_hs)
-            .func<&function::g, entt::as_auto_t>("ag"_hs);
+            .func<&function::a, entt::as_is_t>("ar"_hs)
+            .func<&function::v, entt::as_is_t>("vc"_hs)
+            .func<&function::operator int, entt::as_is_t>("ov"_hs)
+            .func<&function::g, entt::as_is_t>("ge"_hs);
     }
 
     void TearDown() override {
@@ -509,32 +509,32 @@ TEST_F(MetaFunc, AsVoid) {
     ASSERT_EQ(value, instance.value);
 }
 
-TEST_F(MetaFunc, AsAuto) {
+TEST_F(MetaFunc, AsIs) {
     using namespace entt::literals;
 
     auto type = entt::resolve<function>();
     entt::meta_func func{};
     function instance{3};
 
-    func = type.func("aa"_hs);
+    func = type.func("ar"_hs);
 
     ASSERT_TRUE(func);
     ASSERT_EQ(func.ret(), entt::resolve<int>());
     ASSERT_EQ(func.invoke(instance).base().policy(), entt::any_policy::ref);
 
-    func = type.func("av"_hs);
+    func = type.func("vc"_hs);
 
     ASSERT_TRUE(func);
     ASSERT_EQ(func.ret(), entt::resolve<int>());
     ASSERT_EQ(func.invoke(instance, 3).base().policy(), entt::any_policy::cref);
 
-    func = type.func("ao"_hs);
+    func = type.func("ov"_hs);
 
     ASSERT_TRUE(func);
     ASSERT_EQ(func.ret(), entt::resolve<int>());
     ASSERT_EQ(func.invoke(instance).base().policy(), entt::any_policy::embedded);
 
-    func = type.func("ag"_hs);
+    func = type.func("ge"_hs);
 
     ASSERT_TRUE(func);
     ASSERT_EQ(func.ret(), entt::resolve<void>());

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

@@ -62,41 +62,41 @@ TEST_F(MetaUtility, MetaDispatch) {
     auto as_cref = entt::meta_dispatch<entt::as_cref_t>(value);
     auto as_ref = entt::meta_dispatch<entt::as_ref_t>(value);
     auto as_void = entt::meta_dispatch<entt::as_void_t>(value);
-    auto as_auto_copy = entt::meta_dispatch<entt::as_auto_t>(static_cast<int &&>(value));
-    auto as_auto_cref = entt::meta_dispatch<entt::as_auto_t>(std::as_const(value));
-    auto as_auto_ref = entt::meta_dispatch<entt::as_auto_t>(value);
-    auto as_is = entt::meta_dispatch(value);
+    auto as_is_copy = entt::meta_dispatch<entt::as_is_t>(static_cast<int &&>(value));
+    auto as_is_cref = entt::meta_dispatch<entt::as_is_t>(std::as_const(value));
+    auto as_is_ref = entt::meta_dispatch<entt::as_is_t>(value);
+    auto as_value = entt::meta_dispatch(value);
 
     ASSERT_EQ(as_cref.type(), entt::resolve<int>());
     ASSERT_EQ(as_ref.type(), entt::resolve<int>());
     ASSERT_EQ(as_void.type(), entt::resolve<void>());
-    ASSERT_EQ(as_auto_copy.type(), entt::resolve<int>());
-    ASSERT_EQ(as_auto_cref.type(), entt::resolve<int>());
-    ASSERT_EQ(as_auto_ref.type(), entt::resolve<int>());
-    ASSERT_EQ(as_is.type(), entt::resolve<int>());
+    ASSERT_EQ(as_is_copy.type(), entt::resolve<int>());
+    ASSERT_EQ(as_is_cref.type(), entt::resolve<int>());
+    ASSERT_EQ(as_is_ref.type(), entt::resolve<int>());
+    ASSERT_EQ(as_value.type(), entt::resolve<int>());
 
     ASSERT_EQ(as_cref.base().policy(), entt::any_policy::cref);
     ASSERT_EQ(as_ref.base().policy(), entt::any_policy::ref);
     ASSERT_EQ(as_void.base().policy(), entt::any_policy::empty);
-    ASSERT_EQ(as_auto_copy.base().policy(), entt::any_policy::embedded);
-    ASSERT_EQ(as_auto_cref.base().policy(), entt::any_policy::cref);
-    ASSERT_EQ(as_auto_ref.base().policy(), entt::any_policy::ref);
-    ASSERT_EQ(as_is.base().policy(), entt::any_policy::embedded);
+    ASSERT_EQ(as_is_copy.base().policy(), entt::any_policy::embedded);
+    ASSERT_EQ(as_is_cref.base().policy(), entt::any_policy::cref);
+    ASSERT_EQ(as_is_ref.base().policy(), entt::any_policy::ref);
+    ASSERT_EQ(as_value.base().policy(), entt::any_policy::embedded);
 
     ASSERT_EQ(as_cref.try_cast<int>(), nullptr);
     ASSERT_NE(as_cref.try_cast<const int>(), nullptr);
     ASSERT_NE(as_ref.try_cast<int>(), nullptr);
-    ASSERT_NE(as_auto_copy.try_cast<int>(), nullptr);
-    ASSERT_EQ(as_auto_cref.try_cast<int>(), nullptr);
-    ASSERT_NE(as_auto_ref.try_cast<int>(), nullptr);
-    ASSERT_NE(as_is.try_cast<int>(), nullptr);
+    ASSERT_NE(as_is_copy.try_cast<int>(), nullptr);
+    ASSERT_EQ(as_is_cref.try_cast<int>(), nullptr);
+    ASSERT_NE(as_is_ref.try_cast<int>(), nullptr);
+    ASSERT_NE(as_value.try_cast<int>(), nullptr);
 
     ASSERT_EQ(as_cref.cast<int>(), 2);
     ASSERT_EQ(as_ref.cast<int>(), 2);
-    ASSERT_EQ(as_auto_copy.cast<int>(), 2);
-    ASSERT_EQ(as_auto_cref.cast<int>(), 2);
-    ASSERT_EQ(as_auto_ref.cast<int>(), 2);
-    ASSERT_EQ(as_is.cast<int>(), 2);
+    ASSERT_EQ(as_is_copy.cast<int>(), 2);
+    ASSERT_EQ(as_is_cref.cast<int>(), 2);
+    ASSERT_EQ(as_is_ref.cast<int>(), 2);
+    ASSERT_EQ(as_value.cast<int>(), 2);
 }
 
 TEST_F(MetaUtility, MetaDispatchMetaAny) {