Răsfoiți Sursa

meta: built-in labels for meta types

skypjack 10 luni în urmă
părinte
comite
7615e55e96
4 a modificat fișierele cu 40 adăugiri și 3 ștergeri
  1. 23 2
      src/entt/meta/factory.hpp
  2. 8 0
      src/entt/meta/meta.hpp
  3. 1 1
      src/entt/meta/node.hpp
  4. 8 0
      test/entt/meta/meta_type.cpp

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

@@ -11,6 +11,7 @@
 #include "../config/config.h"
 #include "../config/config.h"
 #include "../core/bit.hpp"
 #include "../core/bit.hpp"
 #include "../core/fwd.hpp"
 #include "../core/fwd.hpp"
+#include "../core/hashed_string.hpp"
 #include "../core/type_info.hpp"
 #include "../core/type_info.hpp"
 #include "../core/type_traits.hpp"
 #include "../core/type_traits.hpp"
 #include "../locator/locator.hpp"
 #include "../locator/locator.hpp"
@@ -49,10 +50,11 @@ class basic_meta_factory {
     }
     }
 
 
 protected:
 protected:
-    void type(const id_type id) noexcept {
+    void type(const id_type id, const char *label) noexcept {
         reset_bucket(parent);
         reset_bucket(parent);
         auto &&elem = meta_context::from(*ctx).value[parent];
         auto &&elem = meta_context::from(*ctx).value[parent];
         ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
         ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
+        elem.label = label;
         elem.id = id;
         elem.id = id;
     }
     }
 
 
@@ -166,13 +168,32 @@ public:
     meta_factory(meta_ctx &area) noexcept
     meta_factory(meta_ctx &area) noexcept
         : internal::basic_meta_factory{area, internal::resolve<Type>(internal::meta_context::from(area))} {}
         : internal::basic_meta_factory{area, internal::resolve<Type>(internal::meta_context::from(area))} {}
 
 
+    /**
+     * @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.
+     */
+    meta_factory type(const hashed_string id) noexcept {
+        base_type::type(id.value(), id.data());
+        return *this;
+    }
+
     /**
     /**
      * @brief Assigns a custom unique identifier to a meta type.
      * @brief Assigns a custom unique identifier to a meta type.
      * @param id A custom unique identifier.
      * @param id A custom unique identifier.
      * @return A meta factory for the given type.
      * @return A meta factory for the given type.
      */
      */
     meta_factory type(const id_type id) noexcept {
     meta_factory type(const id_type id) noexcept {
-        base_type::type(id);
+        base_type::type(id, nullptr);
         return *this;
         return *this;
     }
     }
 
 

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

@@ -1158,6 +1158,14 @@ public:
         return node.id;
         return node.id;
     }
     }
 
 
+    /**
+     * @brief Returns the label assigned to a type, if any.
+     * @return The label assigned to the type, if any.
+     */
+    [[nodiscard]] const char *label() const noexcept {
+        return node.label;
+    }
+
     /**
     /**
      * @brief Returns the size of the underlying type if known.
      * @brief Returns the size of the underlying type if known.
      * @return The size of the underlying type if known, 0 otherwise.
      * @return The size of the underlying type if known, 0 otherwise.

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

@@ -140,7 +140,7 @@ struct meta_type_node {
 
 
     const type_info *info{};
     const type_info *info{};
     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 size_of{0u};
     size_type size_of{0u};
     meta_type_node (*resolve)(const meta_context &) noexcept {};
     meta_type_node (*resolve)(const meta_context &) noexcept {};

+ 8 - 0
test/entt/meta/meta_type.cpp

@@ -301,6 +301,14 @@ TEST_F(MetaType, IdAndInfo) {
     ASSERT_EQ(type.info(), entt::type_id<clazz>());
     ASSERT_EQ(type.info(), entt::type_id<clazz>());
 }
 }
 
 
+TEST_F(MetaType, Label) {
+    using namespace entt::literals;
+
+    ASSERT_EQ(entt::resolve<clazz>().label(), std::string_view{"class"});
+    ASSERT_NE(entt::resolve<double>().label(), nullptr);
+    ASSERT_EQ(entt::resolve<int>().label(), nullptr);
+}
+
 TEST_F(MetaType, SizeOf) {
 TEST_F(MetaType, SizeOf) {
     ASSERT_EQ(entt::resolve<void>().size_of(), 0u);
     ASSERT_EQ(entt::resolve<void>().size_of(), 0u);
     ASSERT_EQ(entt::resolve<int>().size_of(), sizeof(int));
     ASSERT_EQ(entt::resolve<int>().size_of(), sizeof(int));