From b6e3c77d9956fa042d06b396c5e5cbf1768614f0 Mon Sep 17 00:00:00 2001 From: "huayong.xu" Date: Thu, 17 Mar 2022 14:05:21 +0800 Subject: [PATCH] Remove element of Property::Map by the specified key. Change-Id: Ife048e33a54edb6b6cebfe79b464707c0a361a45 --- automated-tests/src/dali/utc-Dali-PropertyMap.cpp | 24 ++++++++++++++++++- dali/public-api/object/property-map.cpp | 28 ++++++++++++++++++++++- dali/public-api/object/property-map.h | 20 +++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) mode change 100644 => 100755 automated-tests/src/dali/utc-Dali-PropertyMap.cpp mode change 100644 => 100755 dali/public-api/object/property-map.h diff --git a/automated-tests/src/dali/utc-Dali-PropertyMap.cpp b/automated-tests/src/dali/utc-Dali-PropertyMap.cpp old mode 100644 new mode 100755 index 84d58c2..61edaee --- a/automated-tests/src/dali/utc-Dali-PropertyMap.cpp +++ b/automated-tests/src/dali/utc-Dali-PropertyMap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -582,6 +582,28 @@ int UtcDaliPropertyMapAnonymousAddChainP(void) END_TEST; } +int UtcDaliPropertyMapRemove(void) +{ + Property::Map map; + map["hello"] = 1; + map[10] = "DALi"; + map["world"] = 2; + + DALI_TEST_CHECK(map.Count() == 3); + DALI_TEST_CHECK(!map.Remove(0)); + DALI_TEST_CHECK(map.Count() == 3); + DALI_TEST_CHECK(!map.Remove("doesnotexist")); + DALI_TEST_CHECK(map.Count() == 3); + DALI_TEST_CHECK(map.Remove(10)); + DALI_TEST_CHECK(map.Count() == 2); + DALI_TEST_CHECK(map.Remove("hello")); + DALI_TEST_CHECK(map.Count() == 1); + DALI_TEST_CHECK(map.Remove("world")); + DALI_TEST_CHECK(map.Count() == 0); + + END_TEST; +} + int UtcDaliPropertyMapMerge(void) { Property::Map map; diff --git a/dali/public-api/object/property-map.cpp b/dali/public-api/object/property-map.cpp index 7a500c6..579ec48 100644 --- a/dali/public-api/object/property-map.cpp +++ b/dali/public-api/object/property-map.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -266,6 +266,32 @@ void Property::Map::Clear() mImpl->mIndexValueContainer.clear(); } +bool Property::Map::Remove(Property::Index key) +{ + DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); + + auto iter = std::find_if(mImpl->mIndexValueContainer.begin(), mImpl->mIndexValueContainer.end(), [key](const IndexValuePair& element) { return element.first == key; }); + if(iter != mImpl->mIndexValueContainer.end()) + { + mImpl->mIndexValueContainer.erase(iter); + return true; + } + return false; +} + +bool Property::Map::Remove(std::string_view key) +{ + DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); + + auto iter = std::find_if(mImpl->mStringValueContainer.begin(), mImpl->mStringValueContainer.end(), [key](const StringValuePair& element) { return element.first == key; }); + if(iter != mImpl->mStringValueContainer.end()) + { + mImpl->mStringValueContainer.erase(iter); + return true; + } + return false; +} + void Property::Map::Merge(const Property::Map& from) { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); diff --git a/dali/public-api/object/property-map.h b/dali/public-api/object/property-map.h old mode 100644 new mode 100755 index 45431f5..181d553 --- a/dali/public-api/object/property-map.h +++ b/dali/public-api/object/property-map.h @@ -2,7 +2,7 @@ #define DALI_PROPERTY_MAP_H /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -273,6 +273,24 @@ public: void Clear(); /** + * @brief Removes the item by the specified key. + * + * @SINCE_2_1.15 + * @param[in] key The key to remove + * @return @c true if succeeded, @c false otherwise + */ + bool Remove(Property::Index key); + + /** + * @brief Removes the item by the specified key. + * + * @SINCE_2_1.15 + * @param[in] key The key to remove + * @return @c true if succeeded, @c false otherwise + */ + bool Remove(std::string_view key); + + /** * @brief Merges values from the map 'from' to the current. * * Any values in 'from' will overwrite the values in the current map. -- 2.7.4