diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp index c525c9bf0..bbe54f388 100644 --- a/src/entt/entity/view.hpp +++ b/src/entt/entity/view.hpp @@ -345,6 +345,8 @@ public: using reverse_iterator = internal::view_iterator; /*! @brief Iterable view type. */ using iterable_view = iterable; + /*! @brief Common type among all storage types. */ + using common_type = basic_common_type; /** * @brief Storage type associated with a given component. @@ -381,6 +383,14 @@ public: return other; } + /** + * @brief Returns the leading storage of a view. + * @return The leading storage of the view. + */ + const common_type &handle() const ENTT_NOEXCEPT { + return *view; + } + /** * @brief Estimates the number of entities iterated by the view. * @return Estimated number of entities iterated by the view. @@ -691,6 +701,8 @@ public: using reverse_iterator = typename basic_common_type::reverse_iterator; /*! @brief Iterable view type. */ using iterable_view = internal::iterable_storage; + /*! @brief Common type among all storage types. */ + using common_type = basic_common_type; /*! @brief Storage type associated with the view component. */ using storage_type = constness_as_t>::storage_type, Component>; @@ -707,6 +719,14 @@ public: : pools{&ref}, filter{} {} + /** + * @brief Returns the leading storage of a view. + * @return The leading storage of the view. + */ + const common_type &handle() const ENTT_NOEXCEPT { + return *std::get<0>(pools); + } + /** * @brief Returns the number of entities that have the given component. * @return Number of entities that have the given component. diff --git a/test/entt/entity/view.cpp b/test/entt/entity/view.cpp index 25752f287..c361334d6 100644 --- a/test/entt/entity/view.cpp +++ b/test/entt/entity/view.cpp @@ -74,6 +74,24 @@ TEST(SingleComponentView, Functionalities) { ASSERT_FALSE(invalid); } +TEST(SingleComponentView, Handle) { + entt::registry registry; + const auto entity = registry.create(); + + auto view = registry.view(); + auto &&handle = view.handle(); + + ASSERT_TRUE(handle.empty()); + ASSERT_FALSE(handle.contains(entity)); + ASSERT_EQ(&handle, &view.handle()); + + registry.emplace(entity); + + ASSERT_FALSE(handle.empty()); + ASSERT_TRUE(handle.contains(entity)); + ASSERT_EQ(&handle, &view.handle()); +} + TEST(SingleComponentView, RawData) { entt::registry registry; auto view = registry.view(); @@ -501,6 +519,37 @@ TEST(MultiComponentView, Functionalities) { ASSERT_FALSE(invalid); } +TEST(MultiComponentView, Handle) { + entt::registry registry; + const auto entity = registry.create(); + + auto view = registry.view(); + auto &&handle = view.handle(); + + ASSERT_TRUE(handle.empty()); + ASSERT_FALSE(handle.contains(entity)); + ASSERT_EQ(&handle, &view.handle()); + + registry.emplace(entity); + + ASSERT_FALSE(handle.empty()); + ASSERT_TRUE(handle.contains(entity)); + ASSERT_EQ(&handle, &view.handle()); + + view = registry.view(); + auto &&other = view.handle(); + + ASSERT_TRUE(other.empty()); + ASSERT_FALSE(other.contains(entity)); + ASSERT_EQ(&other, &view.handle()); + ASSERT_NE(&handle, &other); + + view = view.use(); + + ASSERT_NE(&other, &view.handle()); + ASSERT_EQ(&handle, &view.handle()); +} + TEST(MultiComponentView, LazyTypesFromConstRegistry) { entt::registry registry{}; auto view = std::as_const(registry).view();