Browse Source

meta: reduce the number of instantiations due to basic_meta_sequence_container_traits

Michele Caini 4 years ago
parent
commit
5b39ee221f
2 changed files with 16 additions and 25 deletions
  1. 7 15
      src/entt/meta/container.hpp
  2. 9 10
      src/entt/meta/meta.hpp

+ 7 - 15
src/entt/meta/container.hpp

@@ -54,15 +54,15 @@ struct basic_meta_sequence_container_traits {
         return false;
         return false;
     }
     }
 
 
-    [[nodiscard]] static iterator iter(any &container, const bool end) {
+    [[nodiscard]] static iterator iter(any &container, const bool as_end) {
         using std::begin;
         using std::begin;
 
 
         if(auto *const cont = any_cast<Type>(&container); cont) {
         if(auto *const cont = any_cast<Type>(&container); cont) {
-            return iterator{begin(*cont), static_cast<typename iterator::difference_type>(end * cont->size())};
+            return iterator{begin(*cont), static_cast<typename iterator::difference_type>(as_end * cont->size())};
         }
         }
 
 
         const Type &as_const = any_cast<const Type &>(container);
         const Type &as_const = any_cast<const Type &>(container);
-        return iterator{begin(as_const), static_cast<typename iterator::difference_type>(end * as_const.size())};
+        return iterator{begin(as_const), static_cast<typename iterator::difference_type>(as_end * as_const.size())};
     }
     }
 
 
     [[nodiscard]] static iterator insert([[maybe_unused]] any &container, [[maybe_unused]] const std::ptrdiff_t offset, [[maybe_unused]] meta_any &value) {
     [[nodiscard]] static iterator insert([[maybe_unused]] any &container, [[maybe_unused]] const std::ptrdiff_t offset, [[maybe_unused]] meta_any &value) {
@@ -114,24 +114,16 @@ struct basic_meta_associative_container_traits {
         return false;
         return false;
     }
     }
 
 
-    [[nodiscard]] static iterator begin(any &container) {
+    [[nodiscard]] static iterator iter(any &container, const bool as_end) {
         using std::begin;
         using std::begin;
-
-        if(auto *const cont = any_cast<Type>(&container); cont) {
-            return iterator{std::integral_constant<bool, key_only>{}, cont->begin()};
-        }
-
-        return iterator{std::integral_constant<bool, key_only>{}, begin(any_cast<const Type &>(container))};
-    }
-
-    [[nodiscard]] static iterator end(any &container) {
         using std::end;
         using std::end;
 
 
         if(auto *const cont = any_cast<Type>(&container); cont) {
         if(auto *const cont = any_cast<Type>(&container); cont) {
-            return iterator{std::integral_constant<bool, key_only>{}, cont->end()};
+            return iterator{std::integral_constant<bool, key_only>{}, as_end ? end(*cont) : begin(*cont)};
         }
         }
 
 
-        return iterator{std::integral_constant<bool, key_only>{}, end(any_cast<const Type &>(container))};
+        const auto &as_const = any_cast<const Type &>(container);
+        return iterator{std::integral_constant<bool, key_only>{}, as_end ? end(as_const) : begin(as_const)};
     }
     }
 
 
     [[nodiscard]] static bool insert(any &container, meta_any &key, [[maybe_unused]] meta_any &value) {
     [[nodiscard]] static bool insert(any &container, meta_any &key, [[maybe_unused]] meta_any &value) {

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

@@ -111,8 +111,7 @@ struct meta_associative_container {
           value_type_node{internal::meta_node<std::remove_const_t<std::remove_reference_t<typename Type::value_type>>>::resolve()},
           value_type_node{internal::meta_node<std::remove_const_t<std::remove_reference_t<typename Type::value_type>>>::resolve()},
           size_fn{&meta_associative_container_traits<Type>::size},
           size_fn{&meta_associative_container_traits<Type>::size},
           clear_fn{&meta_associative_container_traits<Type>::clear},
           clear_fn{&meta_associative_container_traits<Type>::clear},
-          begin_fn{&meta_associative_container_traits<Type>::begin},
-          end_fn{&meta_associative_container_traits<Type>::end},
+          iter_fn{&meta_associative_container_traits<Type>::iter},
           insert_fn{&meta_associative_container_traits<Type>::insert},
           insert_fn{&meta_associative_container_traits<Type>::insert},
           erase_fn{&meta_associative_container_traits<Type>::erase},
           erase_fn{&meta_associative_container_traits<Type>::erase},
           find_fn{&meta_associative_container_traits<Type>::find},
           find_fn{&meta_associative_container_traits<Type>::find},
@@ -142,7 +141,7 @@ private:
     internal::meta_type_node *value_type_node = nullptr;
     internal::meta_type_node *value_type_node = nullptr;
     size_type (*size_fn)(const any &) ENTT_NOEXCEPT = nullptr;
     size_type (*size_fn)(const any &) ENTT_NOEXCEPT = nullptr;
     bool (*clear_fn)(any &) = nullptr;
     bool (*clear_fn)(any &) = nullptr;
-    iterator (*begin_fn)(any &) = nullptr;
+    iterator (*iter_fn)(any &, const bool) = nullptr;
     iterator (*end_fn)(any &) = nullptr;
     iterator (*end_fn)(any &) = nullptr;
     bool (*insert_fn)(any &, meta_any &, meta_any &) = nullptr;
     bool (*insert_fn)(any &, meta_any &, meta_any &) = nullptr;
     bool (*erase_fn)(any &, meta_any &) = nullptr;
     bool (*erase_fn)(any &, meta_any &) = nullptr;
@@ -1527,10 +1526,10 @@ class meta_associative_container_iterator final {
         deref
         deref
     };
     };
 
 
-    using vtable_type = void(const operation, const any &, void *);
+    using vtable_type = void(const operation, const any &, std::pair<meta_any, meta_any> *);
 
 
     template<bool KeyOnly, typename It>
     template<bool KeyOnly, typename It>
-    static void basic_vtable(const operation op, const any &value, void *other) {
+    static void basic_vtable(const operation op, const any &value, std::pair<meta_any, meta_any> *other) {
         switch(op) {
         switch(op) {
         case operation::incr:
         case operation::incr:
             ++any_cast<It &>(const_cast<any &>(value));
             ++any_cast<It &>(const_cast<any &>(value));
@@ -1538,10 +1537,10 @@ class meta_associative_container_iterator final {
         case operation::deref:
         case operation::deref:
             const auto &it = any_cast<const It &>(value);
             const auto &it = any_cast<const It &>(value);
             if constexpr(KeyOnly) {
             if constexpr(KeyOnly) {
-                static_cast<std::pair<meta_any, meta_any> *>(other)->first.emplace<decltype(*it)>(*it);
+                other->first.emplace<decltype(*it)>(*it);
             } else {
             } else {
-                static_cast<std::pair<meta_any, meta_any> *>(other)->first.emplace<decltype((it->first))>(it->first);
-                static_cast<std::pair<meta_any, meta_any> *>(other)->second.emplace<decltype((it->second))>(it->second);
+                other->first.emplace<decltype((it->first))>(it->first);
+                other->second.emplace<decltype((it->second))>(it->second);
             }
             }
             break;
             break;
         }
         }
@@ -1734,12 +1733,12 @@ inline bool meta_associative_container::clear() {
 
 
 /*! @copydoc meta_sequence_container::begin */
 /*! @copydoc meta_sequence_container::begin */
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
-    return begin_fn(storage);
+    return iter_fn(storage, false);
 }
 }
 
 
 /*! @copydoc meta_sequence_container::end */
 /*! @copydoc meta_sequence_container::end */
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
-    return end_fn(storage);
+    return iter_fn(storage, true);
 }
 }
 
 
 /**
 /**