[dali_1.9.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-value.h
1 #ifndef DALI_PROPERTY_VALUE_H
2 #define DALI_PROPERTY_VALUE_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 <iosfwd>
24 #include <type_traits>
25 #include <utility>
26
27 // INTERNAL INCLUDES
28 #include <dali/public-api/math/rect.h>
29 #include <dali/public-api/object/property.h>
30
31 namespace Dali
32 {
33 /**
34  * @addtogroup dali_core_object
35  * @{
36  */
37
38 struct AngleAxis;
39 class Quaternion;
40 struct Vector2;
41 struct Vector3;
42 struct Vector4;
43 class Matrix3;
44 class Matrix;
45 struct Extents;
46
47 using KeyValuePair = std::pair<Property::Key, Property::Value>;
48
49 /**
50  * @brief A value-type representing a property value.
51  * @SINCE_1_0.0
52  */
53 class DALI_CORE_API Property::Value
54 {
55 public:
56   /**
57    * @brief Default constructor.
58    *
59    * This creates a property with type Property::NONE.
60    * @SINCE_1_0.0
61    */
62   Value();
63
64   /**
65    * @brief Creates a boolean property value.
66    *
67    * @SINCE_1_0.0
68    * @param[in] boolValue A boolean value
69    */
70   Value(bool boolValue);
71
72   /**
73    * @brief Creates an integer property value.
74    *
75    * @SINCE_1_0.0
76    * @param[in] integerValue An integer value
77    */
78   Value(int32_t integerValue);
79
80   /**
81    * @brief Creates a float property value.
82    *
83    * @SINCE_1_0.0
84    * @param[in] floatValue A floating-point value
85    */
86   Value(float floatValue);
87
88   /**
89    * @brief Creates a Vector2 property value.
90    *
91    * @SINCE_1_0.0
92    * @param[in] vectorValue A vector of 2 floating-point values
93    */
94   Value(const Vector2& vectorValue);
95
96   /**
97    * @brief Creates a Vector3 property value.
98    *
99    * @SINCE_1_0.0
100    * @param[in] vectorValue A vector of 3 floating-point values
101    */
102   Value(const Vector3& vectorValue);
103
104   /**
105    * @brief Creates a Vector4 property value.
106    *
107    * @SINCE_1_0.0
108    * @param[in] vectorValue A vector of 4 floating-point values
109    */
110   Value(const Vector4& vectorValue);
111
112   /**
113    * @brief Creates a Matrix3 property value.
114    *
115    * @SINCE_1_0.0
116    * @param[in] matrixValue A matrix of 3x3 floating-point values
117    */
118   Value(const Matrix3& matrixValue);
119
120   /**
121    * @brief Creates a Matrix property value.
122    *
123    * @SINCE_1_0.0
124    * @param[in] matrixValue A matrix of 4x4 floating-point values
125    */
126   Value(const Matrix& matrixValue);
127
128   /**
129    * @brief Creates a Vector4 property value.
130    *
131    * @SINCE_1_0.0
132    * @param[in] vectorValue A vector of 4 integer values
133    */
134   Value(const Rect<int32_t>& vectorValue);
135
136   /**
137    * @brief Creates a Vector4 property value.
138    *
139    * @SINCE_1_9.14
140    * @param[in] vectorValue A vector of 4 float values
141    */
142   Value(const Rect<float>& vectorValue);
143
144   /**
145    * @brief Creates an orientation property value.
146    *
147    * @SINCE_1_0.0
148    * @param[in] angleAxis An angle-axis representing the rotation
149    */
150   Value(const AngleAxis& angleAxis);
151
152   /**
153    * @brief Creates an orientation property value.
154    *
155    * @SINCE_1_0.0
156    * @param[in] quaternion A quaternion representing the rotation
157    */
158   Value(const Quaternion& quaternion);
159
160   /**
161    * @brief Creates an string property value.
162    *
163    * @SINCE_1_0.0
164    * @param[in] stringValue A string
165    */
166   Value(const std::string& stringValue);
167
168   /**
169    * @brief Creates a string property value.
170    *
171    * @SINCE_1_0.0
172    * @param[in] stringValue A string
173    */
174   Value(const char* stringValue);
175
176   /**
177    * @brief Creates an array property value.
178    *
179    * @SINCE_1_0.0
180    * @param[in] arrayValue An array
181    */
182   Value(Property::Array& arrayValue);
183
184   /**
185    * @brief Creates an array property value.
186    *
187    * @SINCE_1_4.16
188    * @param[in] arrayValue An r-value array
189    */
190   Value(Property::Array&& arrayValue);
191
192   /**
193    * @brief Creates a map property value.
194    *
195    * @SINCE_1_0.0
196    * @param[in] mapValue A map
197    */
198   Value(Property::Map& mapValue);
199
200   /**
201    * @brief Creates a map property value.
202    *
203    * @SINCE_1_4.16
204    * @param[in] mapValue An r-value map
205    */
206   Value(Property::Map&& mapValue);
207
208   /**
209    * @brief Create a map property value from an initializer_list.
210    *
211    * @SINCE_1_4.16
212    * @param [in] values An initializer_list of pairs of index and value.
213    */
214   Value(const std::initializer_list<KeyValuePair>& values);
215
216   /**
217    * @brief Creates an extents property value.
218    *
219    * @SINCE_1_2.62
220    * @param[in] extentsValue A collection of 4 uint16_t values
221    */
222   Value(const Extents& extentsValue);
223
224   /**
225    * @brief Creates an enumeration property value.
226    *
227    * @SINCE_1_4.36
228    * @param[in] enumValue An enumeration value
229    */
230   template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
231   Value(T enumValue)
232   : Value(static_cast<int32_t>(enumValue))
233   {
234   }
235
236   /**
237    * @brief Explicitly sets a type and initialize it.
238    *
239    * @SINCE_1_0.0
240    * @param[in] type The property value type
241    */
242   explicit Value(Type type);
243
244   /**
245    * @brief Copy constructor.
246    *
247    * @SINCE_1_0.0
248    * @param[in] value The property value to copy
249    */
250   Value(const Value& value);
251
252   /**
253    * @brief Move constructor.
254    *
255    * A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.
256    * @SINCE_1_4.16
257    * @param[in] value The property value to move from
258    */
259   Value(Value&& value);
260
261   /**
262    * @brief Assigns a property value.
263    *
264    * @SINCE_1_0.0
265    * @param[in] value The property value to assign from
266    * @return a reference to this
267    */
268   Value& operator=(const Value& value);
269
270   /**
271    * @brief Move assignment operator.
272    *
273    * @SINCE_1_4.16
274    * @param[in] value The property value to move from
275    * @return a reference to this
276    */
277   Value& operator=(Value&& value);
278
279   /**
280    * @brief Non-virtual destructor.
281    *
282    * This class is not a base class.
283    * @SINCE_1_0.0
284    */
285   ~Value();
286
287   /**
288    * @brief Queries the type of this property value.
289    *
290    * @SINCE_1_0.0
291    * @return The type ID
292    */
293   Type GetType() const;
294
295   /**
296    * @brief Retrieves a specific value.
297    *
298    * Works on a best-effort approach; if value type is different returns a default value of the type.
299    *
300    * @SINCE_1_4.36
301    * @return A value of type T
302    */
303   template<typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr>
304   T DALI_INTERNAL Get() const
305   {
306     T temp = T(); // value (zero) initialize
307     Get(temp);
308     return temp;
309   }
310
311   /**
312    * @brief Retrieves a specific value.
313    *
314    * Works on a best-effort approach; if value type is different returns a default value of the type.
315    * Specialization for enumeration values
316    *
317    * @SINCE_1_4.36
318    * @return A value of type T
319    */
320   template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
321   T DALI_INTERNAL Get() const
322   {
323     int32_t temp = 0; // value (zero) initialize
324     Get(temp);
325     return static_cast<T>(temp);
326   }
327
328   /**
329    * @brief Retrieves an enumeration value.
330    *
331    * @SINCE_1_4.36
332    * @param[out] enumValue On return, an enumeration value
333    * @return @c true if the value is successfully retrieved, @c false if the type is different
334    * @pre GetType() is any enumeration
335    */
336   template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
337   bool DALI_INTERNAL Get(T& enumValue) const
338   {
339     int32_t temp = 0;
340     if(!Get(temp))
341     {
342       return false;
343     }
344     enumValue = static_cast<T>(temp);
345     return true;
346   }
347
348   /**
349    * @brief Retrieves a boolean value.
350    *
351    * @SINCE_1_0.0
352    * @param[out] boolValue On return, a boolean value
353    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
354    * @pre GetType() is a type convertible to bool.
355    */
356   bool Get(bool& boolValue) const;
357
358   /**
359    * @brief Retrieves a floating-point value.
360    *
361    * @SINCE_1_0.0
362    * @param[out] floatValue On return, a floating-point value
363    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
364    * @pre GetType() is a type convertible to float.
365    */
366   bool Get(float& floatValue) const;
367
368   /**
369    * @brief Retrieves an integer value.
370    *
371    * @SINCE_1_0.0
372    * @param[out] integerValue On return, an integer value
373    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
374    * @pre GetType() is a type convertible to int.
375    */
376   bool Get(int32_t& integerValue) const;
377
378   /**
379    * @brief Retrieves an integer rectangle.
380    *
381    * @SINCE_1_0.0
382    * @param[out] rect On return, an integer rectangle
383    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
384    * @pre GetType() is a type convertible to Rect<int>.
385    */
386   bool Get(Rect<int32_t>& rect) const;
387
388   /**
389    * @brief Retrieves a vector value.
390    *
391    * @SINCE_1_0.0
392    * @param[out] vectorValue On return, a vector value
393    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
394    * @pre GetType() is a type convertible to Vector2.
395    */
396   bool Get(Vector2& vectorValue) const;
397
398   /**
399    * @brief Retrieves a vector value.
400    *
401    * @SINCE_1_0.0
402    * @param[out] vectorValue On return, a vector value
403    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
404    * @pre GetType() is a type convertible to Vector3.
405    */
406   bool Get(Vector3& vectorValue) const;
407
408   /**
409    * @brief Retrieves a vector value.
410    *
411    * @SINCE_1_0.0
412    * @param[out] vectorValue On return, a vector value
413    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
414    * @pre GetType() is a type convertible to Vector4.
415    */
416   bool Get(Vector4& vectorValue) const;
417
418   /**
419    * @brief Retrieves a matrix3 value.
420    *
421    * @SINCE_1_0.0
422    * @param[out] matrixValue On return, a matrix3 value
423    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
424    * @pre GetType() is a type convertible to Matrix3.
425    */
426   bool Get(Matrix3& matrixValue) const;
427
428   /**
429    * @brief Retrieves a matrix value.
430    *
431    * @SINCE_1_0.0
432    * @param[out] matrixValue On return, a matrix value
433    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
434    * @pre GetType() is a type convertible to Matrix.
435    */
436   bool Get(Matrix& matrixValue) const;
437
438   /**
439    * @brief Retrieves an angle-axis value.
440    *
441    * @SINCE_1_0.0
442    * @param[out] angleAxisValue On return, a angle-axis value
443    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
444    * @pre GetType() is a type convertible to AngleAxis.
445    */
446   bool Get(AngleAxis& angleAxisValue) const;
447
448   /**
449    * @brief Retrieves a quaternion value.
450    *
451    * @SINCE_1_0.0
452    * @param[out] quaternionValue On return, a quaternion value
453    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
454    * @pre GetType() is a type convertible to Quaternion.
455    */
456   bool Get(Quaternion& quaternionValue) const;
457
458   /**
459    * @brief Retrieves an string property value.
460    *
461    * @SINCE_1_0.0
462    * @param[out] stringValue A string
463    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
464    * @pre GetType() is a type convertible to string.
465    */
466   bool Get(std::string& stringValue) const;
467
468   /**
469    * @brief Retrieves an array property value.
470    *
471    * @SINCE_1_0.0
472    * @param[out] arrayValue The array as a vector Property Values
473    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
474    * @pre GetType() returns Property::ARRAY.
475    */
476   bool Get(Property::Array& arrayValue) const;
477
478   /**
479    * @brief Retrieves an map property value.
480    *
481    * @SINCE_1_0.0
482    * @param[out] mapValue The map as vector of string and Property Value pairs
483    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
484    * @pre GetType() returns Property::MAP.
485    */
486   bool Get(Property::Map& mapValue) const;
487
488   /**
489    * @brief Retrieves the Array API of the Property::Value without copying the contents of the map.
490    *
491    * @SINCE_1_0.0
492    * @return The Array API of the Property::Value or NULL if not a Property::Array
493    */
494   Property::Array* GetArray() const;
495
496   /**
497    * @brief Retrieves the Map API of the Property::Value without copying the contents of the map.
498    *
499    * @SINCE_1_0.0
500    * @return The Map API of the Property::Value or NULL if not a Property::Map
501    */
502   Property::Map* GetMap() const;
503
504   /**
505    * @brief Retrieves an extents.
506    *
507    * @SINCE_1_2.62
508    * @param[out] extentsValue Extents, a collection of 4 uint16_t
509    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
510    * @pre GetType() is a type convertible to Extents.
511    */
512   bool Get(Extents& extentsValue) const;
513
514   /**
515    * @brief Output to stream.
516    * @SINCE_1_0.0
517    */
518   friend DALI_CORE_API std::ostream& operator<<(std::ostream& ouputStream, const Property::Value& value);
519
520 private:
521   struct DALI_INTERNAL Impl;
522   Impl*                mImpl; ///< Pointer to the implementation
523 };
524
525 /**
526  * @brief Converts the value of the property into a string and append to an output stream.
527  *
528  * @SINCE_1_0.0
529  * @param[in] ouputStream The output stream operator
530  * @param[in] value The value to insert
531  * @return The output stream operator
532  */
533 DALI_CORE_API std::ostream& operator<<(std::ostream& ouputStream, const Property::Value& value);
534
535 /**
536  * @}
537  */
538 } // namespace Dali
539
540 #endif // DALI_PROPERTY_VALUE_H