瀏覽代碼

registry: named pools support for on_construct/on_update/on_destroy

Michele Caini 2 年之前
父節點
當前提交
4dee9dde11
共有 2 個文件被更改,包括 35 次插入6 次删除
  1. 9 6
      src/entt/entity/registry.hpp
  2. 26 0
      test/entt/entity/registry.cpp

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

@@ -1127,11 +1127,12 @@ public:
      * @sa sink
      *
      * @tparam Type Type of component of which to get the sink.
+     * @param id Optional name used to map the storage within the registry.
      * @return A temporary sink object.
      */
     template<typename Type>
-    [[nodiscard]] auto on_construct() {
-        return assure<Type>().on_construct();
+    [[nodiscard]] auto on_construct(const id_type id = type_hash<Type>::value()) {
+        return assure<Type>(id).on_construct();
     }
 
     /**
@@ -1150,11 +1151,12 @@ public:
      * @sa sink
      *
      * @tparam Type Type of component of which to get the sink.
+     * @param id Optional name used to map the storage within the registry.
      * @return A temporary sink object.
      */
     template<typename Type>
-    [[nodiscard]] auto on_update() {
-        return assure<Type>().on_update();
+    [[nodiscard]] auto on_update(const id_type id = type_hash<Type>::value()) {
+        return assure<Type>(id).on_update();
     }
 
     /**
@@ -1173,11 +1175,12 @@ public:
      * @sa sink
      *
      * @tparam Type Type of component of which to get the sink.
+     * @param id Optional name used to map the storage within the registry.
      * @return A temporary sink object.
      */
     template<typename Type>
-    [[nodiscard]] auto on_destroy() {
-        return assure<Type>().on_destroy();
+    [[nodiscard]] auto on_destroy(const id_type id = type_hash<Type>::value()) {
+        return assure<Type>(id).on_destroy();
     }
 
     /**

+ 26 - 0
test/entt/entity/registry.cpp

@@ -1482,6 +1482,32 @@ TEST(Registry, Signals) {
     ASSERT_EQ(listener.last, entities[0u]);
 }
 
+TEST(Registry, SignalsOnRuntimePool) {
+    using namespace entt::literals;
+
+    entt::registry registry;
+    const auto entity = registry.create();
+    listener listener;
+
+    registry.on_construct<int>("custom"_hs).connect<&listener::decr>(listener);
+    registry.on_update<int>("custom"_hs).connect<&listener::decr>(listener);
+    registry.on_destroy<int>("custom"_hs).connect<&listener::decr>(listener);
+
+    ASSERT_EQ(listener.counter, 0);
+
+    registry.emplace<int>(entity);
+    registry.patch<int>(entity);
+    registry.erase<int>(entity);
+
+    ASSERT_EQ(listener.counter, 0);
+
+    registry.storage<int>("custom"_hs).emplace(entity);
+    registry.storage<int>("custom"_hs).patch(entity);
+    registry.storage<int>("custom"_hs).erase(entity);
+
+    ASSERT_EQ(listener.counter, 3);
+}
+
 TEST(Registry, SignalsOnEntity) {
     entt::registry registry;
     listener listener;