Explorar el Código

handle: discard entity on destruction

Michele Caini hace 3 años
padre
commit
2dccd90166
Se han modificado 2 ficheros con 7 adiciones y 5 borrados
  1. 5 3
      src/entt/entity/handle.hpp
  2. 2 2
      test/entt/entity/handle.cpp

+ 5 - 3
src/entt/entity/handle.hpp

@@ -43,7 +43,9 @@ public:
         : entt{value},
         : entt{value},
           it{from},
           it{from},
           last{to} {
           last{to} {
-        while(it != last && !it->second.contains(entt)) { ++it; }
+        while(it != last && !it->second.contains(entt)) {
+            ++it;
+        }
     }
     }
 
 
     constexpr handle_storage_iterator &operator++() noexcept {
     constexpr handle_storage_iterator &operator++() noexcept {
@@ -196,7 +198,7 @@ struct basic_handle {
 
 
     /*! @brief Destroys the entity associated with a handle. */
     /*! @brief Destroys the entity associated with a handle. */
     void destroy() {
     void destroy() {
-        reg->destroy(entt);
+        reg->destroy(std::exchange(entt, null));
     }
     }
 
 
     /**
     /**
@@ -204,7 +206,7 @@ struct basic_handle {
      * @param version A desired version upon destruction.
      * @param version A desired version upon destruction.
      */
      */
     void destroy(const version_type version) {
     void destroy(const version_type version) {
-        reg->destroy(entt, version);
+        reg->destroy(std::exchange(entt, null), version);
     }
     }
 
 
     /**
     /**

+ 2 - 2
test/entt/entity/handle.cpp

@@ -83,8 +83,8 @@ TEST(BasicHandle, Destruction) {
     ASSERT_FALSE(handle);
     ASSERT_FALSE(handle);
     ASSERT_FALSE(handle.valid());
     ASSERT_FALSE(handle.valid());
     ASSERT_NE(handle.registry(), nullptr);
     ASSERT_NE(handle.registry(), nullptr);
-    ASSERT_EQ(handle.entity(), entity);
     ASSERT_EQ(registry.current(entity), typename entt::registry::version_type{});
     ASSERT_EQ(registry.current(entity), typename entt::registry::version_type{});
+    ASSERT_EQ(handle.entity(), entt::entity{entt::null});
 
 
     handle = entt::handle{registry, registry.create()};
     handle = entt::handle{registry, registry.create()};
 
 
@@ -98,8 +98,8 @@ TEST(BasicHandle, Destruction) {
     ASSERT_FALSE(handle);
     ASSERT_FALSE(handle);
     ASSERT_FALSE(handle.valid());
     ASSERT_FALSE(handle.valid());
     ASSERT_NE(handle.registry(), nullptr);
     ASSERT_NE(handle.registry(), nullptr);
-    ASSERT_EQ(handle.entity(), entity);
     ASSERT_NE(registry.current(entity), typename entt::registry::version_type{});
     ASSERT_NE(registry.current(entity), typename entt::registry::version_type{});
+    ASSERT_EQ(handle.entity(), entt::entity{entt::null});
 }
 }
 
 
 TEST(BasicHandle, Comparison) {
 TEST(BasicHandle, Comparison) {