Browse Source

registry: ::replace always returns the component, no matter what

Michele Caini 6 years ago
parent
commit
3e87788541
2 changed files with 6 additions and 5 deletions
  1. 5 3
      src/entt/entity/registry.hpp
  2. 1 2
      test/entt/entity/registry.cpp

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

@@ -97,9 +97,10 @@ class basic_registry {
         }
 
         template<typename... Func>
-        void replace(basic_registry &owner, const Entity entt, Func &&... func) {
+        decltype(auto) replace(basic_registry &owner, const Entity entt, Func &&... func) {
             (std::forward<Func>(func)(this->get(entt)), ...);
             update.publish(owner, entt);
+            return this->get(entt);
         }
 
     private:
@@ -707,11 +708,12 @@ public:
      * @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.
      */
     template<typename Component, typename... Func>
-    void replace(const entity_type entity, Func &&... func) {
+    decltype(auto) replace(const entity_type entity, Func &&... func) {
         ENTT_ASSERT(valid(entity));
-        assure<Component>().replace(*this, entity, std::forward<Func>(func)...);
+        return assure<Component>().replace(*this, entity, std::forward<Func>(func)...);
     }
 
     /**

+ 1 - 2
test/entt/entity/registry.cpp

@@ -181,8 +181,7 @@ TEST(Registry, Functionalities) {
     ASSERT_NE(&registry.get<int>(e0), &registry.get<int>(e2));
     ASSERT_NE(&registry.get<char>(e0), &registry.get<char>(e2));
 
-    ASSERT_NO_THROW(registry.replace<int>(e0, [](auto &instance) { instance = 0; }));
-    ASSERT_EQ(registry.get<int>(e0), 0);
+    ASSERT_EQ(registry.replace<int>(e0, [](auto &instance) { instance = 3; }), 3);
 
     ASSERT_NO_THROW(registry.assign_or_replace<int>(e0, 1));
     ASSERT_NO_THROW(registry.assign_or_replace<int>(e1, 1));