1 #ifndef DALI_PROPERTY_MAP_H
2 #define DALI_PROPERTY_MAP_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
24 #include <initializer_list>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/object/property.h>
29 #include <dali/public-api/object/property-key.h>
30 #include <dali/public-api/object/property-value.h>
35 * @addtogroup dali_core_object
39 using KeyValuePair = std::pair<Property::Key, Property::Value>;
40 using StringValuePair = std::pair<std::string, Property::Value>;
43 * @brief A Map of property values, the key type could be String or Property::Index.
46 class DALI_CORE_API Property::Map
49 using SizeType = std::size_t;
52 * @brief Default constructor.
58 * @brief Constructor from initializer_list.
61 * @param[in] values An initializer_list of pairs of index and value.
63 Map( const std::initializer_list< KeyValuePair >& values );
66 * @brief Copy constructor.
69 * @param[in] other The Map to copy from
71 Map( const Map& other );
74 * @brief Move constructor.
77 * @param[in] other The Map to move from
78 * @note After the @a other array is used, it becomes invalid and is no longer usable.
83 * @brief Non-virtual destructor.
89 * @brief Retrieves the number of elements in the map.
92 * @return The number of elements in the map
94 SizeType Count() const;
97 * @brief Returns whether the map is empty.
100 * @return @c true if empty, @c false otherwise
105 * @brief Inserts the key-value pair in the Map, with the key type as string.
107 * Does not check for duplicates.
109 * @param[in] key The key to insert
110 * @param[in] value The value to insert
112 void Insert( const char* key, const Value& value );
115 * @brief Inserts the key-value pair in the Map, with the key type as string.
117 * Does not check for duplicates.
119 * @param[in] key The key to insert
120 * @param[in] value The value to insert
122 void Insert( const std::string& key, const Value& value );
125 * @brief Inserts the key-value pair in the Map, with the key type as index.
127 * Does not check for duplicates.
129 * @param[in] key The key to insert
130 * @param[in] value The value to insert
132 void Insert( Property::Index key, const Value& value );
136 * @brief Inserts the key-value pair in the Map, with the key type as string.
138 * Does not check for duplicates
140 * @param key to insert
141 * @param value to insert
142 * @return a reference to this object
144 inline Property::Map& Add( const char* key, const Value& value )
151 * @brief Inserts the key-value pair in the Map, with the key type as string.
153 * Does not check for duplicates
155 * @param key to insert
156 * @param value to insert
157 * @return a reference to this object
159 inline Property::Map& Add( const std::string& key, const Value& value )
167 * @brief Inserts the key-value pair in the Map, with the key type as index.
169 * Does not check for duplicates
171 * @param key to insert
172 * @param value to insert
173 * @return a reference to this object
175 inline Property::Map& Add( Property::Index key, const Value& value )
182 * @brief Retrieves the value at the specified position.
185 * @param[in] position The specified position
186 * @return A reference to the value at the specified position
188 * @note Will assert if position >= Count()
190 Value& GetValue( SizeType position ) const;
193 * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
195 * @brief Retrieves the key at the specified position.
198 * @param[in] position The specified position
199 * @return A const reference to the key at the specified position
201 * @note Will assert if position >= Count()
203 const std::string& GetKey( SizeType position ) const DALI_DEPRECATED_API;
206 * @brief Retrieve the key at the specified position.
209 * @param[in] position The specified position
210 * @return A copy of the key at the specified position.
212 * @note Will assert if position >= Count()
214 Key GetKeyAt( SizeType position ) const;
217 * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
219 * @brief Retrieves the key & the value at the specified position.
222 * @param[in] position The specified position
223 * @return A reference to the pair of key and value at the specified position
225 * @note Will assert if position >= Count() or key at position is an index key.
227 StringValuePair& GetPair( SizeType position ) const DALI_DEPRECATED_API;
230 * @brief Retrieve the key & the value at the specified position.
233 * @param[in] position The specified position
234 * @return A copy of the pair of key and value at the specified position.
236 * @note Will assert if position >= Count()
238 KeyValuePair GetKeyValue( SizeType position ) const;
241 * @brief Finds the value for the specified key if it exists.
244 * @param[in] key The key to find
246 * @return A const pointer to the value if it exists, NULL otherwise
248 Value* Find( const char* key ) const;
251 * @brief Finds the value for the specified key if it exists.
254 * @param[in] key The key to find
256 * @return A const pointer to the value if it exists, NULL otherwise
258 Value* Find( const std::string& key ) const;
261 * @brief Finds the value for the specified key if it exists.
264 * @param[in] key The key to find
266 * @return A const pointer to the value if it exists, NULL otherwise
268 Value* Find( Property::Index key ) const;
271 * @brief Finds the value for the specified keys if either exist.
273 * Will search for the index key first.
276 * @param[in] indexKey The index key to find
277 * @param[in] stringKey The string key to find
279 * @return A const pointer to the value if it exists, NULL otherwise
281 Value* Find( Property::Index indexKey, const std::string& stringKey ) const;
284 * @brief Finds the value for the specified key if it exists and its type is type.
287 * @param[in] key The key to find
288 * @param[in] type The type to check
290 * @return A const pointer to the value if it exists, NULL otherwise
292 Value* Find( const std::string& key, Property::Type type ) const;
295 * @brief Finds the value for the specified key if it exists and its type is type.
298 * @param[in] key The key to find
299 * @param[in] type The type to check
301 * @return A const pointer to the value if it exists, NULL otherwise
303 Value* Find( Property::Index key, Property::Type type ) const;
306 * @brief Clears the map.
312 * @brief Merges values from the map 'from' to the current.
314 * Any values in 'from' will overwrite the values in the current map.
317 * @param[in] from The map to merge from
319 void Merge( const Map& from );
322 * @brief Const operator to access element with the specified string key.
325 * @param[in] key The key whose value to access
327 * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned
329 * @note Will assert if invalid-key is given.
331 const Value& operator[]( const std::string& key ) const;
334 * @brief Operator to access the element with the specified string key.
337 * @param[in] key The key whose value to access
339 * @return A reference to the value for the element with the specified key
341 * @note If an element with the key does not exist, then it is created.
343 Value& operator[]( const std::string& key );
346 * @brief Const operator to access element with the specified index key.
349 * @param[in] key The key whose value to access
351 * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned
353 * @note Will assert if invalid-key is given.
355 const Value& operator[]( Property::Index key ) const;
358 * @brief Operator to access the element with the specified index key.
361 * @param[in] key The key whose value to access
363 * @return A reference to the value for the element with the specified key
365 * @note If an element with the key does not exist, then it is created.
367 Value& operator[]( Property::Index key );
370 * @brief Assignment operator.
373 * @param[in] other The map to copy from
375 * @return The copied map
377 Map& operator=( const Map& other );
380 * @brief Move assignment operator.
383 * @param[in] other The map to move from
385 * @return The moved map
387 * @note The other array is an r-value so becomes invalid and is no longer usable.
389 Map& operator=( Map&& other );
392 * @brief Output to stream.
395 friend DALI_CORE_API std::ostream& operator<<( std::ostream& stream, const Property::Map& map );
398 struct DALI_INTERNAL Impl; ///< Private data
399 Impl* mImpl; ///< Pointer to private data
403 * @brief Converts the key/value pairs of the property map into a string and append to an output stream.
406 * @param[in] stream The output stream operator
407 * @param[in] map The map to insert
408 * @return The output stream operator
410 DALI_CORE_API std::ostream& operator<<( std::ostream& stream, const Property::Map& map );
417 #endif // DALI_PROPERTY_MAP_H