Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-input-impl.h
1 #ifndef DALI_INTERNAL_PROPERTY_INPUT_IMPL_H
2 #define DALI_INTERNAL_PROPERTY_INPUT_IMPL_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 <iostream>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/property-input.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector3.h>
28 #include <dali/public-api/math/vector4.h>
29 #include <dali/public-api/math/quaternion.h>
30 #include <dali/public-api/math/matrix3.h>
31 #include <dali/public-api/math/matrix.h>
32 #include <dali/internal/common/buffer-index.h>
33
34 #if defined (ANDROID) || defined(WIN32) || defined(__APPLE__)
35 namespace std
36 {
37
38 uint64_t _Hash_bytes(const void* bytes, uint64_t size, uint64_t seed);
39
40 }
41 #endif
42
43 namespace Dali
44 {
45
46 namespace Internal
47 {
48
49 /**
50  * An abstract interface for receiving property values, and for querying whether
51  * a property value has changed i.e. whether a constraint needs to be reapplied.
52  */
53 class PropertyInputImpl
54 {
55 public:
56
57   /**
58    * Virtual destructor.
59    */
60   virtual ~PropertyInputImpl() = default;
61
62   /**
63    * Query the type of property input.
64    * @return The property type.
65    */
66   virtual Property::Type GetType() const = 0;
67
68   /**
69    * Query the input value (for a constraint) has been initialized.
70    * @return True if initialized, otherwise the constraint should be skipped.
71    */
72   virtual bool InputInitialized() const = 0;
73
74   /**
75    * Query the input value (for a constraint) has changed.
76    * @return True if the input value has changed.
77    */
78   virtual bool InputChanged() const = 0;
79
80   /**
81    * Retrieve a boolean value.
82    * @pre GetType() returns Property::BOOLEAN.
83    * @param[in] bufferIndex The buffer to read from.
84    * @return The boolean value.
85    */
86   virtual const bool& GetBoolean( BufferIndex bufferIndex ) const
87   {
88     // the return will never be executed, it's just to keep the compiler happy
89     return reinterpret_cast<const bool&>(*this);
90   }
91
92   /**
93    * Retrieve an integer value.
94    * @pre GetType() returns Property::INTEGER.
95    * @param[in] bufferIndex The buffer to read from.
96    * @return The integer value.
97    */
98   virtual const int& GetInteger( BufferIndex bufferIndex ) const
99   {
100     // the return will never be executed, it's just to keep the compiler happy
101     return reinterpret_cast<const int&>(*this);
102   }
103
104   /**
105    * Retrieve a float value.
106    * @pre GetType() returns Property::FLOAT.
107    * @param[in] bufferIndex The buffer to read from.
108    * @return The float value.
109    */
110   virtual const float& GetFloat( BufferIndex bufferIndex ) const
111   {
112     // the return will never be executed, it's just to keep the compiler happy
113     return reinterpret_cast<const float&>(*this);
114   }
115
116   /**
117    * Retrieve a Vector2 value.
118    * @pre GetType() returns Property::VECTOR2.
119    * @param[in] bufferIndex The buffer to read from.
120    * @return The Vector2 value.
121    */
122   virtual const Vector2& GetVector2( BufferIndex bufferIndex ) const
123   {
124     // the return will never be executed, it's just to keep the compiler happy
125     return reinterpret_cast<const Vector2&>(*this);
126   }
127
128   /**
129    * Retrieve a Vector3 value.
130    * @pre GetType() returns Property::VECTOR3.
131    * @param[in] bufferIndex The buffer to read from.
132    * @return The Vector3 value.
133    */
134   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
135   {
136     // the return will never be executed, it's just to keep the compiler happy
137     return reinterpret_cast<const Vector3&>(*this);
138   }
139
140   /**
141    * Retrieve a Vector4 value.
142    * @pre GetType() returns Property::VECTOR4.
143    * @param[in] bufferIndex The buffer to read from.
144    * @return The Vector4 value.
145    */
146   virtual const Vector4& GetVector4( BufferIndex bufferIndex ) const
147   {
148     // the return will never be executed, it's just to keep the compiler happy
149     return reinterpret_cast<const Vector4&>(*this);
150   }
151
152   /**
153    * Retrieve a Quaternion value.
154    * @pre GetType() returns Property::Quaternion.
155    * @param[in] bufferIndex The buffer to read from.
156    * @return The Quaternion value.
157    */
158   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
159   {
160     // the return will never be executed, it's just to keep the compiler happy
161     return reinterpret_cast<const Quaternion&>(*this);
162   }
163
164   /**
165    * Retrieve a Matrix value.
166    * @pre GetType() returns Property::Matrix.
167    * @param[in] bufferIndex The buffer to read from.
168    * @return The Matrix value.
169    */
170   virtual const Matrix3& GetMatrix3( BufferIndex bufferIndex ) const
171   {
172     // the return will never be executed, it's just to keep the compiler happy
173     return reinterpret_cast<const Matrix3&>(*this);
174   }
175
176   /**
177    * Retrieve a Matrix value.
178    * @pre GetType() returns Property::Matrix.
179    * @param[in] bufferIndex The buffer to read from.
180    * @return The Matrix value.
181    */
182   virtual const Matrix& GetMatrix( BufferIndex bufferIndex ) const
183   {
184     // the return will never be executed, it's just to keep the compiler happy
185     return reinterpret_cast<const Matrix&>(*this);
186   }
187
188   // Accessors for Constraint functions
189
190   /**
191    * Retrieve a boolean input for a constraint function.
192    * @note For inherited properties, this method should be overriden to return the value
193    * from the previous frame i.e. not from the current update buffer.
194    * @pre GetType() returns Property::BOOLEAN.
195    * @param[in] updateBufferIndex The current update buffer index.
196    * @return The boolean value.
197    */
198   virtual const bool& GetConstraintInputBoolean( BufferIndex updateBufferIndex ) const
199   {
200     return GetBoolean( updateBufferIndex );
201   }
202
203   /**
204    * Retrieve an integer input for a constraint function.
205    * @note For inherited properties, this method should be overriden to return the value
206    * from the previous frame i.e. not from the current update buffer.
207    * @pre GetType() returns Property::INTEGER.
208    * @param[in] updateBufferIndex The current update buffer index.
209    * @return The integer value.
210    */
211   virtual const int& GetConstraintInputInteger( BufferIndex updateBufferIndex ) const
212   {
213     return GetInteger( updateBufferIndex );
214   }
215
216   /**
217    * Retrieve a float input for a constraint function.
218    * @note For inherited properties, this method should be overriden to return the value
219    * from the previous frame i.e. not from the current update buffer.
220    * @pre GetType() returns Property::FLOAT.
221    * @param[in] updateBufferIndex The current update buffer index.
222    * @return The float value.
223    */
224   virtual const float& GetConstraintInputFloat( BufferIndex updateBufferIndex ) const
225   {
226     return GetFloat( updateBufferIndex );
227   }
228
229   /**
230    * Retrieve a Vector2 input for a constraint function.
231    * @note For inherited properties, this method should be overriden to return the value
232    * from the previous frame i.e. not from the current update buffer.
233    * @pre GetType() returns Property::VECTOR2.
234    * @param[in] updateBufferIndex The buffer to read from.
235    * @return The Vector2 value.
236    */
237   virtual const Vector2& GetConstraintInputVector2( BufferIndex updateBufferIndex ) const
238   {
239     return GetVector2( updateBufferIndex );
240   }
241
242   /**
243    * Retrieve a Vector3 input for a constraint function.
244    * @note For inherited properties, this method should be overriden to return the value
245    * from the previous frame i.e. not from the current update buffer.
246    * @pre GetType() returns Property::VECTOR3.
247    * @param[in] updateBufferIndex The buffer to read from.
248    * @return The Vector3 value.
249    */
250   virtual const Vector3& GetConstraintInputVector3( BufferIndex updateBufferIndex ) const
251   {
252     return GetVector3( updateBufferIndex );
253   }
254
255   /**
256    * Retrieve a Vector4 input for a constraint function.
257    * @note For inherited properties, this method should be overriden to return the value
258    * from the previous frame i.e. not from the current update buffer.
259    * @pre GetType() returns Property::VECTOR4.
260    * @param[in] updateBufferIndex The buffer to read from.
261    * @return The Vector4 value.
262    */
263   virtual const Vector4& GetConstraintInputVector4( BufferIndex updateBufferIndex ) const
264   {
265     return GetVector4( updateBufferIndex );
266   }
267
268   /**
269    * Retrieve a Quaternion input for a constraint function.
270    * @note For inherited properties, this method should be overriden to return the value
271    * from the previous frame i.e. not from the current update buffer.
272    * @pre GetType() returns Property::Quaternion.
273    * @param[in] updateBufferIndex The buffer to read from.
274    * @return The Quaternion value.
275    */
276   virtual const Quaternion& GetConstraintInputQuaternion( BufferIndex updateBufferIndex ) const
277   {
278     return GetQuaternion( updateBufferIndex );
279   }
280
281   /**
282    * Retrieve a Matrix3 input for a constraint function.
283    * @note For inherited properties, this method should be overriden to return the value
284    * from the previous frame i.e. not from the current update buffer.
285    * @pre GetType() returns Property::Matrix.
286    * @param[in] updateBufferIndex The buffer to read from.
287    * @return The Matrix value.
288    */
289   virtual const Matrix3& GetConstraintInputMatrix3( BufferIndex updateBufferIndex ) const
290   {
291     return GetMatrix3( updateBufferIndex );
292   }
293
294   /**
295    * Retrieve a Matrix input for a constraint function.
296    * @note For inherited properties, this method should be overriden to return the value
297    * from the previous frame i.e. not from the current update buffer.
298    * @pre GetType() returns Property::Matrix.
299    * @param[in] updateBufferIndex The buffer to read from.
300    * @return The Matrix value.
301    */
302   virtual const Matrix& GetConstraintInputMatrix( BufferIndex updateBufferIndex ) const
303   {
304     return GetMatrix( updateBufferIndex );
305   }
306
307   /**
308    * Query whether the property belongs to the
309    * transform manager or not.
310    * @return True if it is a transform manager property, false otherwise
311    */
312   virtual bool IsTransformManagerProperty() const
313   {
314     return false;
315   }
316
317   std::uint64_t Hash(BufferIndex bufferIndex, uint64_t seed) const
318   {
319     switch ( GetType() )
320     {
321       case Property::BOOLEAN:
322       {
323         return std::_Hash_bytes(&GetBoolean(bufferIndex), sizeof(bool), seed);
324       }
325
326       case Property::INTEGER:
327       {
328         return std::_Hash_bytes(&GetInteger(bufferIndex), sizeof(int), seed);
329       }
330
331       case Property::FLOAT:
332       {
333         return std::_Hash_bytes(&GetFloat(bufferIndex), sizeof(float), seed);
334       }
335
336       case Property::VECTOR2:
337       {
338         return std::_Hash_bytes(&GetVector2(bufferIndex), sizeof(Vector2), seed);
339       }
340
341       case Property::VECTOR3:
342       {
343         return std::_Hash_bytes(&GetVector3(bufferIndex), sizeof(Vector3), seed);
344       }
345
346       case Property::VECTOR4:
347       {
348         return std::_Hash_bytes(&GetVector4(bufferIndex), sizeof(Vector4), seed);
349       }
350
351       case Property::ROTATION:
352       {
353         return std::_Hash_bytes(&GetQuaternion(bufferIndex), sizeof(Quaternion), seed);
354       }
355
356       case Property::MATRIX:
357       {
358         return std::_Hash_bytes(&GetMatrix(bufferIndex), sizeof(Matrix), seed);
359       }
360
361       case Property::MATRIX3:
362       {
363         return std::_Hash_bytes(&GetMatrix3(bufferIndex), sizeof(Matrix3), seed);
364       }
365
366       default:
367         break; // print nothing
368     }
369
370     return seed;
371   }
372
373
374   /**
375    * Print the property value using a stream.
376    * @param[in] debugStream The output stream.
377    * @param[in] bufferIndex The buffer to read from.
378    * @todo Place this far-too-large-to-be-inlined function in a cpp and remove <iostream> header dependency from this file.
379    */
380   void DebugPrint( std::ostream& debugStream, BufferIndex bufferIndex ) const
381   {
382     switch ( GetType() )
383     {
384       case Property::BOOLEAN:
385       {
386         debugStream << GetBoolean( bufferIndex );
387         break;
388       }
389
390       case Property::INTEGER:
391       {
392         debugStream << GetInteger( bufferIndex );
393         break;
394       }
395
396       case Property::FLOAT:
397       {
398         debugStream << GetFloat( bufferIndex );
399         break;
400       }
401
402       case Property::VECTOR2:
403       {
404         debugStream << GetVector2( bufferIndex );
405         break;
406       }
407
408       case Property::VECTOR3:
409       {
410         debugStream << GetVector3( bufferIndex );
411         break;
412       }
413
414       case Property::VECTOR4:
415       {
416         debugStream << GetVector4( bufferIndex );
417         break;
418       }
419
420       case Property::ROTATION:
421       {
422         debugStream << GetQuaternion( bufferIndex );
423         break;
424       }
425
426       case Property::MATRIX:
427       {
428         debugStream << GetMatrix( bufferIndex );
429         break;
430       }
431
432       case Property::MATRIX3:
433       {
434         debugStream << GetMatrix3( bufferIndex );
435         break;
436       }
437
438       default:
439         break; // print nothing
440     }
441   }
442 };
443
444 } // namespace Internal
445
446 } // namespace Dali
447
448 #endif // DALI_INTERNAL_PROPERTY_INPUT_IMPL_H