Просмотр исходного кода

registry: on_replace becomes on_update

Michele Caini 6 лет назад
Родитель
Сommit
dc8fa2153b
3 измененных файлов с 24 добавлено и 17 удалено
  1. 3 3
      docs/md/entity.md
  2. 2 2
      src/entt/entity/observer.hpp
  3. 19 12
      src/entt/entity/registry.hpp

+ 3 - 3
docs/md/entity.md

@@ -229,7 +229,7 @@ This function is overloaded and accepts also a couple of iterators in order to:
   ```
 
 If an entity already has the given component, the `replace` and `patch` member
-function templates can be used to updated it:
+function templates can be used to update it:
 
 ```cpp
 // replaces the component in-place
@@ -250,7 +250,7 @@ This is a slightly faster alternative for the following snippet:
 
 ```cpp
 if(registry.has<comp>(entity)) {
-    registry.replace<velocity>(entity, [](auto &vel) { vel.dx = vel.dy = 0.; });
+    registry.replace<velocity>(entity, 0., 0.);
 } else {
     registry.assign<velocity>(entity, 0., 0.);
 }
@@ -342,7 +342,7 @@ registry.on_construct<position>().disconnect<&my_class::member>(instance);
 ```
 
 To be notified when components are destroyed, use the `on_destroy` member
-function instead. Finally, the `on_replace` member function will return a sink
+function instead. Finally, the `on_update` member function will return a sink
 to which to connect listeners to observe changes.
 
 The function type of a listener should be equivalent to the following:

+ 2 - 2
src/entt/entity/observer.hpp

@@ -198,14 +198,14 @@ class basic_observer {
         static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
             (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
-            reg.template on_replace<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
+            reg.template on_update<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
             reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
         }
 
         static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
             (reg.template on_destroy<Require>().disconnect(obs), ...);
             (reg.template on_construct<Reject>().disconnect(obs), ...);
-            reg.template on_replace<AnyOf>().disconnect(obs);
+            reg.template on_update<AnyOf>().disconnect(obs);
             reg.template on_destroy<AnyOf>().disconnect(obs);
         }
     };

+ 19 - 12
src/entt/entity/registry.hpp

@@ -53,7 +53,7 @@ class basic_registry {
             return sink{construction};
         }
 
-        auto on_replace() ENTT_NOEXCEPT {
+        auto on_update() ENTT_NOEXCEPT {
             return sink{update};
         }
 
@@ -97,7 +97,7 @@ class basic_registry {
         }
 
         template<typename... Func>
-        decltype(auto) replace(basic_registry &owner, const Entity entt, Func &&... func) {
+        decltype(auto) patch(basic_registry &owner, const Entity entt, Func &&... func) {
             (std::forward<Func>(func)(this->get(entt)), ...);
             update.publish(owner, entt);
             return this->get(entt);
@@ -688,12 +688,12 @@ public:
         auto &cpool = assure<Component>();
 
         return cpool.has(entity)
-                ? (cpool.replace(*this, entity, [&args...](auto &&component) { component = Component{std::forward<Args>(args)...}; }), cpool.get(entity))
+                ? (cpool.patch(*this, entity, [&args...](auto &&component) { component = Component{std::forward<Args>(args)...}; }), cpool.get(entity))
                 : cpool.assign(*this, entity, std::forward<Args>(args)...);
     }
 
     /**
-     * @brief Replaces the given component for an entity in-place.
+     * @brief Patches the given component for an entity.
      *
      * The signature of the functions should be equivalent to the following:
      *
@@ -705,23 +705,23 @@ public:
      * copy or by const reference if needed.
      *
      * @warning
-     * Attempting to use an invalid entity or to replace a component of an
-     * entity that doesn't own it results in undefined behavior.<br/>
+     * Attempting to use an invalid entity or to patch a component of an entity
+     * that doesn't own it results in undefined behavior.<br/>
      * An assertion will abort the execution at runtime in debug mode in case of
      * invalid entity or if the entity doesn't own an instance of the given
      * component.
      *
-     * @tparam Component Type of component to replace.
+     * @tparam Component Type of component to patch.
      * @tparam Func Types of the function objects to invoke.
      * @param entity A valid entity identifier.
      * @param func Valid function objects.
-     * @return A reference to the replaced component.
+     * @return A reference to the patched component.
      */
     template<typename Component, typename... Func>
     [[deprecated("use registry::patch instead")]]
     decltype(auto) patch(const entity_type entity, Func &&... func) {
         ENTT_ASSERT(valid(entity));
-        return assure<Component>().replace(*this, entity, std::forward<Func>(func)...);
+        return assure<Component>().patch(*this, entity, std::forward<Func>(func)...);
     }
 
     /*! @copydoc patch */
@@ -1082,7 +1082,7 @@ public:
      *
      * 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
-     * whenever an instance of the given component is explicitly replaced.
+     * whenever an instance of the given component is explicitly updated.
      *
      * The function type for a listener is equivalent to:
      *
@@ -1090,7 +1090,7 @@ public:
      * void(registry<Entity> &, Entity, Component &);
      * @endcode
      *
-     * Listeners are invoked **before** the component has been replaced.
+     * Listeners are invoked **before** the component has been updated.
      *
      * @note
      * Empty types aren't explicitly instantiated. Therefore, temporary objects
@@ -1103,8 +1103,15 @@ public:
      * @return A temporary sink object.
      */
     template<typename Component>
+    auto on_update() {
+        return assure<Component>().on_update();
+    }
+
+    /*! @copydoc on_update */
+    template<typename Component>
+    [[deprecated("use registry::on_update instead")]]
     auto on_replace() {
-        return assure<Component>().on_replace();
+        return on_update<Component>();
     }
 
     /**