Kaynağa Gözat

added Registry::fast

Michele Caini 8 yıl önce
ebeveyn
işleme
f2eb0c8427
2 değiştirilmiş dosya ile 30 ekleme ve 3 silme
  1. 26 2
      src/entt/entity/registry.hpp
  2. 4 1
      test/entt/entity/registry.cpp

+ 26 - 2
src/entt/entity/registry.hpp

@@ -286,15 +286,39 @@ public:
     }
 
     /**
-     * @brief Verifies if an entity identifier still refers to a valid entity.
+     * @brief Checks if an entity identifier refers to a valid entity.
      * @param entity An entity identifier, either valid or not.
-     * @return True if the identifier is still valid, false otherwise.
+     * @return True if the identifier is valid, false otherwise.
      */
     bool valid(entity_type entity) const noexcept {
         const auto pos = size_type(entity & traits_type::entity_mask);
         return (pos < entities.size() && entities[pos] == entity);
     }
 
+    /**
+     * @brief Checks if an entity identifier refers to a valid entity.
+     *
+     * Alternative version of `valid`. It accesses the internal data structures
+     * without bounds checking and thus it's both unsafe and risky to use.<br/>
+     * You should not invoke directly this function unless you know exactly what
+     * you are doing. Prefer the `valid` member function instead.
+     *
+     * @warning
+     * Attempting to use an entity that doesn't belong to the registry can
+     * result in undefined behavior.<br/>
+     * An assertion will abort the execution at runtime in debug mode in case of
+     * bounds violation.
+     *
+     * @param entity A valid entity identifier.
+     * @return True if the identifier is valid, false otherwise.
+     */
+    bool fast(entity_type entity) const noexcept {
+        const auto pos = size_type(entity & traits_type::entity_mask);
+        assert(pos < entities.size());
+        // the in-use control bit permits to avoid accessing the direct vector
+        return (entities[pos] == entity);
+    }
+
     /**
      * @brief Returns the version stored along with an entity identifier.
      * @param entity An entity identifier, either valid or not.

+ 4 - 1
test/entt/entity/registry.cpp

@@ -92,8 +92,11 @@ TEST(DefaultRegistry, Functionalities) {
     ASSERT_EQ(registry.current(e2), entt::DefaultRegistry::version_type{1});
 
     ASSERT_TRUE(registry.valid(e0));
+    ASSERT_TRUE(registry.fast(e0));
     ASSERT_TRUE(registry.valid(e1));
+    ASSERT_TRUE(registry.fast(e1));
     ASSERT_FALSE(registry.valid(e2));
+    ASSERT_FALSE(registry.fast(e2));
 
     ASSERT_EQ(registry.size(), entt::DefaultRegistry::size_type{2});
     ASSERT_FALSE(registry.empty());
@@ -452,7 +455,7 @@ TEST(DefaultRegistry, ConstructWithComponents) {
 }
 
 TEST(DefaultRegistry, MergeTwoRegistries) {
-    using entity_type = typename entt::DefaultRegistry::entity_type;
+    using entity_type = entt::DefaultRegistry::entity_type;
 
     entt::DefaultRegistry src;
     entt::DefaultRegistry dst;