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