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