Explorar o código

meta: prepare to a safer approach to custom data

Michele Caini hai 1 ano
pai
achega
e76d58c76c
Modificáronse 4 ficheiros con 19 adicións e 15 borrados
  1. 0 1
      TODO
  2. 5 5
      src/entt/meta/factory.hpp
  3. 6 6
      src/entt/meta/meta.hpp
  4. 8 3
      src/entt/meta/node.hpp

+ 0 - 1
TODO

@@ -43,4 +43,3 @@ TODO:
 * deprecate meta properties in favor of custom data
 * deprecate meta properties in favor of custom data
 * refine custom with debug-only type check if possible
 * refine custom with debug-only type check if possible
 * make meta objects safe to use with null nodes
 * make meta objects safe to use with null nodes
-

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

@@ -114,13 +114,13 @@ protected:
         }
         }
     }
     }
 
 
-    void custom(std::shared_ptr<void> udata) {
+    void custom(const id_type type, std::shared_ptr<void> udata) {
         if(bucket == parent) {
         if(bucket == parent) {
-            internal::meta_context::from(*ctx).value[parent].custom = std::move(udata);
+            internal::meta_context::from(*ctx).value[parent].custom = {type, std::move(udata)};
         } else if(is_data) {
         } else if(is_data) {
-            details->data[bucket].custom = std::move(udata);
+            details->data[bucket].custom = {type, std::move(udata)};
         } else {
         } else {
-            details->func[bucket].custom = std::move(udata);
+            details->func[bucket].custom = {type, std::move(udata)};
         }
         }
     }
     }
 
 
@@ -544,7 +544,7 @@ public:
      */
      */
     template<typename Value, typename... Args>
     template<typename Value, typename... Args>
     meta_factory custom(Args &&...args) {
     meta_factory custom(Args &&...args) {
-        base_type::custom(std::make_shared<Value>(std::forward<Args>(args)...));
+        base_type::custom(type_id<Value>().hash(), std::make_shared<Value>(std::forward<Args>(args)...));
         return *this;
         return *this;
     }
     }
 };
 };

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

@@ -934,8 +934,8 @@ struct meta_data {
      */
      */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] const Type &custom() const noexcept {
     [[nodiscard]] const Type &custom() const noexcept {
-        ENTT_ASSERT(node->custom != nullptr, "Invalid user data");
-        return *std::static_pointer_cast<Type>(node->custom);
+        ENTT_ASSERT(node->custom.data != nullptr, "Invalid user data");
+        return *std::static_pointer_cast<Type>(node->custom.data);
     }
     }
 
 
     /**
     /**
@@ -1069,8 +1069,8 @@ struct meta_func {
     /*! @copydoc meta_data::custom */
     /*! @copydoc meta_data::custom */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] const Type &custom() const noexcept {
     [[nodiscard]] const Type &custom() const noexcept {
-        ENTT_ASSERT(node->custom != nullptr, "Invalid user data");
-        return *std::static_pointer_cast<Type>(node->custom);
+        ENTT_ASSERT(node->custom.data != nullptr, "Invalid user data");
+        return *std::static_pointer_cast<Type>(node->custom.data);
     }
     }
 
 
     /**
     /**
@@ -1546,8 +1546,8 @@ public:
     /*! @copydoc meta_data::custom */
     /*! @copydoc meta_data::custom */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] const Type &custom() const noexcept {
     [[nodiscard]] const Type &custom() const noexcept {
-        ENTT_ASSERT(node.custom != nullptr, "Invalid user data");
-        return *std::static_pointer_cast<Type>(node.custom);
+        ENTT_ASSERT(node.custom.data != nullptr, "Invalid user data");
+        return *std::static_pointer_cast<Type>(node.custom.data);
     }
     }
 
 
     /**
     /**

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

@@ -60,6 +60,11 @@ template<typename Type>
 
 
 struct meta_type_node;
 struct meta_type_node;
 
 
+struct meta_custom_node {
+    id_type type;
+    std::shared_ptr<void> data;
+};
+
 struct meta_prop_node {
 struct meta_prop_node {
     meta_type_node (*type)(const meta_context &) noexcept {};
     meta_type_node (*type)(const meta_context &) noexcept {};
     std::shared_ptr<void> value{};
     std::shared_ptr<void> value{};
@@ -96,7 +101,7 @@ struct meta_data_node {
     bool (*set)(meta_handle, meta_any){};
     bool (*set)(meta_handle, meta_any){};
     meta_any (*get)(const meta_ctx &, meta_handle){};
     meta_any (*get)(const meta_ctx &, meta_handle){};
     dense_map<id_type, meta_prop_node, identity> prop{};
     dense_map<id_type, meta_prop_node, identity> prop{};
-    std::shared_ptr<void> custom{};
+    meta_custom_node custom{};
 };
 };
 
 
 struct meta_func_node {
 struct meta_func_node {
@@ -109,7 +114,7 @@ struct meta_func_node {
     meta_any (*invoke)(const meta_ctx &, meta_handle, meta_any *const){};
     meta_any (*invoke)(const meta_ctx &, meta_handle, meta_any *const){};
     std::shared_ptr<meta_func_node> next{};
     std::shared_ptr<meta_func_node> next{};
     dense_map<id_type, meta_prop_node, identity> prop{};
     dense_map<id_type, meta_prop_node, identity> prop{};
-    std::shared_ptr<void> custom{};
+    meta_custom_node custom{};
 };
 };
 
 
 struct meta_template_node {
 struct meta_template_node {
@@ -144,7 +149,7 @@ struct meta_type_node {
     meta_template_node templ{};
     meta_template_node templ{};
     meta_dtor_node dtor{};
     meta_dtor_node dtor{};
     std::shared_ptr<meta_type_descriptor> details{};
     std::shared_ptr<meta_type_descriptor> details{};
-    std::shared_ptr<void> custom{};
+    meta_custom_node custom{};
 };
 };
 
 
 template<auto Member>
 template<auto Member>