Browse Source

storage: allow on_update signals on entity storage

Michele Caini 3 năm trước cách đây
mục cha
commit
5079f38e99
2 tập tin đã thay đổi với 34 bổ sung0 xóa
  1. 12 0
      src/entt/entity/storage.hpp
  2. 22 0
      test/entt/entity/storage_entity.cpp

+ 12 - 0
src/entt/entity/storage.hpp

@@ -1116,6 +1116,18 @@ public:
         return hint;
     }
 
+    /**
+     * @brief Updates a given identifier.
+     * @tparam Func Types of the function objects to invoke.
+     * @param entt A valid identifier.
+     * @param func Valid function objects.
+     */
+    template<typename... Func>
+    void patch([[maybe_unused]] const entity_type entt, Func &&...func) {
+        ENTT_ASSERT(base_type::contains(entt), "Storage does not contain entity");
+        (std::forward<Func>(func)(), ...);
+    }
+
     /**
      * @brief Assigns each element in a range an identifier.
      * @tparam It Type of mutable forward iterator.

+ 22 - 0
test/entt/entity/storage_entity.cpp

@@ -222,6 +222,28 @@ TEST(StorageEntity, Emplace) {
     ASSERT_EQ(entities[1u], entt::entity{8});
 }
 
+TEST(StorageEntity, Patch) {
+    entt::storage<entt::entity> pool;
+    const auto entity = pool.emplace();
+
+    int counter = 0;
+    auto callback = [&counter]() { ++counter; };
+
+    ASSERT_EQ(counter, 0);
+
+    pool.patch(entity);
+    pool.patch(entity, callback);
+    pool.patch(entity, callback, callback);
+
+    ASSERT_EQ(counter, 3);
+}
+
+ENTT_DEBUG_TEST(StorageEntityDeathTest, Patch) {
+    entt::storage<entt::entity> pool;
+
+    ASSERT_DEATH(pool.patch(entt::null), "");
+}
+
 TEST(StorageEntity, Insert) {
     entt::storage<entt::entity> pool;
     entt::entity entities[2u]{};