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