Forráskód Böngészése

meta: share look_for function to reduce duplication

Michele Caini 2 éve
szülő
commit
607e6d96a4
3 módosított fájl, 24 hozzáadás és 40 törlés
  1. 1 1
      TODO
  2. 6 39
      src/entt/meta/meta.hpp
  3. 17 0
      src/entt/meta/node.hpp

+ 1 - 1
TODO

@@ -17,7 +17,7 @@ TODO (high prio):
 * view with entity storage: begin/end should return filtered iterators
 * update view doc: single vs multi type views are no longer a thing actually
 * meta container: add value type to resize
-* meta: merge deep search functions if possible
+* make meta_type::lokup function generic, move it to node.hpp or similar
 * ===> TEST: review view tests after the last changes
 
 WIP:

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

@@ -1368,19 +1368,8 @@ public:
      * @return The registered meta data for the given identifier, if any.
      */
     [[nodiscard]] meta_data data(const id_type id) const {
-        if(node.details) {
-            if(const auto it = node.details->data.find(id); it != node.details->data.cend()) {
-                return meta_data{*ctx, it->second};
-            }
-        }
-
-        for(auto &&curr: base()) {
-            if(auto elem = curr.second.data(id); elem) {
-                return elem;
-            }
-        }
-
-        return meta_data{};
+        const auto *elem = internal::look_for<&internal::meta_type_descriptor::data>(internal::meta_context::from(*ctx), node, id);
+        return elem ? meta_data{*ctx, *elem} : meta_data{};
     }
 
     /**
@@ -1401,19 +1390,8 @@ public:
      * @return The registered meta function for the given identifier, if any.
      */
     [[nodiscard]] meta_func func(const id_type id) const {
-        if(node.details) {
-            if(const auto it = node.details->func.find(id); it != node.details->func.cend()) {
-                return meta_func{*ctx, it->second};
-            }
-        }
-
-        for(auto &&curr: base()) {
-            if(auto elem = curr.second.func(id); elem) {
-                return elem;
-            }
-        }
-
-        return meta_func{};
+        const auto *elem = internal::look_for<&internal::meta_type_descriptor::func>(internal::meta_context::from(*ctx), node, id);
+        return elem ? meta_func{*ctx, *elem} : meta_func{};
     }
 
     /**
@@ -1550,19 +1528,8 @@ public:
      * @return The registered meta property for the given key, if any.
      */
     [[nodiscard]] meta_prop prop(const id_type key) const {
-        if(node.details) {
-            if(const auto it = node.details->prop.find(key); it != node.details->prop.cend()) {
-                return meta_prop{*ctx, it->second};
-            }
-        }
-
-        for(auto &&curr: base()) {
-            if(auto elem = curr.second.prop(key); elem) {
-                return elem;
-            }
-        }
-
-        return meta_prop{};
+        const auto *elem = internal::look_for<&internal::meta_type_descriptor::prop>(internal::meta_context::from(*ctx), node, key);
+        return elem ? meta_prop{*ctx, *elem} : meta_prop{};
     }
 
     /**

+ 17 - 0
src/entt/meta/node.hpp

@@ -131,6 +131,23 @@ struct meta_type_node {
     std::shared_ptr<meta_type_descriptor> details{};
 };
 
+template<auto Member>
+auto *look_for(const meta_context &context, const meta_type_node &node, const id_type id) {
+    if(node.details) {
+        if(const auto it = (node.details.get()->*Member).find(id); it != (node.details.get()->*Member).cend()) {
+            return &it->second;
+        }
+
+        for(auto &&curr: node.details->base) {
+            if(auto *elem = look_for<Member>(context, curr.second.type(context), id); elem) {
+                return elem;
+            }
+        }
+    }
+
+    return static_cast<typename std::remove_reference_t<decltype(node.details.get()->*Member)>::mapped_type *>(nullptr);
+}
+
 template<typename Type>
 meta_type_node resolve(const meta_context &) noexcept;