Просмотр исходного кода

meta: built-in labels for meta functions

skypjack 10 месяцев назад
Родитель
Сommit
154c16a9d3
4 измененных файлов с 51 добавлено и 3 удалено
  1. 22 2
      src/entt/meta/factory.hpp
  2. 8 0
      src/entt/meta/meta.hpp
  3. 1 1
      src/entt/meta/node.hpp
  4. 20 0
      test/entt/meta/meta_func.cpp

+ 22 - 2
src/entt/meta/factory.hpp

@@ -429,6 +429,25 @@ public:
         return *this;
         return *this;
     }
     }
 
 
+    /**
+     * @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.
+     * @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());
+    }
+
     /**
     /**
      * @brief Assigns a meta function to a meta type.
      * @brief Assigns a meta function to a meta type.
      *
      *
@@ -440,17 +459,18 @@ public:
      * @tparam Candidate The actual function to attach to the meta type.
      * @tparam Candidate The actual function to attach to the meta type.
      * @tparam Policy Optional policy (no policy set by default).
      * @tparam Policy Optional policy (no policy set by default).
      * @param id Unique identifier.
      * @param id Unique identifier.
+     * @param label An optional custom name for the type.
      * @return A meta factory for the parent type.
      * @return A meta factory for the parent type.
      */
      */
     template<auto Candidate, typename Policy = as_is_t>
     template<auto Candidate, typename Policy = as_is_t>
-    meta_factory func(const id_type id) noexcept {
+    meta_factory func(const id_type id, const char *label = nullptr) noexcept {
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
         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");
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
 
 
         base_type::func(
         base_type::func(
             internal::meta_func_node{
             internal::meta_func_node{
                 id,
                 id,
-                nullptr,
+                label,
                 (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::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,
                 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>>>>,
                 &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>>>>,

+ 8 - 0
src/entt/meta/meta.hpp

@@ -955,6 +955,14 @@ struct meta_func {
         : node{std::move(curr)},
         : node{std::move(curr)},
           ctx{&area} {}
           ctx{&area} {}
 
 
+    /**
+     * @brief Returns the label assigned to a member function, if any.
+     * @return The label assigned to the member function, if any.
+     */
+    [[nodiscard]] const char *label() const noexcept {
+        return node.label;
+    }
+
     /**
     /**
      * @brief Returns the number of arguments accepted by a member function.
      * @brief Returns the number of arguments accepted by a member function.
      * @return The number of arguments accepted by the member function.
      * @return The number of arguments accepted by the member function.

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

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

+ 20 - 0
test/entt/meta/meta_func.cpp

@@ -216,6 +216,26 @@ ENTT_DEBUG_TEST_F(MetaFuncDeathTest, Custom) {
     ASSERT_DEATH([[maybe_unused]] const char value = entt::resolve<function>().func("h"_hs).custom(), "");
     ASSERT_DEATH([[maybe_unused]] const char value = entt::resolve<function>().func("h"_hs).custom(), "");
 }
 }
 
 
+TEST_F(MetaFunc, Label) {
+    using namespace entt::literals;
+
+    entt::meta_reset<function>();
+
+    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"});
+}
+
 TEST_F(MetaFunc, Comparison) {
 TEST_F(MetaFunc, Comparison) {
     using namespace entt::literals;
     using namespace entt::literals;