Browse Source

view: added the ::storage method (with tests)

Michele Caini 4 years ago
parent
commit
f9013375f8
3 changed files with 67 additions and 16 deletions
  1. 1 1
      TODO
  2. 10 15
      src/entt/entity/view.hpp
  3. 56 0
      test/entt/entity/view.cpp

+ 1 - 1
TODO

@@ -8,7 +8,7 @@ WIP:
 * fast-contains for sparse sets (low prio but nice-to-have)
 * runtime events (dispatcher/emitter), runtime context variables...
 * runtime_view/registry, remove reference to basic_sparse_set<E>
-* make pools available (view/group), review operator| for views, make views accept registry to ctor
+* make pools available (group), review operator| for views
 * dedicated entity storage, in-place O(1) release/destroy for non-orphaned entities, out-of-sync model
 * custom allocators all over
 

+ 10 - 15
src/entt/entity/view.hpp

@@ -412,17 +412,21 @@ public:
 
     /**
      * @brief Returns the storage for a given component type.
-     * @tparam Comp Type or index of component of which to return the storage.
+     * @tparam Comp Type of component of which to return the storage.
      * @return The storage for the given component type.
      */
     template<typename Comp>
-    [[nodiscard]] storage_type<Comp> &storage() {
+    [[nodiscard]] storage_type<Comp> &storage() const ENTT_NOEXCEPT {
         return *std::get<storage_type<Comp> *>(pools);
     }
 
-    /*! @copydoc storage */
+    /**
+     * @brief Returns the storage for a given component type.
+     * @tparam Comp Index of component of which to return the storage.
+     * @return The storage for the given component type.
+     */
     template<std::size_t Comp>
-    [[nodiscard]] decltype(auto) storage() {
+    [[nodiscard]] decltype(auto) storage() const ENTT_NOEXCEPT {
         return *std::get<Comp>(pools);
     }
 
@@ -718,22 +722,13 @@ public:
     }
 
     /**
-     * @brief Returns the storage for a given component type.
-     * @tparam Comp Type or index of component of which to return the storage.
+     * @brief Returns the storage for the given component type.
      * @return The storage for the given component type.
      */
-    template<typename... Comp>
-    [[nodiscard]] storage_type &storage() {
-        static_assert((std::is_same_v<Comp, Component> && ...), "Invalid component type");
+    [[nodiscard]] storage_type &storage() const ENTT_NOEXCEPT {
         return *std::get<0>(pools);
     }
 
-    /*! @copydoc storage */
-    template<std::size_t Comp>
-    [[nodiscard]] storage_type &storage() {
-        return *std::get<Comp>(pools);
-    }
-
     /**
      * @brief Returns the number of entities that have the given component.
      * @return Number of entities that have the given component.

+ 56 - 0
test/entt/entity/view.cpp

@@ -485,6 +485,36 @@ TEST(SingleComponentView, StableType) {
     ASSERT_EQ(view.size_hint(), 1u);
 }
 
+TEST(SingleComponentView, Storage) {
+    entt::registry registry;
+    const auto entity = registry.create();
+    const auto view = registry.view<int>();
+    const auto cview = registry.view<const char>();
+
+    static_assert(std::is_same_v<decltype(view.storage()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
+    static_assert(std::is_same_v<decltype(cview.storage()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
+
+    ASSERT_EQ(view.size(), 0u);
+    ASSERT_EQ(cview.size(), 0u);
+
+    view.storage().emplace(entity);
+    registry.emplace<char>(entity);
+
+    ASSERT_EQ(view.size(), 1u);
+    ASSERT_EQ(cview.size(), 1u);
+    ASSERT_TRUE(view.storage().contains(entity));
+    ASSERT_TRUE(cview.storage().contains(entity));
+    ASSERT_TRUE((registry.all_of<int, char>(entity)));
+
+    view.storage().erase(entity);
+
+    ASSERT_EQ(view.size(), 0u);
+    ASSERT_EQ(cview.size(), 1u);
+    ASSERT_FALSE(view.storage().contains(entity));
+    ASSERT_TRUE(cview.storage().contains(entity));
+    ASSERT_FALSE((registry.all_of<int, char>(entity)));
+}
+
 TEST(MultiComponentView, Functionalities) {
     entt::registry registry;
     auto view = registry.view<int, char>();
@@ -1278,3 +1308,29 @@ TEST(View, Pipe) {
     ASSERT_FALSE((view1 | view4 | view2).contains(entity));
     ASSERT_TRUE((view1 | view4 | view2).contains(other));
 }
+
+TEST(MultiComponentView, Storage) {
+    entt::registry registry;
+    const auto entity = registry.create();
+    const auto view = registry.view<int, const char>();
+
+    static_assert(std::is_same_v<decltype(view.storage<0u>()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
+    static_assert(std::is_same_v<decltype(view.storage<int>()), typename entt::storage_traits<entt::entity, int>::storage_type &>);
+    static_assert(std::is_same_v<decltype(view.storage<1u>()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
+    static_assert(std::is_same_v<decltype(view.storage<const char>()), const typename entt::storage_traits<entt::entity, char>::storage_type &>);
+
+    ASSERT_EQ(view.size_hint(), 0u);
+
+    view.storage<int>().emplace(entity);
+    registry.emplace<char>(entity);
+
+    ASSERT_EQ(view.size_hint(), 1u);
+    ASSERT_TRUE(view.storage<const char>().contains(entity));
+    ASSERT_TRUE((registry.all_of<int, char>(entity)));
+
+    view.storage<0u>().erase(entity);
+
+    ASSERT_EQ(view.size_hint(), 0u);
+    ASSERT_TRUE(view.storage<1u>().contains(entity));
+    ASSERT_FALSE((registry.all_of<int, char>(entity)));
+}