(Property Map) Added method to find and return an index or equivalent string key
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-map.h
1 #ifndef __DALI_PROPERTY_MAP_H__
2 #define __DALI_PROPERTY_MAP_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/property-value.h>
27 #include <dali/public-api/object/property.h>
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_object
33  * @{
34  */
35
36 typedef std::pair<std::string, Property::Value> StringValuePair;
37
38 /**
39  * @brief A Map of property values, the key type could be String or Property::Index.
40  * @SINCE_1_0.0
41  */
42 class DALI_IMPORT_API Property::Map
43 {
44 public:
45
46   typedef std::size_t SizeType;
47
48   /**
49    * @brief Default constructor.
50    * @SINCE_1_0.0
51    */
52   Map();
53
54   /**
55    * @brief Copy Constructor.
56    *
57    * @SINCE_1_0.0
58    * @param[in] other The Map to copy from.
59    */
60   Map( const Map& other );
61
62   /**
63    * @brief Non-virtual destructor.
64    * @SINCE_1_0.0
65    */
66   ~Map();
67
68   /**
69    * @brief Retrieve the number of elements in the map.
70    *
71    * @SINCE_1_0.0
72    * @return The number of elements in the map.
73    */
74   SizeType Count() const;
75
76   /**
77    * @brief Returns whether the map is empty.
78    *
79    * @SINCE_1_0.0
80    * @return true if empty, false otherwise
81    */
82   bool Empty() const;
83
84   /**
85    * @brief Inserts the key-value pair in the Map, with the key type as string.
86    *
87    * Does not check for duplicates
88    * @SINCE_1_0.0
89    * @param key to insert
90    * @param value to insert
91    */
92   void Insert( const char* key, const Value& value );
93
94   /**
95    * @brief Inserts the key-value pair in the Map, with the key type as string.
96    *
97    * Does not check for duplicates
98    * @SINCE_1_0.0
99    * @param key to insert
100    * @param value to insert
101    */
102   void Insert( const std::string& key, const Value& value );
103
104   /**
105    * @brief Inserts the key-value pair in the Map, with the key type as index.
106    *
107    * Does not check for duplicates
108    * @SINCE_1_1.39
109    * @param key to insert
110    * @param value to insert
111    */
112   void Insert( Property::Index key, const Value& value );
113
114   /**
115    * DEPRECATED_1_1.39. Retrieve the value with key instead of position, Use Find( key ) instead.
116    *
117    * @brief Retrieve the value of the string-value pair at the specified position.
118    *
119    * @SINCE_1_0.0
120    * @return A reference to the value at the specified position.
121    *
122    * @note Will assert if position >= Count()
123    */
124   Value& GetValue( SizeType position ) const;
125
126   /**
127    * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
128    *
129    * @brief Retrieve the key at the specified position.
130    *
131    * @SINCE_1_0.0
132    * @return A const reference to the key at the specified position.
133    *
134    * @note Will assert if position >= Count()
135    */
136   const std::string& GetKey( SizeType position ) const;
137
138   /**
139    * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
140    *
141    * @brief Retrieve the key & the value at the specified position.
142    *
143    * @SINCE_1_0.0
144    * @return A reference to the pair of key and value at the specified position.
145    *
146    * @note Will assert if position >= Count()
147    */
148   StringValuePair& GetPair( SizeType position ) const;
149
150   /**
151    * @brief Finds the value for the specified key if it exists.
152    *
153    * @SINCE_1_0.0
154    * @param[in]  key   The key to find.
155    *
156    * @return A const pointer to the value if it exists, NULL otherwise
157    */
158   Value* Find( const char* key ) const;
159
160   /**
161    * @brief Finds the value for the specified key if it exists.
162    *
163    * @SINCE_1_0.0
164    * @param[in]  key   The key to find.
165    *
166    * @return A const pointer to the value if it exists, NULL otherwise
167    */
168   Value* Find( const std::string& key ) const;
169
170   /**
171    * @brief Finds the value for the specified key if it exists.
172    *
173    * @SINCE_1_1.39
174    * @param[in]  key   The key to find.
175    *
176    * @return A const pointer to the value if it exists, NULL otherwise
177    */
178   Value* Find( Property::Index key ) const;
179
180   /**
181    * @brief Finds the value for the specified keys if either exist.
182    *
183    * Will search for the index key first.
184    *
185    * @SINCE_1_1.45
186    * @param[in]  indexKey   The index key to find.
187    * @param[in]  stringKey  The string key to find.
188    *
189    * @return A const pointer to the value if it exists, NULL otherwise
190    */
191   Value* Find( Property::Index indexKey, const std::string& stringKey ) const;
192
193   /**
194    * @brief Finds the value for the specified key if it exists and its type is type
195    *
196    * @SINCE_1_0.0
197    * @param[in]  key   The key to find.
198    * @param[in]  type  The type to check.
199    *
200    * @return A const pointer to the value if it exists, NULL otherwise
201    */
202   Value* Find( const std::string& key, Property::Type type ) const;
203
204   /**
205    * @brief Finds the value for the specified key if it exists and its type is type
206    *
207    * @SINCE_1_1.39
208    * @param[in]  key   The key to find.
209    * @param[in]  type  The type to check.
210    *
211    * @return A const pointer to the value if it exists, NULL otherwise
212    */
213   Value* Find( Property::Index key, Property::Type type ) const;
214
215   /**
216    * @brief Clears the map.
217    * @SINCE_1_0.0
218    */
219   void Clear();
220
221   /**
222    * @brief Merges values from the map 'from' to the current.
223    *
224    * Any values in 'from' will overwrite the values in the current map.
225    *
226    * @SINCE_1_0.0
227    * @param[in]  from  The map to merge from.
228    */
229   void Merge( const Map& from );
230
231   /**
232    * @brief Const operator to access element with the specified string key.
233    *
234    * @SINCE_1_0.0
235    * @param[in] key The key whose value to access.
236    *
237    * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned.
238    *
239    * @note Will assert if invalid-key is given.
240    */
241   const Value& operator[]( const std::string& key ) const;
242
243   /**
244    * @brief Operator to access the element with the specified string key.
245    *
246    * @SINCE_1_0.0
247    * @param[in] key The key whose value to access.
248    *
249    * @return A reference to the value for the element with the specified key.
250    *
251    * @note If an element with the key does not exist, then it is created.
252    */
253   Value& operator[]( const std::string& key );
254
255   /**
256    * @brief Const operator to access element with the specified index key.
257    *
258    * @SINCE_1_1.39
259    * @param[in] key The key whose value to access.
260    *
261    * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned.
262    *
263    * @note Will assert if invalid-key is given.
264    */
265   const Value& operator[]( Property::Index key ) const;
266
267   /**
268    * @brief Operator to access the element with the specified index key.
269    *
270    * @SINCE_1_1.39
271    * @param[in] key The key whose value to access.
272    *
273    * @return A reference to the value for the element with the specified key.
274    *
275    * @note If an element with the key does not exist, then it is created.
276    */
277   Value& operator[]( Property::Index key );
278
279   /**
280    * @brief Assignment Operator
281    *
282    * @SINCE_1_0.0
283    * @param[in] other The map to copy from.
284    *
285    * @return The copied map.
286    */
287   Map& operator=( const Map& other );
288
289   /**
290    * @brief output to stream
291    * @SINCE_1_1.28
292    */
293   friend std::ostream& operator<<( std::ostream& stream, const Property::Map& map );
294
295 private:
296   struct DALI_INTERNAL Impl; ///< Private data
297   Impl* mImpl; ///< Pointer to private data
298 };
299
300 /**
301  * @brief Convert the key/value pairs of the property map into a string and append to an output stream.
302  *
303  * @SINCE_1_1.28
304  * @param [in] stream The output stream operator.
305  * @param [in] map The map to insert
306  * @return The output stream operator.
307  */
308 DALI_IMPORT_API std::ostream& operator<<( std::ostream& stream, const Property::Map& map );
309
310
311 /**
312  * @}
313  */
314 } // namespace Dali
315
316 #endif // __DALI_PROPERTY_MAP_H__