From 859edf3e000ac4d47afb6ae7a8556f35c8f0fcad Mon Sep 17 00:00:00 2001 From: Subhransu Mohanty Date: Wed, 27 Jan 2021 18:20:24 +0900 Subject: [PATCH] Add EraseIf() api to OwnerContainer for proper implementaion of Erase-Remove idiom. 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 | 26 +++++++++++++++++++ .../animation/scene-graph-animation.cpp | 5 +--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/dali/devel-api/common/owner-container.h b/dali/devel-api/common/owner-container.h index c8c37d52e..b010f414d 100644 --- a/dali/devel-api/common/owner-container.h +++ b/dali/devel-api/common/owner-container.h @@ -85,6 +85,32 @@ public: return Vector::Erase(position); } + /** + * @brief Erases all elements that satisfy the predicate from the OwnerContainer. + * + * @param[in] predicate The predicate + */ + template + void EraseIf(Predicate predicate) + { + auto begin = Vector::Begin(); + auto end = Vector::End(); + + auto function = [predicate](auto& obj) { + if(predicate(obj)) + { + delete obj; + return true; + } + else + { + return false; + } + }; + + Vector::Erase(std::remove_if(begin, end, function), end); + } + /** * Erases a range of elements.(delete from heap). */ diff --git a/dali/internal/update/animation/scene-graph-animation.cpp b/dali/internal/update/animation/scene-graph-animation.cpp index 1ac2e43e8..4c60adc1f 100644 --- a/dali/internal/update/animation/scene-graph-animation.cpp +++ b/dali/internal/update/animation/scene-graph-animation.cpp @@ -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(); }); } } -- 2.34.1