Michele Caini 7 лет назад
Родитель
Сommit
e76f881c67
4 измененных файлов с 57 добавлено и 56 удалено
  1. 16 15
      docs/md/entity.md
  2. 26 26
      src/entt/entity/registry.hpp
  3. 4 4
      test/entt/entity/helper.cpp
  4. 11 11
      test/entt/entity/registry.cpp

+ 16 - 15
docs/md/entity.md

@@ -324,32 +324,33 @@ the component owned by an entity if any, a null pointer otherwise.
 
 
 ## Observe changes
 ## Observe changes
 
 
-Because of how the registry works internally, it stores a couple of signal
+Because of how the registry works internally, it stores a bunch of signal
 handlers for each pool in order to notify some of its data structures on the
 handlers for each pool in order to notify some of its data structures on the
-construction and destruction of components.<br/>
+construction and destruction of components or when an instance of a component is
+explicitly replaced by the user.<br/>
 These signal handlers are also exposed and made available to users. These are
 These signal handlers are also exposed and made available to users. These are
 the basic bricks to build fancy things like dependencies and reactive systems.
 the basic bricks to build fancy things like dependencies and reactive systems.
 
 
 To get a sink to be used to connect and disconnect listeners so as to be
 To get a sink to be used to connect and disconnect listeners so as to be
-notified on the creation of a component, use the `construction` member function:
+notified on the creation of a component, use the `on_construct` member function:
 
 
 ```cpp
 ```cpp
 // connects a free function
 // connects a free function
-registry.construction<position>().connect<&my_free_function>();
+registry.on_construct<position>().connect<&my_free_function>();
 
 
 // connects a member function
 // connects a member function
-registry.construction<position>().connect<&my_class::member>(&instance);
+registry.on_construct<position>().connect<&my_class::member>(&instance);
 
 
 // disconnects a free function
 // disconnects a free function
-registry.construction<position>().disconnect<&my_free_function>();
+registry.on_construct<position>().disconnect<&my_free_function>();
 
 
 // disconnects a member function
 // disconnects a member function
-registry.construction<position>().disconnect<&my_class::member>(&instance);
+registry.on_construct<position>().disconnect<&my_class::member>(&instance);
 ```
 ```
 
 
-To be notified when components are destroyed, use the `destruction` member
-function instead. Finally, the `update` member function will return a sink to
-which to connect listeners to observe changes on components.
+To be notified when components are destroyed, use the `on_destroy` member
+function instead. Finally, the `on_replace` member function will return a sink
+to which to connect listeners to observe changes on components.
 
 
 The function type of a listener for the construction signal should be equivalent
 The function type of a listener for the construction signal should be equivalent
 to the following:
 to the following:
@@ -362,9 +363,9 @@ Where `Component` is intuitively the type of component of interest. In other
 words, a listener is provided with the registry that triggered the notification
 words, a listener is provided with the registry that triggered the notification
 and the entity affected by the change, in addition to the newly created
 and the entity affected by the change, in addition to the newly created
 instance.<br/>
 instance.<br/>
-The function type of a listener to be notified about changes is the same of that
-of the construction signal. The one the destruction signal is also similar,
-except for the `Component` parameter:
+The sink returned by the `on_replace` member function accepts listeners the
+signature of which is the same of that of the construction signal. The one of
+the destruction signal is also similar, except for the `Component` parameter:
 
 
 ```cpp
 ```cpp
 void(registry &, entt::entity);
 void(registry &, entt::entity);
@@ -817,7 +818,7 @@ The following adds components `a_type` and `another_type` whenever `my_type` is
 assigned to an entity:
 assigned to an entity:
 
 
 ```cpp
 ```cpp
-entt::connnect<a_type, another_type>(registry.construction<my_type>());
+entt::connnect<a_type, another_type>(registry.on_construct<my_type>());
 ```
 ```
 
 
 A component is assigned to an entity and thus default initialized only in case
 A component is assigned to an entity and thus default initialized only in case
@@ -826,7 +827,7 @@ be overriden.<br/>
 A dependency can easily be broken by means of the following function template:
 A dependency can easily be broken by means of the following function template:
 
 
 ```cpp
 ```cpp
