Make Dali::Vector movable & add use Modern C++ semantics on public & devel classes
[platform/core/uifw/dali-core.git] / dali / public-api / object / handle.h
1 #ifndef DALI_HANDLE_H
2 #define DALI_HANDLE_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 <string>
23 #include <cstdint> // uint32_t
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/object/base-handle.h>
28 #include <dali/public-api/object/property-types.h>
29 #include <dali/public-api/object/property-value.h>
30 #include <dali/public-api/object/property-notification-declarations.h>
31 #include <dali/public-api/object/ref-object.h>
32
33 namespace Dali
34 {
35 /**
36  * @addtogroup dali_core_object
37  * @{
38  */
39
40 class Constraint;
41 class PropertyNotification;
42 class PropertyCondition;
43
44 namespace Internal DALI_INTERNAL
45 {
46 class Object;
47 }
48
49 /**
50  * @brief Dali::Handle is a handle to an internal property owning Dali object that can have constraints applied to it.
51  * @SINCE_1_0.0
52  */
53 class DALI_CORE_API Handle : public BaseHandle
54 {
55 public:
56
57   /**
58    * @brief Enumeration for Handle's capabilities that can be queried using Handle::Supports().
59    * @SINCE_1_0.0
60    */
61   enum Capability
62   {
63     /**
64      * @brief Some objects support dynamic property creation at run-time.
65      *
66      * New properties are registered by calling RegisterProperty() with an unused property name.
67      * @SINCE_1_0.0
68      */
69     DYNAMIC_PROPERTIES = 0x01,
70   };
71
72 public:
73
74   /**
75    * @brief This constructor is used by Dali New() methods.
76    *
77    * @SINCE_1_0.0
78    * @param[in] handle A pointer to a newly allocated Dali resource
79    */
80   Handle( Dali::Internal::Object* handle );
81
82   /**
83    * @brief This constructor provides an uninitialized Dali::Handle.
84    *
85    * This should be initialized with a Dali New() method before use.
86    * Methods called on an uninitialized Dali::Handle will assert.
87    * @code
88    * Handle handle; // uninitialized
89    * handle.SomeMethod(); // unsafe! This will assert
90    *
91    * handle = SomeClass::New(); // now initialized
92    * handle.SomeMethod(); // safe
93    * @endcode
94    * @SINCE_1_0.0
95    */
96   Handle();
97
98   /**
99    * @brief Creates a new object.
100    *
101    * @SINCE_1_0.0
102    * @return A handle to a newly allocated object
103    */
104   static Handle New();
105
106   /**
107    * @brief Dali::Handle is intended as a base class.
108    *
109    * This is non-virtual since derived Handle types must not contain data or virtual methods.
110    * @SINCE_1_0.0
111    */
112   ~Handle();
113
114   /**
115    * @brief This copy constructor is required for (smart) pointer semantics.
116    *
117    * @SINCE_1_0.0
118    * @param[in] handle A reference to the copied handle
119    */
120   Handle( const Handle& handle );
121
122   /**
123    * @brief This assignment operator is required for (smart) pointer semantics.
124    *
125    * @SINCE_1_0.0
126    * @param[in] rhs A reference to the copied handle
127    * @return A reference to this
128    */
129   Handle& operator=( const Handle& rhs );
130
131   /**
132    * @brief Move constructor.
133    *
134    * @SINCE_1_9.22
135    * @param[in] rhs A reference to the moved handle
136    */
137   Handle( Handle&& rhs );
138
139   /**
140    * @brief Move assignment operator.
141    *
142    * @SINCE_1_9.22
143    * @param[in] rhs A reference to the moved handle
144    * @return A reference to this handle
145    */
146   Handle& operator=( Handle&& rhs );
147
148   /**
149    * @brief Downcasts to a handle.
150    *
151    * If not, the returned handle is left uninitialized.
152    * @SINCE_1_0.0
153    * @param[in] handle to An object
154    * @return handle or an uninitialized handle
155    */
156   static Handle DownCast( BaseHandle handle );
157
158   /**
159    * @brief Queries whether an handle supports a given capability.
160    *
161    * @SINCE_1_0.0
162    * @param[in] capability The queried capability
163    * @return True if the capability is supported
164    */
165   bool Supports( Capability capability ) const;
166
167   // Properties
168
169   /**
170    * @brief Queries how many properties are provided by an handle.
171    *
172    * This may vary between instances of a class, if dynamic properties are supported.
173    * @SINCE_1_0.0
174    * @return The number of properties
175    */
176   uint32_t GetPropertyCount() const;
177
178   /**
179    * @brief Queries the name of a property.
180    *
181    * @SINCE_1_0.0
182    * @param[in] index The index of the property
183    * @return The name of the property
184    */
185   std::string GetPropertyName( Property::Index index ) const;
186
187   /**
188    * @brief Queries the index of a property.
189    *
190    * Returns the first property index that matches the given name exactly.
191    *
192    * @SINCE_1_0.0
193    * @param[in] name The name of the property
194    * @return The index of the property, or Property::INVALID_INDEX if no property exists with the given name
195    */
196   Property::Index GetPropertyIndex( const std::string& name ) const;
197
198   /**
199    * @brief Queries whether a property can be set using SetProperty().
200    *
201    * @SINCE_1_0.0
202    * @param[in] index The index of the property
203    * @return True if the property is writable
204    * @pre Property::INVALID_INDEX < index.
205    */
206   bool IsPropertyWritable( Property::Index index ) const;
207
208   /**
209    * @brief Queries whether a writable property can be the target of an animation or constraint.
210    *
211    * @SINCE_1_0.0
212    * @param[in] index The index of the property
213    * @return True if the property is animatable
214    */
215   bool IsPropertyAnimatable( Property::Index index ) const;
216
217   /**
218    * @brief Queries whether a property can be used as in input to a constraint.
219    *
220    * @SINCE_1_0.0
221    * @param[in] index The index of the property
222    * @return True if the property can be used as a constraint input
223    */
224   bool IsPropertyAConstraintInput( Property::Index index ) const;
225
226   /**
227    * @brief Queries the type of a property.
228    *
229    * @SINCE_1_0.0
230    * @param[in] index The index of the property
231    * @return The type of the property
232    */
233   Property::Type GetPropertyType( Property::Index index ) const;
234
235   /**
236    * @brief Sets the value of an existing property.
237    *
238    * Property should be write-able. Setting a read-only property is a no-op.
239    * @SINCE_1_0.0
240    * @param[in] index The index of the property
241    * @param[in] propertyValue The new value of the property
242    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
243    */
244   void SetProperty( Property::Index index, const Property::Value& propertyValue );
245
246   /**
247    * @brief Registers a new animatable property.
248    *
249    * @SINCE_1_0.0
250    * @param[in] name The name of the property
251    * @param[in] propertyValue The new value of the property
252    * @return The index of the property or Property::INVALID_INDEX if registration failed
253    * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
254    * Property names are expected to be unique, but this is not enforced.
255    * Property indices are unique to each registered custom property in a given object.
256    * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
257    * animatable property on an object that does not have scene graph object.
258    * @note Only the following types can be animated:
259    *       - Property::BOOLEAN
260    *       - Property::FLOAT
261    *       - Property::INTEGER
262    *       - Property::VECTOR2
263    *       - Property::VECTOR3
264    *       - Property::VECTOR4
265    *       - Property::MATRIX3
266    *       - Property::MATRIX
267    *       - Property::ROTATION
268    * @note If a property with the desired name already exists, then the value given is just set.
269    */
270   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
271
272   /**
273    * @brief Registers a new property.
274    *
275    * Properties can be set as non animatable using property attributes.
276    * @SINCE_1_0.0
277    * @param[in] name The name of the property
278    * @param[in] propertyValue The new value of the property
279    * @param[in] accessMode The property access mode (writable, animatable etc)
280    * @return The index of the property
281    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
282    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
283    * @note Only the following types can be animated:
284    *       - Property::BOOLEAN
285    *       - Property::FLOAT
286    *       - Property::INTEGER
287    *       - Property::VECTOR2
288    *       - Property::VECTOR3
289    *       - Property::VECTOR4
290    *       - Property::MATRIX3
291    *       - Property::MATRIX
292    *       - Property::ROTATION
293    * @note If a property with the desired name already exists, then the value given is just set.
294    */
295   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
296
297   /**
298    * @brief Retrieves a property value.
299    *
300    * @SINCE_1_0.0
301    * @param[in] index The index of the property
302    * @return The property value
303    */
304   Property::Value GetProperty( Property::Index index ) const;
305
306   /**
307    * @brief Convenience function for obtaining a property of a known type.
308    *
309    * @SINCE_1_0.0
310    * @param[in] index The index of the property
311    * @return The property value
312    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
313    */
314   template <typename T>
315   T GetProperty( Property::Index index ) const
316   {
317     Property::Value value = GetProperty(index);
318
319     return T( value.Get<T>() );
320   }
321
322   /**
323    * @brief Retrieves the latest value of the property from the scene-graph.
324    *
325    * @SINCE_1_2.41
326    * @param[in] index The index of the property
327    * @return The property value
328    */
329   Property::Value GetCurrentProperty( Property::Index index ) const;
330
331   /**
332    * @brief Convenience function for obtaining the current value of a property of a known type.
333    *
334    * @SINCE_1_2.41
335    * @param[in] index The index of the property
336    * @return The property value
337    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
338    */
339   template <typename T>
340   T GetCurrentProperty( Property::Index index ) const
341   {
342     Property::Value value = GetCurrentProperty( index );
343
344     return T( value.Get<T>() );
345   }
346
347   /**
348    * @brief Retrieves all the property indices for this object (including custom properties).
349    *
350    * @SINCE_1_0.0
351    * @param[out] indices A container of property indices for this object
352    * @note The added container is cleared.
353    */
354   void GetPropertyIndices( Property::IndexContainer& indices ) const;
355
356   /**
357    * @brief Adds a property notification to this object.
358    *
359    * @SINCE_1_0.0
360    * @param[in] index The index of the property
361    * @param[in] condition The notification will be triggered when this condition is satisfied
362    * @return A handle to the newly created PropertyNotification
363    */
364   PropertyNotification AddPropertyNotification( Property::Index index,
365                                                 const PropertyCondition& condition );
366
367   /**
368    * @brief Adds a property notification to this object.
369    *
370    * @SINCE_1_0.0
371    * @param[in] index The index of the property
372    * @param[in] componentIndex Index to the component of a complex property such as a Vector
373    * @param[in] condition The notification will be triggered when this condition is satisfied
374    * @return A handle to the newly created PropertyNotification
375    */
376   PropertyNotification AddPropertyNotification( Property::Index index,
377                                                 int componentIndex,
378                                                 const PropertyCondition& condition );
379
380   /**
381    * @brief Removes a property notification from this object.
382    *
383    * @SINCE_1_0.0
384    * @param[in] propertyNotification The propertyNotification to be removed
385    */
386   void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
387
388   /**
389    * @brief Removes all property notifications from this object.
390    * @SINCE_1_0.0
391    */
392   void RemovePropertyNotifications();
393
394   // Constraints
395
396   /**
397    * @brief Removes all constraints from an Object.
398    *
399    * @SINCE_1_0.0
400    * @pre The object has been initialized.
401    */
402   void RemoveConstraints();
403
404   /**
405    * @brief Removes all the constraint from the Object with a matching tag.
406    *
407    * @SINCE_1_0.0
408    * @param[in] tag The tag of the constraints which will be removed
409    * @pre The Object has been initialized.
410    */
411   void RemoveConstraints( uint32_t tag );
412 };
413
414 /**
415  * @brief This namespace provides a convenient function to create an object with a custom "weight" property.
416  * @SINCE_1_0.0
417  */
418 namespace WeightObject
419 {
420
421 DALI_CORE_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
422
423 /**
424  * @brief Convenience function to create an object with a custom "weight" property.
425  *
426  * @SINCE_1_0.0
427  * @return A handle to a newly allocated object
428  */
429 DALI_CORE_API Handle New();
430
431 } // namespace WeightObject
432
433 /**
434  * @}
435  */
436 } // namespace Dali
437
438 #endif // DALI_HANDLE_H