Add EraseIf() api to OwnerContainer for proper implementaion of Erase-Remove idiom. 87/252387/2
authorSubhransu Mohanty <sub.mohanty@samsung.com>
Wed, 27 Jan 2021 09:20:24 +0000 (18:20 +0900)
committerSubhransu Mohanty <sub.mohanty@samsung.com>
Wed, 27 Jan 2021 09:25:44 +0000 (18:25 +0900)
std::remove_if() moves the end object to the removed object position.
as OwnerContainer keeps the raw pointer, have introduced a higher order function
which will free the object if predicate returns true.

Change-Id: I0e69fee7ef115d9e15cc36e8cbe6889b3ad77c4d

dali/devel-api/common/owner-container.h
dali/internal/update/animation/scene-graph-animation.cpp

index c8c37d5..b010f41 100644 (file)
@@ -86,6 +86,32 @@ public:
   }
 
   /**
+   * @brief Erases all elements that satisfy the predicate from the OwnerContainer.
+   *
+   * @param[in] predicate The predicate
+   */
+  template<class Predicate>
+  void EraseIf(Predicate predicate)
+  {
+    auto begin = Vector<T>::Begin();
+    auto end   = Vector<T>::End();
+
+    auto function = [predicate](auto& obj) {
+      if(predicate(obj))
+      {
+        delete obj;
+        return true;
+      }
+      else
+      {
+        return false;
+      }
+    };
+
+    Vector<T>::Erase(std::remove_if(begin, end, function), end);
+  }
+
+  /**
    * Erases a range of elements.(delete from heap).
    */
   Iterator Erase(Iterator first, Iterator last)
index 1ac2e43..4c60adc 100644 (file)
@@ -483,10 +483,7 @@ void Animation::UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animat
   if(cleanup)
   {
     //Remove animators whose PropertyOwner has been destroyed
-    mAnimators.Erase(std::remove_if(mAnimators.begin(),
-                                    mAnimators.end(),
-                                    [](auto& animator) { return animator->Orphan(); }),
-                     mAnimators.end());
+    mAnimators.EraseIf([](auto& animator) { return animator->Orphan(); });
   }
 }