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