From d775841457d7e20a2163d8b3a2bdf89367cfd23a Mon Sep 17 00:00:00 2001 From: skypjack Date: Fri, 9 May 2025 17:22:57 +0200 Subject: [PATCH] meta: built-in labels for meta data --- src/entt/meta/factory.hpp | 48 ++++++++++++++++++++++++++++++++++-- src/entt/meta/meta.hpp | 8 ++++++ test/entt/meta/meta_data.cpp | 34 +++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index 0d5cb7912..8290897a6 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -319,6 +319,27 @@ public: return *this; } + /** + * @brief Assigns a meta data to a meta type. + * + * Extended function for hashed string support.
+ * 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. + * @return A meta factory for the given type. + */ + template + meta_factory data(const hashed_string id) noexcept { + return data(id.value(), id.data()); + } + /** * @brief Assigns a meta data to a meta type. * @@ -362,7 +383,7 @@ public: base_type::data( internal::meta_data_node{ id, - nullptr, + label, ((!std::is_pointer_v || std::is_const_v) ? internal::meta_traits::is_const : internal::meta_traits::is_none) | internal::meta_traits::is_static, 1u, &internal::resolve>>, @@ -374,6 +395,29 @@ public: return *this; } + /** + * @brief Assigns a meta data to a meta type by means of its setter and + * getter. + * + * Extended function for hashed string support.
+ * 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. + * @return A meta factory for the given type. + */ + template + meta_factory data(const hashed_string id) noexcept { + return data(id.value(), id.data()); + } + /** * @brief Assigns a meta data to a meta type by means of its setter and * getter. @@ -418,7 +462,7 @@ public: base_type::data( internal::meta_data_node{ id, - nullptr, + label, /* this is never static nor const */ internal::meta_traits::is_none, 1u, diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index ba8f5126d..a0279e4b0 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -832,6 +832,14 @@ struct meta_data { : node{std::move(curr)}, ctx{&area} {} + /** + * @brief Returns the label assigned to a data member, if any. + * @return The label assigned to the data member, if any. + */ + [[nodiscard]] const char *label() const noexcept { + return node.label; + } + /** * @brief Returns the number of setters available. * @return The number of setters available. diff --git a/test/entt/meta/meta_data.cpp b/test/entt/meta/meta_data.cpp index 103b84142..435309143 100644 --- a/test/entt/meta/meta_data.cpp +++ b/test/entt/meta/meta_data.cpp @@ -184,6 +184,40 @@ ENTT_DEBUG_TEST_F(MetaDataDeathTest, Custom) { ASSERT_DEATH([[maybe_unused]] const char value = entt::resolve().data("j"_hs).custom(), ""); } +TEST_F(MetaData, Label) { + using namespace entt::literals; + + entt::meta_reset(); + entt::meta_reset(); + + entt::meta_factory{} + .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{} + .data<&setter_getter::static_setter, &setter_getter::static_getter>("x") + .data<&setter_getter::setter, &setter_getter::getter>("y"_hs) + .data(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(); + const entt::meta_type other = entt::resolve(); + + 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"}); +} + TEST_F(MetaData, Comparison) { using namespace entt::literals;