Browse Source

registry: lazily/partially initialize views in the ::view const function

Michele Caini 2 years ago
parent
commit
dcb5aed901
1 changed files with 4 additions and 1 deletions
  1. 4 1
      src/entt/entity/registry.hpp

+ 4 - 1
src/entt/entity/registry.hpp

@@ -1195,7 +1195,10 @@ public:
     template<typename Type, typename... Other, typename... Exclude>
     [[nodiscard]] basic_view<get_t<storage_for_type<const Type>, storage_for_type<const Other>...>, exclude_t<storage_for_type<const Exclude>...>>
     view(exclude_t<Exclude...> = exclude_t{}) const {
-        return {*assure<std::remove_const_t<Type>>(), *assure<std::remove_const_t<Other>>()..., *assure<std::remove_const_t<Exclude>>()...};
+        const auto cpools = std::make_tuple(assure<std::remove_const_t<Type>>(), assure<std::remove_const_t<Other>>()..., assure<std::remove_const_t<Exclude>>()...);
+        basic_view<get_t<storage_for_type<const Type>, storage_for_type<const Other>...>, exclude_t<storage_for_type<const Exclude>...>> elem{};
+        std::apply([&elem](const auto *...curr) { ((curr ? elem.storage(*curr) : void()), ...); }, cpools);
+        return elem;
     }
 
     /*! @copydoc view */