2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/public-api/object/property-map.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/vector-wrapper.h>
31 typedef std::vector<StringValuePair> StringValueContainer;
33 using IndexValuePair = std::pair<Property::Index, Property::Value>;
34 using IndexValueContainer = std::vector<IndexValuePair>;
36 }; // unnamed namespace
38 struct Property::Map::Impl
40 StringValueContainer mStringValueContainer;
41 IndexValueContainer mIndexValueContainer;
49 Property::Map::Map(const std::initializer_list<KeyValuePair>& values)
52 for(auto&& value : values)
54 const auto& key = value.first;
57 case Property::Key::INDEX:
59 Property::Map::Insert(key.indexKey, value.second);
62 case Property::Key::STRING:
64 Property::Map::Insert(key.stringKey, value.second);
71 Property::Map::Map(const Property::Map& other)
74 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
75 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
78 Property::Map::Map(Property::Map&& other)
81 other.mImpl = nullptr;
89 Property::Map::SizeType Property::Map::Count() const
91 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
92 return mImpl->mStringValueContainer.size() + mImpl->mIndexValueContainer.size();
95 bool Property::Map::Empty() const
97 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
98 return mImpl->mStringValueContainer.empty() && mImpl->mIndexValueContainer.empty();
101 void Property::Map::Insert(std::string key, Value value)
103 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
104 mImpl->mStringValueContainer.push_back(std::make_pair(std::move(key), std::move(value)));
107 void Property::Map::Insert(Property::Index key, Value value)
109 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
110 mImpl->mIndexValueContainer.push_back(std::make_pair(key, std::move(value)));
113 Property::Value& Property::Map::GetValue(SizeType position) const
115 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
116 SizeType numStringKeys = mImpl->mStringValueContainer.size();
117 SizeType numIndexKeys = mImpl->mIndexValueContainer.size();
118 DALI_ASSERT_ALWAYS(position < (numStringKeys + numIndexKeys) && "position out-of-bounds");
120 if(position < numStringKeys)
122 return mImpl->mStringValueContainer[position].second;
126 return mImpl->mIndexValueContainer[position - numStringKeys].second;
130 const std::string& Property::Map::GetKey(SizeType position) const
132 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetKey() is deprecated and will be removed from next release.\n");
134 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
135 SizeType numStringKeys = mImpl->mStringValueContainer.size();
136 DALI_ASSERT_ALWAYS(position < numStringKeys && "position out-of-bounds");
138 return mImpl->mStringValueContainer[position].first;
141 Property::Key Property::Map::GetKeyAt(SizeType position) const
143 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
145 SizeType numStringKeys = mImpl->mStringValueContainer.size();
146 SizeType numIndexKeys = mImpl->mIndexValueContainer.size();
147 DALI_ASSERT_ALWAYS(position < (numStringKeys + numIndexKeys) && "position out-of-bounds");
149 if(position < numStringKeys)
151 Key key(mImpl->mStringValueContainer[position].first);
156 Key key(mImpl->mIndexValueContainer[position - numStringKeys].first);
161 StringValuePair& Property::Map::GetPair(SizeType position) const
163 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetPair() is deprecated and will be removed from next release.\n");
165 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
167 SizeType numStringKeys = mImpl->mStringValueContainer.size();
169 DALI_ASSERT_ALWAYS(position < (numStringKeys) && "position out-of-bounds");
171 return mImpl->mStringValueContainer[position];
174 KeyValuePair Property::Map::GetKeyValue(SizeType position) const
176 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
178 SizeType numStringKeys = mImpl->mStringValueContainer.size();
179 SizeType numIndexKeys = mImpl->mIndexValueContainer.size();
181 DALI_ASSERT_ALWAYS(position < (numStringKeys + numIndexKeys) && "position out-of-bounds");
183 if(position < numStringKeys)
185 Key key(mImpl->mStringValueContainer[position].first);
186 KeyValuePair keyValue(key, mImpl->mStringValueContainer[position].second);
191 return mImpl->mIndexValueContainer[position - numStringKeys];
195 Property::Value* Property::Map::Find(std::string_view key) const
197 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
199 for(auto&& iter : mImpl->mStringValueContainer)
201 if(key == iter.first)
206 return nullptr; // Not found
209 Property::Value* Property::Map::Find(Property::Index key) const
211 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
213 for(auto&& iter : mImpl->mIndexValueContainer)
215 if(iter.first == key)
220 return nullptr; // Not found
223 Property::Value* Property::Map::Find(Property::Index indexKey, std::string_view stringKey) const
225 Property::Value* valuePtr = Find(indexKey);
228 valuePtr = Find(stringKey);
233 Property::Value* Property::Map::Find(std::string_view key, Property::Type type) const
235 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
237 for(auto&& iter : mImpl->mStringValueContainer)
239 if((iter.second.GetType() == type) && (key == iter.first))
244 return nullptr; // Not found
247 Property::Value* Property::Map::Find(Property::Index key, Property::Type type) const
249 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
251 for(auto&& iter : mImpl->mIndexValueContainer)
253 if((iter.second.GetType() == type) && (iter.first == key))
258 return nullptr; // Not found
261 void Property::Map::Clear()
263 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
265 mImpl->mStringValueContainer.clear();
266 mImpl->mIndexValueContainer.clear();
269 void Property::Map::Merge(const Property::Map& from)
271 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
273 // Ensure we're not attempting to merge with ourself
278 for(auto&& iter : from.mImpl->mStringValueContainer)
280 (*this)[iter.first] = iter.second;
283 for(auto&& iter : from.mImpl->mIndexValueContainer)
285 (*this)[iter.first] = iter.second;
290 // If we're empty, then just copy
296 const Property::Value& Property::Map::operator[](std::string_view key) const
298 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
300 for(auto&& iter : mImpl->mStringValueContainer)
302 if(iter.first == key)
308 DALI_ASSERT_ALWAYS(!"Invalid Key");
311 Property::Value& Property::Map::operator[](std::string_view key)
313 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
315 for(auto&& iter : mImpl->mStringValueContainer)
317 if(iter.first == key)
323 // Create and return reference to new value
324 mImpl->mStringValueContainer.push_back(std::make_pair(std::string(key), Property::Value()));
325 return mImpl->mStringValueContainer.back().second;
328 const Property::Value& Property::Map::operator[](Property::Index key) const
330 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
332 for(auto&& iter : mImpl->mIndexValueContainer)
334 if(iter.first == key)
340 DALI_ASSERT_ALWAYS(!"Invalid Key");
343 Property::Value& Property::Map::operator[](Property::Index key)
345 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
347 for(auto&& iter : mImpl->mIndexValueContainer)
349 if(iter.first == key)
355 // Create and return reference to new value
356 mImpl->mIndexValueContainer.push_back(std::make_pair(key, Property::Value()));
357 return mImpl->mIndexValueContainer.back().second;
360 Property::Map& Property::Map::operator=(const Property::Map& other)
362 DALI_ASSERT_DEBUG(mImpl && "Cannot use an object previously used as an r-value");
366 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
367 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
372 Property::Map& Property::Map::operator=(Property::Map&& other)
378 other.mImpl = nullptr;
383 std::ostream& operator<<(std::ostream& stream, const Property::Map& map)
385 stream << "Map(" << map.Count() << ") = {";
390 // Output the String-Value pairs
391 for(auto&& iter : map.mImpl->mStringValueContainer)
397 stream << iter.first << ":" << iter.second;
400 // Output the Index-Value pairs
401 for(auto&& iter : map.mImpl->mIndexValueContainer)
407 stream << iter.first << ":" << iter.second;