Clean up the code to build successfully on macOS
[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) 2020 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 <initializer_list>
23 #include <sstream>
24 #include <string>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/object/property-key.h>
29 #include <dali/public-api/object/property-value.h>
30 #include <dali/public-api/object/property.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    * @brief Inserts the key-value pair in the Map, with the key type as string.
136    *
137    * Does not check for duplicates
138    * @SINCE_1_2.5
139    * @param key to insert
140    * @param value to insert
141    * @return a reference to this object
142    */
143   inline Property::Map& Add(const char* key, const Value& value)
144   {
145     Insert(key, value);
146     return *this;
147   }
148
149   /**
150    * @brief Inserts the key-value pair in the Map, with the key type as string.
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(const std::string& key, const Value& value)
159   {
160     Insert(key, value);
161     return *this;
162   }
163
164   /**
165    * @brief Inserts the key-value pair in the Map, with the key type as index.
166    *
167    * Does not check for duplicates
168    * @SINCE_1_2.5
169    * @param key to insert
170    * @param value to insert
171    * @return a reference to this object
172    */
173   inline Property::Map& Add(Property::Index key, const Value& value)
174   {
175     Insert(key, value);
176     return *this;
177   }
178
179   /**
180    * @brief Retrieves the value at the specified position.
181    *
182    * @SINCE_1_0.0
183    * @param[in] position The specified position
184    * @return A reference to the value at the specified position
185    *
186    * @note Will assert if position >= Count()
187    */
188   Value& GetValue(SizeType position) const;
189
190   /**
191    * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
192    *
193    * @brief Retrieves the key at the specified position.
194    *
195    * @SINCE_1_0.0
196    * @param[in] position The specified position
197    * @return A const reference to the key at the specified position
198    *
199    * @note Will assert if position >= Count()
200    */
201   const std::string& GetKey(SizeType position) const DALI_DEPRECATED_API;
202
203   /**
204    * @brief Retrieve the key at the specified position.
205    *
206    * @SINCE_1_2.7
207    * @param[in] position The specified position
208    * @return A copy of the key at the specified position.
209    *
210    * @note Will assert if position >= Count()
211    */
212   Key GetKeyAt(SizeType position) const;
213
214   /**
215    * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
216    *
217    * @brief Retrieves the key & the value at the specified position.
218    *
219    * @SINCE_1_0.0
220    * @param[in] position The specified position
221    * @return A reference to the pair of key and value at the specified position
222    *
223    * @note Will assert if position >= Count() or key at position is an index key.
224    */
225   StringValuePair& GetPair(SizeType position) const DALI_DEPRECATED_API;
226
227   /**
228    * @brief Retrieve the key & the value at the specified position.
229    *
230    * @SINCE_1_2.7
231    * @param[in] position The specified position
232    * @return A copy of the pair of key and value at the specified position.
233    *
234    * @note Will assert if position >= Count()
235    */
236   KeyValuePair GetKeyValue(SizeType position) const;
237
238   /**
239    * @brief Finds the value for the specified key if it exists.
240    *
241    * @SINCE_1_0.0
242    * @param[in] key The key to find
243    *
244    * @return A const pointer to the value if it exists, NULL otherwise
245    */
246   Value* Find(const char* key) const;
247
248   /**
249    * @brief Finds the value for the specified key if it exists.
250    *
251    * @SINCE_1_0.0
252    * @param[in] key The key to find
253    *
254    * @return A const pointer to the value if it exists, NULL otherwise
255    */
256   Value* Find(const std::string& key) const;
257
258   /**
259    * @brief Finds the value for the specified key if it exists.
260    *
261    * @SINCE_1_1.39
262    * @param[in] key The key to find
263    *
264    * @return A const pointer to the value if it exists, NULL otherwise
265    */
266   Value* Find(Property::Index key) const;
267
268   /**
269    * @brief Finds the value for the specified keys if either exist.
270    *
271    * Will search for the index key first.
272    *
273    * @SINCE_1_1.45
274    * @param[in] indexKey  The index key to find
275    * @param[in] stringKey The string key to find
276    *
277    * @return A const pointer to the value if it exists, NULL otherwise
278    */
279   Value* Find(Property::Index indexKey, const std::string& stringKey) const;
280
281   /**
282    * @brief Finds the value for the specified key if it exists and its type is type.
283    *
284    * @SINCE_1_0.0
285    * @param[in] key  The key to find
286    * @param[in] type The type to check
287    *
288    * @return A const pointer to the value if it exists, NULL otherwise
289    */
290   Value* Find(const std::string& key, Property::Type type) const;
291
292   /**
293    * @brief Finds the value for the specified key if it exists and its type is type.
294    *
295    * @SINCE_1_1.39
296    * @param[in] key  The key to find
297    * @param[in] type The type to check
298    *
299    * @return A const pointer to the value if it exists, NULL otherwise
300    */
301   Value* Find(Property::Index key, Property::Type type) const;
302
303   /**
304    * @brief Clears the map.
305    * @SINCE_1_0.0
306    */
307   void Clear();
308
309   /**
310    * @brief Merges values from the map 'from' to the current.
311    *
312    * Any values in 'from' will overwrite the values in the current map.
313    *
314    * @SINCE_1_0.0
315    * @param[in] from The map to merge from
316    */
317   void Merge(const Map& from);
318
319   /**
320    * @brief Const operator to access element with the specified string key.
321    *
322    * @SINCE_1_0.0
323    * @param[in] key The key whose value to access
324    *
325    * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned
326    *
327    * @note Will assert if invalid-key is given.
328    */
329   const Value& operator[](const std::string& key) const;
330
331   /**
332    * @brief Operator to access the element with the specified string key.
333    *
334    * @SINCE_1_0.0
335    * @param[in] key The key whose value to access
336    *
337    * @return A reference to the value for the element with the specified key
338    *
339    * @note If an element with the key does not exist, then it is created.
340    */
341   Value& operator[](const std::string& key);
342
343   /**
344    * @brief Const operator to access element with the specified index key.
345    *
346    * @SINCE_1_1.39
347    * @param[in] key The key whose value to access
348    *
349    * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned
350    *
351    * @note Will assert if invalid-key is given.
352    */
353   const Value& operator[](Property::Index key) const;
354
355   /**
356    * @brief Operator to access the element with the specified index key.
357    *
358    * @SINCE_1_1.39
359    * @param[in] key The key whose value to access
360    *
361    * @return A reference to the value for the element with the specified key
362    *
363    * @note If an element with the key does not exist, then it is created.
364    */
365   Value& operator[](Property::Index key);
366
367   /**
368    * @brief Assignment operator.
369    *
370    * @SINCE_1_0.0
371    * @param[in] other The map to copy from
372    *
373    * @return The copied map
374    */
375   Map& operator=(const Map& other);
376
377   /**
378    * @brief Move assignment operator.
379    *
380    * @SINCE_1_4.17
381    * @param[in] other The map to move from
382    *
383    * @return The moved map
384    *
385    * @note The other array is an r-value so becomes invalid and is no longer usable.
386    */
387   Map& operator=(Map&& other);
388
389   /**
390    * @brief Output to stream.
391    * @SINCE_1_1.28
392    */
393   friend DALI_CORE_API std::ostream& operator<<(std::ostream& stream, const Property::Map& map);
394
395 private:
396   struct DALI_INTERNAL Impl;  ///< Private data
397   Impl*                mImpl; ///< Pointer to private data
398 };
399
400 /**
401  * @brief Converts the key/value pairs of the property map into a string and append to an output stream.
402  *
403  * @SINCE_1_1.28
404  * @param[in] stream The output stream operator
405  * @param[in] map The map to insert
406  * @return The output stream operator
407  */
408 DALI_CORE_API std::ostream& operator<<(std::ostream& stream, const Property::Map& map);
409
410 /**
411  * @}
412  */
413 } // namespace Dali
414
415 #endif // DALI_PROPERTY_MAP_H