Explorar o código

registry: const poly storage function

Michele Caini %!s(int64=5) %!d(string=hai) anos
pai
achega
29c1766714
Modificáronse 3 ficheiros con 25 adicións e 1 borrados
  1. 0 1
      TODO
  2. 6 0
      src/entt/entity/registry.hpp
  3. 19 0
      test/entt/entity/poly_storage.cpp

+ 0 - 1
TODO

@@ -18,7 +18,6 @@
   - ...
 
 WIP:
-* HP: const poly storage function for the registry
 * HP: headless (sparse set only) view
 * HP: pass the registry to pools, basic poly storage should have only component member
 * HP: make view pack work also with groups, make packs input iterator only, add view adapter for external sources

+ 6 - 0
src/entt/entity/registry.hpp

@@ -189,6 +189,12 @@ public:
         return info.seq() < pools.size() ? pools[info.seq()].poly : poly_storage{};
     }
 
+    /*! @copydoc storage */
+    poly_storage storage(const type_info info) const {
+        // as_ref forces a constness conversion for the underlying pool
+        return info.seq() < pools.size() ? as_ref(pools[info.seq()].poly) : poly_storage{};
+    }
+
     /**
      * @brief Returns the number of existing components of the given type.
      * @tparam Component Type of component of which to return the size.

+ 19 - 0
test/entt/entity/poly_storage.cpp

@@ -133,3 +133,22 @@ TEST(PolyStorage, CopyRegistry) {
 
     ASSERT_EQ(registry.size(), other.size());
 }
+
+TEST(PolyStorage, Constness) {
+    entt::registry registry;
+    const auto &cregistry = registry;
+
+    entt::entity entity[1];
+    entity[0] = registry.create();
+    registry.emplace<int>(entity[0], 42);
+
+    auto cstorage = cregistry.storage(entt::type_id<int>());
+
+    ASSERT_DEATH(cstorage->remove(registry, std::begin(entity), std::end(entity)), ".*");
+    ASSERT_TRUE(registry.has<int>(entity[0]));
+
+    auto storage = registry.storage(entt::type_id<int>());
+    storage->remove(registry, std::begin(entity), std::end(entity));
+
+    ASSERT_FALSE(registry.has<int>(entity[0]));
+}