From: Subhransu Mohanty Date: Thu, 24 Sep 2020 02:02:36 +0000 (+0900) Subject: refactor Property::Map interface. X-Git-Tag: dali_1.9.35~2^2 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-core.git;a=commitdiff_plain;h=e2506e1881fdc574ec7a3dabc6775db4193091e2 refactor Property::Map interface. - take std::string and Property::Value by value. - use std::string_view in the find interface. Change-Id: I8bcedb15f1b84663520a79b0e4a724e0cb28487b --- diff --git a/dali/public-api/object/property-map.cpp b/dali/public-api/object/property-map.cpp index 4e9aaaa..7a500c6 100644 --- a/dali/public-api/object/property-map.cpp +++ b/dali/public-api/object/property-map.cpp @@ -98,22 +98,16 @@ bool Property::Map::Empty() const return mImpl->mStringValueContainer.empty() && mImpl->mIndexValueContainer.empty(); } -void Property::Map::Insert(const char* key, const Value& value) +void Property::Map::Insert(std::string key, Value value) { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); - mImpl->mStringValueContainer.push_back(std::make_pair(key, value)); + mImpl->mStringValueContainer.push_back(std::make_pair(std::move(key), std::move(value))); } -void Property::Map::Insert(const std::string& key, const Value& value) +void Property::Map::Insert(Property::Index key, Value value) { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); - mImpl->mStringValueContainer.push_back(std::make_pair(key, value)); -} - -void Property::Map::Insert(Property::Index key, const Value& value) -{ - DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); - mImpl->mIndexValueContainer.push_back(std::make_pair(key, value)); + mImpl->mIndexValueContainer.push_back(std::make_pair(key, std::move(value))); } Property::Value& Property::Map::GetValue(SizeType position) const @@ -194,19 +188,17 @@ KeyValuePair Property::Map::GetKeyValue(SizeType position) const } else { - Key key(mImpl->mIndexValueContainer[position - numStringKeys].first); - KeyValuePair keyValue(key, mImpl->mIndexValueContainer[position - numStringKeys].second); - return keyValue; + return mImpl->mIndexValueContainer[position - numStringKeys]; } } -Property::Value* Property::Map::Find(const char* key) const +Property::Value* Property::Map::Find(std::string_view key) const { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); for(auto&& iter : mImpl->mStringValueContainer) { - if(iter.first == key) + if(key == iter.first) { return &iter.second; } @@ -214,11 +206,6 @@ Property::Value* Property::Map::Find(const char* key) const return nullptr; // Not found } -Property::Value* Property::Map::Find(const std::string& key) const -{ - return Find(key.c_str()); -} - Property::Value* Property::Map::Find(Property::Index key) const { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); @@ -233,7 +220,7 @@ Property::Value* Property::Map::Find(Property::Index key) const return nullptr; // Not found } -Property::Value* Property::Map::Find(Property::Index indexKey, const std::string& stringKey) const +Property::Value* Property::Map::Find(Property::Index indexKey, std::string_view stringKey) const { Property::Value* valuePtr = Find(indexKey); if(!valuePtr) @@ -243,13 +230,13 @@ Property::Value* Property::Map::Find(Property::Index indexKey, const std::string return valuePtr; } -Property::Value* Property::Map::Find(const std::string& key, Property::Type type) const +Property::Value* Property::Map::Find(std::string_view key, Property::Type type) const { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); for(auto&& iter : mImpl->mStringValueContainer) { - if((iter.second.GetType() == type) && (iter.first == key)) + if((iter.second.GetType() == type) && (key == iter.first)) { return &iter.second; } @@ -306,7 +293,7 @@ void Property::Map::Merge(const Property::Map& from) } } -const Property::Value& Property::Map::operator[](const std::string& key) const +const Property::Value& Property::Map::operator[](std::string_view key) const { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); @@ -321,7 +308,7 @@ const Property::Value& Property::Map::operator[](const std::string& key) const DALI_ASSERT_ALWAYS(!"Invalid Key"); } -Property::Value& Property::Map::operator[](const std::string& key) +Property::Value& Property::Map::operator[](std::string_view key) { DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value"); @@ -334,8 +321,8 @@ Property::Value& Property::Map::operator[](const std::string& key) } // Create and return reference to new value - mImpl->mStringValueContainer.push_back(std::make_pair(key, Property::Value())); - return (mImpl->mStringValueContainer.end() - 1)->second; + mImpl->mStringValueContainer.push_back(std::make_pair(std::string(key), Property::Value())); + return mImpl->mStringValueContainer.back().second; } const Property::Value& Property::Map::operator[](Property::Index key) const @@ -367,7 +354,7 @@ Property::Value& Property::Map::operator[](Property::Index key) // Create and return reference to new value mImpl->mIndexValueContainer.push_back(std::make_pair(key, Property::Value())); - return (mImpl->mIndexValueContainer.end() - 1)->second; + return mImpl->mIndexValueContainer.back().second; } Property::Map& Property::Map::operator=(const Property::Map& other) diff --git a/dali/public-api/object/property-map.h b/dali/public-api/object/property-map.h index 115a7fa..45431f5 100644 --- a/dali/public-api/object/property-map.h +++ b/dali/public-api/object/property-map.h @@ -22,6 +22,7 @@ #include #include #include +#include // INTERNAL INCLUDES #include @@ -109,17 +110,7 @@ public: * @param[in] key The key to insert * @param[in] value The value to insert */ - void Insert(const char* key, const Value& value); - - /** - * @brief Inserts the key-value pair in the Map, with the key type as string. - * - * Does not check for duplicates. - * @SINCE_1_0.0 - * @param[in] key The key to insert - * @param[in] value The value to insert - */ - void Insert(const std::string& key, const Value& value); + void Insert(std::string key, Value value); /** * @brief Inserts the key-value pair in the Map, with the key type as index. @@ -129,7 +120,7 @@ public: * @param[in] key The key to insert * @param[in] value The value to insert */ - void Insert(Property::Index key, const Value& value); + void Insert(Property::Index key, Value value); /** * @brief Inserts the key-value pair in the Map, with the key type as string. @@ -140,24 +131,9 @@ public: * @param value to insert * @return a reference to this object */ - inline Property::Map& Add(const char* key, const Value& value) + Property::Map& Add(std::string key, Value value) { - Insert(key, value); - return *this; - } - - /** - * @brief Inserts the key-value pair in the Map, with the key type as string. - * - * Does not check for duplicates - * @SINCE_1_2.5 - * @param key to insert - * @param value to insert - * @return a reference to this object - */ - inline Property::Map& Add(const std::string& key, const Value& value) - { - Insert(key, value); + Insert(std::move(key), std::move(value)); return *this; } @@ -170,9 +146,9 @@ public: * @param value to insert * @return a reference to this object */ - inline Property::Map& Add(Property::Index key, const Value& value) + Property::Map& Add(Property::Index key, Value value) { - Insert(key, value); + Insert(key, std::move(value)); return *this; } @@ -243,17 +219,7 @@ public: * * @return A const pointer to the value if it exists, NULL otherwise */ - Value* Find(const char* key) const; - - /** - * @brief Finds the value for the specified key if it exists. - * - * @SINCE_1_0.0 - * @param[in] key The key to find - * - * @return A const pointer to the value if it exists, NULL otherwise - */ - Value* Find(const std::string& key) const; + Value* Find(std::string_view key) const; /** * @brief Finds the value for the specified key if it exists. @@ -276,7 +242,7 @@ public: * * @return A const pointer to the value if it exists, NULL otherwise */ - Value* Find(Property::Index indexKey, const std::string& stringKey) const; + Value* Find(Property::Index indexKey, std::string_view stringKey) const; /** * @brief Finds the value for the specified key if it exists and its type is type. @@ -287,7 +253,7 @@ public: * * @return A const pointer to the value if it exists, NULL otherwise */ - Value* Find(const std::string& key, Property::Type type) const; + Value* Find(std::string_view key, Property::Type type) const; /** * @brief Finds the value for the specified key if it exists and its type is type. @@ -326,7 +292,7 @@ public: * * @note Will assert if invalid-key is given. */ - const Value& operator[](const std::string& key) const; + const Value& operator[](std::string_view key) const; /** * @brief Operator to access the element with the specified string key. @@ -338,7 +304,7 @@ public: * * @note If an element with the key does not exist, then it is created. */ - Value& operator[](const std::string& key); + Value& operator[](std::string_view key); /** * @brief Const operator to access element with the specified index key.