Refactor Property::Value::Impl class
[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(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_9.30
180    * @param[in] arrayValue A property array
181    */
182   Value(Property::Array arrayValue);
183
184   /**
185    * @brief Creates a map property value.
186    *
187    * @SINCE_1_9.30
188    * @param[in] mapValue A property map
189    */
190   Value(Property::Map mapValue);
191
192   /**
193    * @brief Create a map property value from an initializer_list.
194    *
195    * @SINCE_1_4.16
196    * @param [in] values An initializer_list of pairs of index and value.
197    */
198   Value(const std::initializer_list<KeyValuePair>& values);
199
200   /**
201    * @brief Creates an extents property value.
202    *
203    * @SINCE_1_2.62
204    * @param[in] extentsValue A collection of 4 uint16_t values
205    */
206   Value(const Extents& extentsValue);
207
208   /**
209    * @brief Creates an enumeration property value.
210    *
211    * @SINCE_1_4.36
212    * @param[in] enumValue An enumeration value
213    */
214   template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
215   Value(T enumValue)
216   : Value(static_cast<int32_t>(enumValue))
217   {
218   }
219
220   /**
221    * @brief Explicitly sets a type and initialize it.
222    *
223    * @SINCE_1_0.0
224    * @param[in] type The property value type
225    */
226   explicit Value(Type type);
227
228   /**
229    * @brief Copy constructor.
230    *
231    * @SINCE_1_0.0
232    * @param[in] value The property value to copy
233    */
234   Value(const Value& value);
235
236   /**
237    * @brief Move constructor.
238    *
239    * A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.
240    * @SINCE_1_4.16
241    * @param[in] value The property value to move from
242    */
243   Value(Value&& value) noexcept;
244
245   /**
246    * @brief Assigns a property value.
247    *
248    * @SINCE_1_0.0
249    * @param[in] value The property value to assign from
250    * @return a reference to this
251    */
252   Value& operator=(const Value& value);
253
254   /**
255    * @brief Move assignment operator.
256    *
257    * @SINCE_1_4.16
258    * @param[in] value The property value to move from
259    * @return a reference to this
260    */
261   Value& operator=(Value&& value) noexcept;
262
263   /**
264    * @brief Non-virtual destructor.
265    *
266    * This class is not a base class.
267    * @SINCE_1_0.0
268    */
269   ~Value();
270
271   /**
272    * @brief Queries the type of this property value.
273    *
274    * @SINCE_1_0.0
275    * @return The type ID
276    */
277   Type GetType() const;
278
279   /**
280    * @brief Retrieves a specific value.
281    *
282    * Works on a best-effort approach; if value type is different returns a default value of the type.
283    *
284    * @SINCE_1_4.36
285    * @return A value of type T
286    */
287   template<typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr>
288   T DALI_INTERNAL Get() const
289   {
290     T temp = T(); // value (zero) initialize
291     Get(temp);
292     return temp;
293   }
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    * Specialization for enumeration values
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     int32_t temp = 0; // value (zero) initialize
308     Get(temp);
309     return static_cast<T>(temp);
310   }
311
312   /**
313    * @brief Retrieves an enumeration value.
314    *
315    * @SINCE_1_4.36
316    * @param[out] enumValue On return, an enumeration value
317    * @return @c true if the value is successfully retrieved, @c false if the type is different
318    * @pre GetType() is any enumeration
319    */
320   template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
321   bool DALI_INTERNAL Get(T& enumValue) const
322   {
323     int32_t temp = 0;
324     if(!Get(temp))
325     {
326       return false;
327     }
328     enumValue = static_cast<T>(temp);
329     return true;
330   }
331
332   /**
333    * @brief Retrieves a boolean value.
334    *
335    * @SINCE_1_0.0
336    * @param[out] boolValue On return, a boolean value
337    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
338    * @pre GetType() is a type convertible to bool.
339    */
340   bool Get(bool& boolValue) const;
341
342   /**
343    * @brief Retrieves a floating-point value.
344    *
345    * @SINCE_1_0.0
346    * @param[out] floatValue On return, a floating-point value
347    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
348    * @pre GetType() is a type convertible to float.
349    */
350   bool Get(float& floatValue) const;
351
352   /**
353    * @brief Retrieves an integer value.
354    *
355    * @SINCE_1_0.0
356    * @param[out] integerValue On return, an integer value
357    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
358    * @pre GetType() is a type convertible to int.
359    */
360   bool Get(int32_t& integerValue) const;
361
362   /**
363    * @brief Retrieves an integer rectangle.
364    *
365    * @SINCE_1_0.0
366    * @param[out] rect On return, an integer rectangle
367    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
368    * @pre GetType() is a type convertible to Rect<int>.
369    */
370   bool Get(Rect<int32_t>& rect) const;
371
372   /**
373    * @brief Retrieves a vector value.
374    *
375    * @SINCE_1_0.0
376    * @param[out] vectorValue On return, a vector value
377    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
378    * @pre GetType() is a type convertible to Vector2.
379    */
380   bool Get(Vector2& vectorValue) const;
381
382   /**
383    * @brief Retrieves a vector value.
384    *
385    * @SINCE_1_0.0
386    * @param[out] vectorValue On return, a vector value
387    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
388    * @pre GetType() is a type convertible to Vector3.
389    */
390   bool Get(Vector3& vectorValue) const;
391
392   /**
393    * @brief Retrieves a vector value.
394    *
395    * @SINCE_1_0.0
396    * @param[out] vectorValue On return, a vector value
397    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
398    * @pre GetType() is a type convertible to Vector4.
399    */
400   bool Get(Vector4& vectorValue) const;
401
402   /**
403    * @brief Retrieves a matrix3 value.
404    *
405    * @SINCE_1_0.0
406    * @param[out] matrixValue On return, a matrix3 value
407    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
408    * @pre GetType() is a type convertible to Matrix3.
409    */
410   bool Get(Matrix3& matrixValue) const;
411
412   /**
413    * @brief Retrieves a matrix value.
414    *
415    * @SINCE_1_0.0
416    * @param[out] matrixValue On return, a matrix value
417    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
418    * @pre GetType() is a type convertible to Matrix.
419    */
420   bool Get(Matrix& matrixValue) const;
421
422   /**
423    * @brief Retrieves an angle-axis value.
424    *
425    * @SINCE_1_0.0
426    * @param[out] angleAxisValue On return, a angle-axis value
427    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
428    * @pre GetType() is a type convertible to AngleAxis.
429    */
430   bool Get(AngleAxis& angleAxisValue) const;
431
432   /**
433    * @brief Retrieves a quaternion value.
434    *
435    * @SINCE_1_0.0
436    * @param[out] quaternionValue On return, a quaternion value
437    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
438    * @pre GetType() is a type convertible to Quaternion.
439    */
440   bool Get(Quaternion& quaternionValue) const;
441
442   /**
443    * @brief Retrieves an string property value.
444    *
445    * @SINCE_1_0.0
446    * @param[out] stringValue A string
447    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
448    * @pre GetType() is a type convertible to string.
449    */
450   bool Get(std::string& stringValue) const;
451
452   /**
453    * @brief Retrieves an array property value.
454    *
455    * @SINCE_1_0.0
456    * @param[out] arrayValue The array as a vector Property Values
457    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
458    * @pre GetType() returns Property::ARRAY.
459    */
460   bool Get(Property::Array& arrayValue) const;
461
462   /**
463    * @brief Retrieves an map property value.
464    *
465    * @SINCE_1_0.0
466    * @param[out] mapValue The map as vector of string and Property Value pairs
467    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
468    * @pre GetType() returns Property::MAP.
469    */
470   bool Get(Property::Map& mapValue) const;
471
472   /**
473    * @brief Retrieves the Array API of the Property::Value without copying the contents of the map.
474    *
475    * @SINCE_1_0.0
476    * @return The Array API of the Property::Value or NULL if not a Property::Array
477    */
478   Property::Array* GetArray() const;
479
480   /**
481    * @brief Retrieves the Map API of the Property::Value without copying the contents of the map.
482    *
483    * @SINCE_1_0.0
484    * @return The Map API of the Property::Value or NULL if not a Property::Map
485    */
486   Property::Map* GetMap() const;
487
488   /**
489    * @brief Retrieves an extents.
490    *
491    * @SINCE_1_2.62
492    * @param[out] extentsValue Extents, a collection of 4 uint16_t
493    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
494    * @pre GetType() is a type convertible to Extents.
495    */
496   bool Get(Extents& extentsValue) const;
497
498   /**
499    * @brief Output to stream.
500    * @SINCE_1_0.0
501    */
502   friend DALI_CORE_API std::ostream& operator<<(std::ostream& ouputStream, const Property::Value& value);
503
504 private:
505   struct DALI_INTERNAL Impl;
506   Impl*                mImpl; ///< Pointer to the implementation
507 };
508
509 /**
510  * @brief Converts the value of the property into a string and append to an output stream.
511  *
512  * @SINCE_1_0.0
513  * @param[in] ouputStream The output stream operator
514  * @param[in] value The value to insert
515  * @return The output stream operator
516  */
517 DALI_CORE_API std::ostream& operator<<(std::ostream& ouputStream, const Property::Value& value);
518
519 /**
520  * @}
521  */
522 } // namespace Dali
523
524 #endif // DALI_PROPERTY_VALUE_H