Browse Source

meta: make meta_range iterators return an id and meta object pair

Michele Caini 3 years ago
parent
commit
a8eebcb0e2
3 changed files with 15 additions and 8 deletions
  1. 9 5
      src/entt/meta/meta.hpp
  2. 4 2
      src/entt/meta/range.hpp
  3. 2 1
      test/entt/meta/meta_type.cpp

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

@@ -1202,7 +1202,7 @@ public:
         }
 
         for(auto &&curr: base()) {
-            if(auto &&elem = curr.data(id); elem) {
+            if(auto &&elem = curr.second.data(id); elem) {
                 return elem;
             }
         }
@@ -1295,13 +1295,17 @@ public:
      * @return A wrapper containing the returned value, if any.
      */
     meta_any invoke(const id_type id, meta_handle instance, meta_any *const args, const size_type sz) const {
-        const auto *candidate = old_lookup<&node_type::func>(args, sz, id);
+        if(const auto *candidate = old_lookup<&node_type::func>(args, sz, id); candidate) {
+            return candidate->invoke(std::move(instance), args);
+        }
 
-        for(auto it = base().begin(), last = base().end(); it != last && !candidate; ++it) {
-            candidate = it->old_lookup<&node_type::func>(args, sz, id);
+        for(auto &&curr: base()) {
+            if(auto res = curr.second.invoke(id, instance->as_ref(), args, sz); res) {
+                return res;
+            }
         }
 
-        return candidate ? candidate->invoke(std::move(instance), args) : meta_any{};
+        return meta_any{};
     }
 
     /**

+ 4 - 2
src/entt/meta/range.hpp

@@ -3,6 +3,8 @@
 
 #include <cstddef>
 #include <iterator>
+#include <utility>
+#include "../core/fwd.hpp"
 #include "../core/iterator.hpp"
 
 namespace entt {
@@ -61,7 +63,7 @@ private:
 template<typename Type, typename It>
 struct meta_range_iterator final {
     using difference_type = std::ptrdiff_t;
-    using value_type = Type;
+    using value_type = std::pair<id_type, Type>;
     using pointer = input_iterator_pointer<value_type>;
     using reference = value_type;
     using iterator_category = std::input_iterator_tag;
@@ -82,7 +84,7 @@ struct meta_range_iterator final {
     }
 
     [[nodiscard]] reference operator*() const noexcept {
-        return &it->second;
+        return std::make_pair(it->first, &it->second);
     }
 
     [[nodiscard]] pointer operator->() const noexcept {

+ 2 - 1
test/entt/meta/meta_type.cpp

@@ -297,7 +297,8 @@ TEST_F(MetaType, Base) {
     ASSERT_NE(type.base().cbegin(), type.base().cend());
 
     for(auto curr: type.base()) {
-        ASSERT_EQ(curr, entt::resolve<base_t>());
+        ASSERT_EQ(curr.first, entt::type_id<base_t>().hash());
+        ASSERT_EQ(curr.second, entt::resolve<base_t>());
     }
 }