소스 검색

meta: refine label support implementation

skypjack 9 달 전
부모
커밋
182a6d5fe4
7개의 변경된 파일72개의 추가작업 그리고 144개의 파일을 삭제
  1. 0 2
      TODO
  2. 28 64
      src/entt/meta/factory.hpp
  3. 12 12
      src/entt/meta/meta.hpp
  4. 3 3
      src/entt/meta/node.hpp
  5. 14 31
      test/entt/meta/meta_data.cpp
  6. 8 17
      test/entt/meta/meta_func.cpp
  7. 7 15
      test/entt/meta/meta_type.cpp

+ 0 - 2
TODO

@@ -38,6 +38,4 @@ TODO:
 * support names directly on meta nodes
 * fwd meta_ctx (and update testbed)
 * avoid copying meta_type/data/func nodes
-* review doc for hashed string extended functions in meta factory
-* review tests for hashed string extended functions to avoid meta_reset
 * doc for labels on meta elements

+ 28 - 64
src/entt/meta/factory.hpp

@@ -50,11 +50,11 @@ class basic_meta_factory {
     }
 
 protected:
-    void type(const id_type id, const char *label) noexcept {
+    void type(const id_type id, const char *name) noexcept {
         reset_bucket(parent);
         auto &&elem = meta_context::from(*ctx).value[parent];
         ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
-        elem.label = label;
+        elem.name = name;
         elem.id = id;
     }
 
@@ -170,30 +170,21 @@ public:
 
     /**
      * @brief Assigns a custom unique identifier to a meta type.
-     *
-     * Extended function for hashed string support.<br/>
-     * The identifier is used for the type, while the associated string is used
-     * as the name. The length is ignored.
-     *
-     * @warning
-     * The reflection system expects string literals, does not make copies, and
-     * is not in charge of freeing memory in any case.
-     *
-     * @param id A custom unique identifier.
+     * @param name A custom unique identifier as a **string literal**.
      * @return A meta factory for the given type.
      */
-    meta_factory type(const hashed_string id) noexcept {
-        return type(id.value(), id.data());
+    meta_factory type(const char *name) noexcept {
+        return type(entt::hashed_string::value(name), name);
     }
 
     /**
      * @brief Assigns a custom unique identifier to a meta type.
      * @param id A custom unique identifier.
-     * @param label An optional custom name for the type.
+     * @param name An optional name for the type as a **string literal**.
      * @return A meta factory for the given type.
      */
-    meta_factory type(const id_type id, const char *label = nullptr) noexcept {
-        base_type::type(id, label);
+    meta_factory type(const id_type id, const char *name = nullptr) noexcept {
+        base_type::type(id, name);
         return *this;
     }
 
@@ -321,23 +312,14 @@ public:
 
     /**
      * @brief Assigns a meta data to a meta type.
-     *
-     * Extended function for hashed string support.<br/>
-     * The identifier is used for the type, while the associated string is used
-     * as the name. The length is ignored.
-     *
-     * @warning
-     * The reflection system expects string literals, does not make copies, and
-     * is not in charge of freeing memory in any case.
-     *
      * @tparam Data The actual variable to attach to the meta type.
      * @tparam Policy Optional policy (no policy set by default).
-     * @param id A custom unique identifier.
+     * @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>
-    meta_factory data(const hashed_string id) noexcept {
-        return data<Data, Policy>(id.value(), id.data());
+    meta_factory data(const char *name) noexcept {
+        return data<Data, Policy>(entt::hashed_string::value(name), name);
     }
 
     /**
@@ -351,11 +333,11 @@ public:
      * @tparam Data The actual variable to attach to the meta type.
      * @tparam Policy Optional policy (no policy set by default).
      * @param id Unique identifier.
-     * @param label An optional custom name for the type.
+     * @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>
-    meta_factory data(const id_type id, const char *label = nullptr) noexcept {
+    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 &>;
             static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
@@ -363,7 +345,7 @@ public:
             base_type::data(
                 internal::meta_data_node{
                     id,
-                    label,
+                    name,
                     /* this is never static */
                     std::is_const_v<std::remove_reference_t<data_type>> ? internal::meta_traits::is_const : internal::meta_traits::is_none,
                     1u,
@@ -383,7 +365,7 @@ public:
             base_type::data(
                 internal::meta_data_node{
                     id,
-                    label,
+                    name,
                     ((!std::is_pointer_v<decltype(Data)> || std::is_const_v<data_type>) ? internal::meta_traits::is_const : internal::meta_traits::is_none) | internal::meta_traits::is_static,
                     1u,
                     &internal::resolve<std::remove_cv_t<std::remove_reference_t<data_type>>>,
@@ -398,24 +380,15 @@ public:
     /**
      * @brief Assigns a meta data to a meta type by means of its setter and
      * getter.
-     *
-     * Extended function for hashed string support.<br/>
-     * The identifier is used for the type, while the associated string is used
-     * as the name. The length is ignored.
-     *
-     * @warning
-     * The reflection system expects string literals, does not make copies, and
-     * is not in charge of freeing memory in any case.
-     *
      * @tparam Setter The actual function to use as a setter.
      * @tparam Getter The actual function to use as a getter.
      * @tparam Policy Optional policy (no policy set by default).
-     * @param id A custom unique identifier.
+     * @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>
-    meta_factory data(const hashed_string id) noexcept {
-        return data<Setter, Getter, Policy>(id.value(), id.data());
+    meta_factory data(const char *name) noexcept {
+        return data<Setter, Getter, Policy>(entt::hashed_string::value(name), name);
     }
 
     /**
@@ -436,11 +409,11 @@ public:
      * @tparam Getter The actual function to use as a getter.
      * @tparam Policy Optional policy (no policy set by default).
      * @param id Unique identifier.
-     * @param label An optional custom name for the type.
+     * @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>
-    meta_factory data(const id_type id, const char *label = nullptr) noexcept {
+    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");
 
@@ -448,7 +421,7 @@ public:
             base_type::data(
                 internal::meta_data_node{
                     id,
-                    label,
+                    name,
                     /* this is never static */
                     internal::meta_traits::is_const,
                     0u,
@@ -462,7 +435,7 @@ public:
             base_type::data(
                 internal::meta_data_node{
                     id,
-                    label,
+                    name,
                     /* this is never static nor const */
                     internal::meta_traits::is_none,
                     1u,
@@ -477,23 +450,14 @@ public:
 
     /**
      * @brief Assigns a meta function to a meta type.
-     *
-     * Extended function for hashed string support.<br/>
-     * The identifier is used for the type, while the associated string is used
-     * as the name. The length is ignored.
-     *
-     * @warning
-     * The reflection system expects string literals, does not make copies, and
-     * is not in charge of freeing memory in any case.
-     *
      * @tparam Candidate The actual function to attach to the meta function.
      * @tparam Policy Optional policy (no policy set by default).
-     * @param id A custom unique identifier.
+     * @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>
-    meta_factory func(const hashed_string id) noexcept {
-        return func<Candidate, Policy>(id.value(), id.data());
+    meta_factory func(const char *name) noexcept {
+        return func<Candidate, Policy>(entt::hashed_string::value(name), name);
     }
 
     /**
@@ -507,18 +471,18 @@ public:
      * @tparam Candidate The actual function to attach to the meta type.
      * @tparam Policy Optional policy (no policy set by default).
      * @param id Unique identifier.
-     * @param label An optional custom name for the type.
+     * @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>
-    meta_factory func(const id_type id, const char *label = nullptr) noexcept {
+    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");
 
         base_type::func(
             internal::meta_func_node{
                 id,
-                label,
+                name,
                 (descriptor::is_const ? internal::meta_traits::is_const : internal::meta_traits::is_none) | (descriptor::is_static ? internal::meta_traits::is_static : internal::meta_traits::is_none),
                 descriptor::args_type::size,
                 &internal::resolve<std::conditional_t<std::is_same_v<Policy, as_void_t>, void, std::remove_cv_t<std::remove_reference_t<typename descriptor::return_type>>>>,

+ 12 - 12
src/entt/meta/meta.hpp

@@ -833,11 +833,11 @@ struct meta_data {
           ctx{&area} {}
 
     /**
-     * @brief Returns the label assigned to a data member, if any.
-     * @return The label assigned to the data member, if any.
+     * @brief Returns the name assigned to a data member, if any.
+     * @return The name assigned to the data member, if any.
      */
-    [[nodiscard]] const char *label() const noexcept {
-        return node.label;
+    [[nodiscard]] const char *name() const noexcept {
+        return node.name;
     }
 
     /**
@@ -964,11 +964,11 @@ struct meta_func {
           ctx{&area} {}
 
     /**
-     * @brief Returns the label assigned to a member function, if any.
-     * @return The label assigned to the member function, if any.
+     * @brief Returns the name assigned to a member function, if any.
+     * @return The name assigned to the member function, if any.
      */
-    [[nodiscard]] const char *label() const noexcept {
-        return node.label;
+    [[nodiscard]] const char *name() const noexcept {
+        return node.name;
     }
 
     /**
@@ -1175,11 +1175,11 @@ public:
     }
 
     /**
-     * @brief Returns the label assigned to a type, if any.
-     * @return The label assigned to the type, if any.
+     * @brief Returns the name assigned to a type, if any.
+     * @return The name assigned to the type, if any.
      */
-    [[nodiscard]] const char *label() const noexcept {
-        return node.label;
+    [[nodiscard]] const char *name() const noexcept {
+        return node.name;
     }
 
     /**

+ 3 - 3
src/entt/meta/node.hpp

@@ -95,7 +95,7 @@ struct meta_data_node {
     using size_type = std::size_t;
 
     id_type id{};
-    const char *label{};
+    const char *name{};
     meta_traits traits{meta_traits::is_none};
     size_type arity{0u};
     meta_type_node (*type)(const meta_context &) noexcept {};
@@ -109,7 +109,7 @@ struct meta_func_node {
     using size_type = std::size_t;
 
     id_type id{};
-    const char *label{};
+    const char *name{};
     meta_traits traits{meta_traits::is_none};
     size_type arity{0u};
     meta_type_node (*ret)(const meta_context &) noexcept {};
@@ -140,7 +140,7 @@ struct meta_type_node {
 
     const type_info *info{};
     id_type id{};
-    const char *label{};
+    const char *name{};
     meta_traits traits{meta_traits::is_none};
     size_type size_of{0u};
     meta_type_node (*resolve)(const meta_context &) noexcept {};

+ 14 - 31
test/entt/meta/meta_data.cpp

@@ -96,9 +96,9 @@ struct MetaData: ::testing::Test {
             .custom<char>('c')
             .traits(test::meta_traits::one | test::meta_traits::two | test::meta_traits::three)
             .data<&clazz::i, entt::as_cref_t>("ci"_hs)
-            .data<&clazz::j>("j"_hs)
+            .data<&clazz::j>("j")
             .traits(test::meta_traits::one)
-            .data<&clazz::h>("h"_hs)
+            .data<&clazz::h>("h"_hs, "hhh")
             .traits(test::meta_traits::two)
             .data<&clazz::k>("k"_hs)
             .traits(test::meta_traits::three)
@@ -112,8 +112,8 @@ struct MetaData: ::testing::Test {
             .data<&setter_getter::static_setter, &setter_getter::static_getter>("x"_hs)
             .data<&setter_getter::setter, &setter_getter::getter>("y"_hs)
             .data<&setter_getter::static_setter, &setter_getter::getter>("z"_hs)
-            .data<&setter_getter::setter_with_ref, &setter_getter::getter_with_ref>("w"_hs)
-            .data<nullptr, &setter_getter::getter>("z_ro"_hs)
+            .data<&setter_getter::setter_with_ref, &setter_getter::getter_with_ref>("w")
+            .data<nullptr, &setter_getter::getter>("z_ro"_hs, "readonly")
             .data<nullptr, &setter_getter::value>("value"_hs);
 
         entt::meta_factory<array>{}
@@ -184,38 +184,21 @@ ENTT_DEBUG_TEST_F(MetaDataDeathTest, Custom) {
     ASSERT_DEATH([[maybe_unused]] const char value = entt::resolve<clazz>().data("j"_hs).custom(), "");
 }
 
-TEST_F(MetaData, Label) {
+TEST_F(MetaData, Name) {
     using namespace entt::literals;
 
-    entt::meta_reset<clazz>();
-    entt::meta_reset<setter_getter>();
-
-    entt::meta_factory<clazz>{}
-        .data<&clazz::i, entt::as_ref_t>("i")
-        .data<&clazz::i, entt::as_cref_t>("ci"_hs)
-        .data<&clazz::j>(entt::hashed_string::value("j"))
-        .data<&clazz::h>("h"_hs, "hhh");
-
-    entt::meta_factory<setter_getter>{}
-        .data<&setter_getter::static_setter, &setter_getter::static_getter>("x")
-        .data<&setter_getter::setter, &setter_getter::getter>("y"_hs)
-        .data<nullptr, &setter_getter::getter>(entt::hashed_string::value("z"))
-        .data<&setter_getter::setter_with_ref, &setter_getter::getter_with_ref>("h"_hs, "hhh");
-
     const entt::meta_type type = entt::resolve<clazz>();
     const entt::meta_type other = entt::resolve<setter_getter>();
 
-    ASSERT_EQ(type.data("i"_hs).label(), std::string_view{"i"});
-    ASSERT_EQ(type.data("ci"_hs).label(), std::string_view{"ci"});
-    ASSERT_EQ(type.data("j"_hs).label(), nullptr);
-    ASSERT_EQ(type.data("k"_hs).label(), nullptr);
-    ASSERT_EQ(type.data("h"_hs).label(), std::string_view{"hhh"});
-
-    ASSERT_EQ(other.data("x"_hs).label(), std::string_view{"x"});
-    ASSERT_EQ(other.data("y"_hs).label(), std::string_view{"y"});
-    ASSERT_EQ(other.data("z"_hs).label(), nullptr);
-    ASSERT_EQ(other.data("v"_hs).label(), nullptr);
-    ASSERT_EQ(other.data("h"_hs).label(), std::string_view{"hhh"});
+    ASSERT_EQ(type.data("i"_hs).name(), nullptr);
+    ASSERT_STREQ(type.data("j"_hs).name(), "j");
+    ASSERT_STREQ(type.data("h"_hs).name(), "hhh");
+    ASSERT_EQ(type.data("none"_hs).name(), nullptr);
+
+    ASSERT_EQ(other.data("z"_hs).name(), nullptr);
+    ASSERT_STREQ(other.data("w"_hs).name(), "w");
+    ASSERT_STREQ(other.data("z_ro"_hs).name(), "readonly");
+    ASSERT_EQ(other.data("none"_hs).name(), nullptr);
 }
 
 TEST_F(MetaData, Comparison) {

+ 8 - 17
test/entt/meta/meta_func.cpp

@@ -107,8 +107,8 @@ struct MetaFunc: ::testing::Test {
             .type("derived"_hs)
             .base<base>()
             .func<&base::setter>("setter_from_base"_hs)
-            .func<&base::getter>("getter_from_base"_hs)
-            .func<&base::static_setter>("static_setter_from_base"_hs);
+            .func<&base::getter>("getter_from_base")
+            .func<&base::static_setter>("static_setter_from_base"_hs, "static setter");
 
         entt::meta_factory<function>{}
             .type("func"_hs)
@@ -216,24 +216,15 @@ ENTT_DEBUG_TEST_F(MetaFuncDeathTest, Custom) {
     ASSERT_DEATH([[maybe_unused]] const char value = entt::resolve<function>().func("h"_hs).custom(), "");
 }
 
-TEST_F(MetaFunc, Label) {
+TEST_F(MetaFunc, Name) {
     using namespace entt::literals;
 
-    entt::meta_reset<function>();
+    const entt::meta_type type = entt::resolve<derived>();
 
-    entt::meta_factory<function>()
-        .func<&function::g>("g")
-        .func<function::h>("h"_hs)
-        .func<function::k>(entt::hashed_string::value("k"))
-        .func<&function::v>("v"_hs, "w");
-
-    const entt::meta_type type = entt::resolve<function>();
-
-    ASSERT_EQ(type.func("g"_hs).label(), std::string_view{"g"});
-    ASSERT_EQ(type.func("h"_hs).label(), std::string_view{"h"});
-    ASSERT_EQ(type.func("j"_hs).label(), nullptr);
-    ASSERT_EQ(type.func("k"_hs).label(), nullptr);
-    ASSERT_EQ(type.func("v"_hs).label(), std::string_view{"w"});
+    ASSERT_EQ(type.func("setter_from_base"_hs).name(), nullptr);
+    ASSERT_STREQ(type.func("getter_from_base"_hs).name(), "getter_from_base");
+    ASSERT_STREQ(type.func("static_setter_from_base"_hs).name(), "static setter");
+    ASSERT_EQ(type.func("none"_hs).name(), nullptr);
 }
 
 TEST_F(MetaFunc, Comparison) {

+ 7 - 15
test/entt/meta/meta_type.cpp

@@ -127,7 +127,7 @@ struct MetaType: ::testing::Test {
             .data<set<double>, get<double>>("var"_hs);
 
         entt::meta_factory<unsigned int>{}
-            .type("unsigned int"_hs)
+            .type("unsigned int"_hs, "uint")
             .traits(test::meta_traits::two)
             .data<0u>("min"_hs)
             .data<128u>("max"_hs);
@@ -137,7 +137,7 @@ struct MetaType: ::testing::Test {
             .data<&base::value>("value"_hs);
 
         entt::meta_factory<derived>{}
-            .type("derived"_hs)
+            .type("derived")
             .traits(test::meta_traits::one | test::meta_traits::three)
             .base<base>();
 
@@ -301,21 +301,13 @@ TEST_F(MetaType, IdAndInfo) {
     ASSERT_EQ(type.info(), entt::type_id<clazz>());
 }
 
-TEST_F(MetaType, Label) {
+TEST_F(MetaType, Name) {
     using namespace entt::literals;
 
-    entt::meta_reset();
-
-    entt::meta_factory<base>().type("base");
-    entt::meta_factory<derived>().type("derived"_hs);
-    entt::meta_factory<double>().type(entt::hashed_string::value("double"));
-    entt::meta_factory<unsigned int>().type("unsigned int"_hs, "uint");
-
-    ASSERT_EQ(entt::resolve<base>().label(), std::string_view{"base"});
-    ASSERT_EQ(entt::resolve<derived>().label(), std::string_view{"derived"});
-    ASSERT_STREQ(entt::resolve<unsigned int>().label(), "uint");
-    ASSERT_STREQ(entt::resolve<double>().label(), nullptr);
-    ASSERT_EQ(entt::resolve<int>().label(), nullptr);
+    ASSERT_EQ(entt::resolve<base>().name(), nullptr);
+    ASSERT_STREQ(entt::resolve<derived>().name(), "derived");
+    ASSERT_STREQ(entt::resolve<unsigned int>().name(), "uint");
+    ASSERT_EQ(entt::resolve<void>().name(), nullptr);
 }
 
 TEST_F(MetaType, SizeOf) {