-entt::disconnect<a_type, another_type>(registry.construction<my_type>());
+entt::disconnect<a_type, another_type>(registry.on_construct<my_type>());
 ```
 ```
 
 
 ### Tags
 ### Tags

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

@@ -67,7 +67,7 @@ class basic_registry {
         template<typename... Args>
         template<typename... Args>
         Component & construct(const Entity entt, Args &&... args) {
         Component & construct(const Entity entt, Args &&... args) {
             auto &component = sparse_set<Entity, Component>::construct(entt, std::forward<Args>(args)...);
             auto &component = sparse_set<Entity, Component>::construct(entt, std::forward<Args>(args)...);
-            construction.publish(*owner, entt, component);
+            on_construct.publish(*owner, entt, component);
             return component;
             return component;
         }
         }
 
 
@@ -75,9 +75,9 @@ class basic_registry {
         Component * batch(It first, It last) {
         Component * batch(It first, It last) {
             auto *component = sparse_set<Entity, Component>::batch(first, last);
             auto *component = sparse_set<Entity, Component>::batch(first, last);
 
 
-            if(!construction.empty()) {
+            if(!on_construct.empty()) {
                 std::for_each(first, last, [component, this](const auto entt) mutable {
                 std::for_each(first, last, [component, this](const auto entt) mutable {
-                    construction.publish(*owner, entt, *(component++));
+                    on_construct.publish(*owner, entt, *(component++));
                 });
                 });
             }
             }
 
 
@@ -85,22 +85,22 @@ class basic_registry {
         }
         }
 
 
         void destroy(const Entity entt) override {
         void destroy(const Entity entt) override {
-            destruction.publish(*owner, entt);
+            on_destroy.publish(*owner, entt);
             sparse_set<Entity, Component>::destroy(entt);
             sparse_set<Entity, Component>::destroy(entt);
         }
         }
 
 
         template<typename... Args>
         template<typename... Args>
         Component & replace(const Entity entt, Args &&... args) {
         Component & replace(const Entity entt, Args &&... args) {
             Component component{std::forward<Args>(args)...};
             Component component{std::forward<Args>(args)...};
-            update.publish(*owner, entt, component);
+            on_replace.publish(*owner, entt, component);
             auto &other = sparse_set<Entity, Component>::get(entt);
             auto &other = sparse_set<Entity, Component>::get(entt);
             std::swap(other, component);
             std::swap(other, component);
             return other;
             return other;
         }
         }
 
 
-        sigh<void(basic_registry &, const Entity, Component &)> construction;
-        sigh<void(basic_registry &, const Entity, Component &)> update;
-        sigh<void(basic_registry &, const Entity)> destruction;
+        sigh<void(basic_registry &, const Entity, Component &)> on_construct;
+        sigh<void(basic_registry &, const Entity, Component &)> on_replace;
+        sigh<void(basic_registry &, const Entity)> on_destroy;
         basic_registry *owner;
         basic_registry *owner;
     };
     };
 
 
@@ -316,11 +316,11 @@ class basic_registry {
                 auto *curr = static_cast<group_type *>(gdata.data.get());
                 auto *curr = static_cast<group_type *>(gdata.data.get());
                 const auto cpools = std::make_tuple(assure<Get>()..., assure<Exclude>()...);
                 const auto cpools = std::make_tuple(assure<Get>()..., assure<Exclude>()...);
 
 
-                (std::get<pool_type<Get> *>(cpools)->destruction.sink().template connect<&group_type::template destroy_if<>>(curr), ...);
-                (std::get<pool_type<Get> *>(cpools)->construction.sink().template connect<&group_type::template maybe_valid_if<Get, Get>>(curr), ...);
+                (std::get<pool_type<Get> *>(cpools)->on_destroy.sink().template connect<&group_type::template destroy_if<>>(curr), ...);
+                (std::get<pool_type<Get> *>(cpools)->on_construct.sink().template connect<&group_type::template maybe_valid_if<Get, Get>>(curr), ...);
 
 
-                (std::get<pool_type<Exclude> *>(cpools)->destruction.sink().template connect<&group_type::template maybe_valid_if<Exclude>>(curr), ...);
-                (std::get<pool_type<Exclude> *>(cpools)->construction.sink().template connect<&group_type::template destroy_if<Exclude>>(curr), ...);
+                (std::get<pool_type<Exclude> *>(cpools)->on_destroy.sink().template connect<&group_type::template maybe_valid_if<Exclude>>(curr), ...);
+                (std::get<pool_type<Exclude> *>(cpools)->on_construct.sink().template connect<&group_type::template destroy_if<Exclude>>(curr), ...);
 
 
                 for(const auto entity: view<Get...>()) {
                 for(const auto entity: view<Get...>()) {
                     if(!(has<Exclude>(entity) || ...)) {
                     if(!(has<Exclude>(entity) || ...)) {
@@ -354,14 +354,14 @@ class basic_registry {
                 auto *curr = static_cast<group_type *>(gdata.data.get());
                 auto *curr = static_cast<group_type *>(gdata.data.get());
                 const auto cpools = std::make_tuple(assure<Owned>()..., assure<Get>()..., assure<Exclude>()...);
                 const auto cpools = std::make_tuple(assure<Owned>()..., assure<Get>()..., assure<Exclude>()...);
 
 
-                (std::get<pool_type<Owned> *>(cpools)->construction.sink().template connect<&group_type::template maybe_valid_if<Owned, Owned>>(curr), ...);
-                (std::get<pool_type<Owned> *>(cpools)->destruction.sink().template connect<&group_type::template discard_if<>>(curr), ...);
+                (std::get<pool_type<Owned> *>(cpools)->on_construct.sink().template connect<&group_type::template maybe_valid_if<Owned, Owned>>(curr), ...);
+                (std::get<pool_type<Owned> *>(cpools)->on_destroy.sink().template connect<&group_type::template discard_if<>>(curr), ...);
 
 
-                (std::get<pool_type<Get> *>(cpools)->construction.sink().template connect<&group_type::template maybe_valid_if<Get, Get>>(curr), ...);
-                (std::get<pool_type<Get> *>(cpools)->destruction.sink().template connect<&group_type::template discard_if<>>(curr), ...);
+                (std::get<pool_type<Get> *>(cpools)->on_construct.sink().template connect<&group_type::template maybe_valid_if<Get, Get>>(curr), ...);
+                (std::get<pool_type<Get> *>(cpools)->on_destroy.sink().template connect<&group_type::template discard_if<>>(curr), ...);
 
 
-                (std::get<pool_type<Exclude> *>(cpools)->destruction.sink().template connect<&group_type::template maybe_valid_if<Exclude>>(curr), ...);
-                (std::get<pool_type<Exclude> *>(cpools)->construction.sink().template connect<&group_type::template discard_if<Exclude>>(curr), ...);
+                (std::get<pool_type<Exclude> *>(cpools)->on_destroy.sink().template connect<&group_type::template maybe_valid_if<Exclude>>(curr), ...);
+                (std::get<pool_type<Exclude> *>(cpools)->on_construct.sink().template connect<&group_type::template discard_if<Exclude>>(curr), ...);
 
 
                 const auto *cpool = std::min({ static_cast<sparse_set<entity_type> *>(std::get<pool_type<Owned> *>(cpools))... }, [](const auto *lhs, const auto *rhs) {
                 const auto *cpool = std::min({ static_cast<sparse_set<entity_type> *>(std::get<pool_type<Owned> *>(cpools))... }, [](const auto *lhs, const auto *rhs) {
                     return lhs->size() < rhs->size();
                     return lhs->size() < rhs->size();
@@ -1047,8 +1047,8 @@ public:
      * @return A temporary sink object.
      * @return A temporary sink object.
      */
      */
     template<typename Component>
     template<typename Component>
-    auto construction() ENTT_NOEXCEPT {
-        return assure<Component>()->construction.sink();
+    auto on_construct() ENTT_NOEXCEPT {
+        return assure<Component>()->on_construct.sink();
     }
     }
 
 
     /**
     /**
@@ -1056,7 +1056,7 @@ public:
      *
      *
      * A sink is an opaque object used to connect listeners to components.<br/>
      * A sink is an opaque object used to connect listeners to components.<br/>
      * The sink returned by this function can be used to receive notifications
      * The sink returned by this function can be used to receive notifications
-     * whenever an instance of the given component is updated.
+     * whenever an instance of the given component is explicitly replaced.
      *
      *
      * The function type for a listener is equivalent to:
      * The function type for a listener is equivalent to:
      * @code{.cpp}
      * @code{.cpp}
@@ -1074,8 +1074,8 @@ public:
      * @return A temporary sink object.
      * @return A temporary sink object.
      */
      */
     template<typename Component>
     template<typename Component>
-    auto update() ENTT_NOEXCEPT {
-        return assure<Component>()->update.sink();
+    auto on_replace() ENTT_NOEXCEPT {
+        return assure<Component>()->on_replace.sink();
     }
     }
 
 
     /**
     /**
@@ -1102,8 +1102,8 @@ public:
      * @return A temporary sink object.
      * @return A temporary sink object.
      */
      */
     template<typename Component>
     template<typename Component>
-    auto destruction() ENTT_NOEXCEPT {
-        return assure<Component>()->destruction.sink();
+    auto on_destroy() ENTT_NOEXCEPT {
+        return assure<Component>()->on_destroy.sink();
     }
     }
 
 
     /**
     /**
@@ -1236,7 +1236,7 @@ public:
      */
      */
     template<typename Component>
     template<typename Component>
     void reset() {
     void reset() {
-        if(auto *cpool = assure<Component>(); cpool->destruction.empty()) {
+        if(auto *cpool = assure<Component>(); cpool->on_destroy.empty()) {
             // no group set, otherwise the signal wouldn't be empty
             // no group set, otherwise the signal wouldn't be empty
             cpool->reset();
             cpool->reset();
         } else {
         } else {

+ 4 - 4
test/entt/entity/helper.cpp

@@ -25,7 +25,7 @@ TEST(Helper, AsGroup) {
 TEST(Helper, Dependency) {
 TEST(Helper, Dependency) {
     entt::registry registry;
     entt::registry registry;
     const auto entity = registry.create();
     const auto entity = registry.create();
-    entt::connect<double, float>(registry.construction<int>());
+    entt::connect<double, float>(registry.on_construct<int>());
 
 
     ASSERT_FALSE(registry.has<double>(entity));
     ASSERT_FALSE(registry.has<double>(entity));
     ASSERT_FALSE(registry.has<float>(entity));
     ASSERT_FALSE(registry.has<float>(entity));
@@ -61,7 +61,7 @@ TEST(Helper, Dependency) {
     registry.remove<int>(entity);
     registry.remove<int>(entity);
     registry.remove<double>(entity);
     registry.remove<double>(entity);
     registry.remove<float>(entity);
     registry.remove<float>(entity);
-    entt::disconnect<double, float>(registry.construction<int>());
+    entt::disconnect<double, float>(registry.on_construct<int>());
     registry.assign<int>(entity);
     registry.assign<int>(entity);
 
 
     ASSERT_FALSE(registry.has<double>(entity));
     ASSERT_FALSE(registry.has<double>(entity));
@@ -70,8 +70,8 @@ TEST(Helper, Dependency) {
 
 
 TEST(Dependency, MultipleListenersOnTheSameType) {
 TEST(Dependency, MultipleListenersOnTheSameType) {
     entt::registry registry;
     entt::registry registry;
-    entt::connect<double>(registry.construction<int>());
-    entt::connect<char>(registry.construction<int>());
+    entt::connect<double>(registry.on_construct<int>());
+    entt::connect<char>(registry.on_construct<int>());
 
 
     const auto entity = registry.create();
     const auto entity = registry.create();
     registry.assign<int>(entity);
     registry.assign<int>(entity);

+ 11 - 11
test/entt/entity/registry.cpp

@@ -854,8 +854,8 @@ TEST(Registry, Signals) {
     entt::registry registry;
     entt::registry registry;
     listener listener;
     listener listener;
 
 
-    registry.construction<int>().connect<&listener::incr<int>>(&listener);
-    registry.destruction<int>().connect<&listener::decr<int>>(&listener);
+    registry.on_construct<int>().connect<&listener::incr<int>>(&listener);
+    registry.on_destroy<int>().connect<&listener::decr<int>>(&listener);
 
 
     auto e0 = registry.create();
     auto e0 = registry.create();
     auto e1 = registry.create();
     auto e1 = registry.create();
@@ -871,20 +871,20 @@ TEST(Registry, Signals) {
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.last, e0);
     ASSERT_EQ(listener.last, e0);
 
 
-    registry.destruction<int>().disconnect<&listener::decr<int>>(&listener);
+    registry.on_destroy<int>().disconnect<&listener::decr<int>>(&listener);
     registry.remove<int>(e1);
     registry.remove<int>(e1);
 
 
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.last, e0);
     ASSERT_EQ(listener.last, e0);
 
 
-    registry.construction<int>().disconnect<&listener::incr<int>>(&listener);
+    registry.on_construct<int>().disconnect<&listener::incr<int>>(&listener);
     registry.assign<int>(e1);
     registry.assign<int>(e1);
 
 
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.last, e0);
     ASSERT_EQ(listener.last, e0);
 
 
-    registry.construction<int>().connect<&listener::incr<int>>(&listener);
-    registry.destruction<int>().connect<&listener::decr<int>>(&listener);
+    registry.on_construct<int>().connect<&listener::incr<int>>(&listener);
+    registry.on_destroy<int>().connect<&listener::decr<int>>(&listener);
     registry.assign<int>(e0);
     registry.assign<int>(e0);
     registry.reset<int>(e1);
     registry.reset<int>(e1);
 
 
@@ -909,13 +909,13 @@ TEST(Registry, Signals) {
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.last, e0);
     ASSERT_EQ(listener.last, e0);
 
 
-    registry.destruction<int>().disconnect<&listener::decr<int>>(&listener);
+    registry.on_destroy<int>().disconnect<&listener::decr<int>>(&listener);
     registry.assign_or_replace<int>(e0);
     registry.assign_or_replace<int>(e0);
 
 
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.counter, 1);
     ASSERT_EQ(listener.last, e0);
     ASSERT_EQ(listener.last, e0);
 
 
-    registry.update<int>().connect<&listener::incr<int>>(&listener);
+    registry.on_replace<int>().connect<&listener::incr<int>>(&listener);
     registry.assign_or_replace<int>(e0);
     registry.assign_or_replace<int>(e0);
 
 
     ASSERT_EQ(listener.counter, 2);
     ASSERT_EQ(listener.counter, 2);
@@ -1069,7 +1069,7 @@ TEST(Registry, CreateManyEntitiesWithComponentsAtOnceWithListener) {
     entt::entity entities[3];
     entt::entity entities[3];
     listener listener;
     listener listener;
 
 
-    registry.construction<int>().connect<&listener::incr<int>>(&listener);
+    registry.on_construct<int>().connect<&listener::incr<int>>(&listener);
     registry.create<int, char>(std::begin(entities), std::end(entities));
     registry.create<int, char>(std::begin(entities), std::end(entities));
 
 
     ASSERT_EQ(listener.counter, 3);
     ASSERT_EQ(listener.counter, 3);
@@ -1246,8 +1246,8 @@ TEST(Registry, Clone) {
     ASSERT_NE(e1, entity);
     ASSERT_NE(e1, entity);
     ASSERT_EQ(registry.entity(e1), registry.entity(entity));
     ASSERT_EQ(registry.entity(e1), registry.entity(entity));
 
 
-    registry.construction<char>().connect<&listener::incr<char>>(&listener);
-    registry.destruction<char>().connect<&listener::decr<char>>(&listener);
+    registry.on_construct<char>().connect<&listener::incr<char>>(&listener);
+    registry.on_destroy<char>().connect<&listener::decr<char>>(&listener);
     registry.assign<char>(entity, 'e');
     registry.assign<char>(entity, 'e');
     registry.assign<char>(e0, '0');
     registry.assign<char>(e0, '0');
     registry.remove<char>(e0);
     registry.remove<char>(e0);