Fix interface to take sink argument by value.
[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 <cstdint> // uint32_t
23 #include <string>
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/indirect-value.h>
29 #include <dali/public-api/object/property-key.h>
30 #include <dali/public-api/object/property-notification-declarations.h>
31 #include <dali/public-api/object/property-types.h>
32 #include <dali/public-api/object/property-value.h>
33 #include <dali/public-api/object/ref-object.h>
34 #include <dali/public-api/signals/dali-signal.h>
35
36 namespace Dali
37 {
38 /**
39  * @addtogroup dali_core_object
40  * @{
41  */
42
43 class Constraint;
44 class PropertyNotification;
45 class PropertyCondition;
46
47 namespace Internal DALI_INTERNAL
48 {
49 class Object;
50 }
51
52 /**
53  * @brief Dali::Handle is a handle to an internal property owning Dali object that can have constraints applied to it.
54  * @SINCE_1_0.0
55  */
56 class DALI_CORE_API Handle : public BaseHandle
57 {
58 public:
59   /**
60    * @brief Enumeration for Handle's capabilities that can be queried using Handle::Supports().
61    * @SINCE_1_0.0
62    */
63   enum Capability
64   {
65     /**
66      * @brief Some objects support dynamic property creation at run-time.
67      *
68      * New properties are registered by calling RegisterProperty() with an unused property name.
69      * @SINCE_1_0.0
70      */
71     DYNAMIC_PROPERTIES = 0x01,
72   };
73
74   /**
75    * @brief PropertySetSignal function prototype for signal handler. Called when a property is set on this object.
76    */
77   using PropertySetSignalType = Signal<void(Handle& handle, Property::Index index, Property::Value value)>;
78
79 public:
80   /**
81    * @brief This constructor is used by Dali New() methods.
82    *
83    * @SINCE_1_0.0
84    * @param[in] handle A pointer to a newly allocated Dali resource
85    */
86   Handle(Dali::Internal::Object* handle);
87
88   /**
89    * @brief This constructor provides an uninitialized Dali::Handle.
90    *
91    * This should be initialized with a Dali New() method before use.
92    * Methods called on an uninitialized Dali::Handle will assert.
93    * @code
94    * Handle handle; // uninitialized
95    * handle.SomeMethod(); // unsafe! This will assert
96    *
97    * handle = SomeClass::New(); // now initialized
98    * handle.SomeMethod(); // safe
99    * @endcode
100    * @SINCE_1_0.0
101    */
102   Handle();
103
104   /**
105    * @brief Creates a new object.
106    *
107    * @SINCE_1_0.0
108    * @return A handle to a newly allocated object
109    */
110   static Handle New();
111
112   /**
113    * @brief Template to create a derived handle and set properties on it.
114    *
115    * Marked as DALI_NO_EXPORT_API to prevent internal usage exporting symbols.
116    * @SINCE_1_9.27
117    * @tparam T The derived class to create
118    * @param[in] properties The properties to set
119    */
120   template<typename Type>
121   static DALI_NO_EXPORT_API Type New(const Property::Map& properties)
122   {
123     Type handle = Type::New();
124     handle.SetProperties(properties);
125     return handle;
126   }
127
128   /**
129    * @brief Dali::Handle is intended as a base class.
130    *
131    * This is non-virtual since derived Handle types must not contain data or virtual methods.
132    * @SINCE_1_0.0
133    */
134   ~Handle();
135
136   /**
137    * @brief This copy constructor is required for (smart) pointer semantics.
138    *
139    * @SINCE_1_0.0
140    * @param[in] handle A reference to the copied handle
141    */
142   Handle(const Handle& handle);
143
144   /**
145    * @brief This assignment operator is required for (smart) pointer semantics.
146    *
147    * @SINCE_1_0.0
148    * @param[in] rhs A reference to the copied handle
149    * @return A reference to this
150    */
151   Handle& operator=(const Handle& rhs);
152
153   /**
154    * @brief Move constructor.
155    *
156    * @SINCE_1_9.22
157    * @param[in] rhs A reference to the moved handle
158    */
159   Handle(Handle&& rhs);
160
161   /**
162    * @brief Move assignment operator.
163    *
164    * @SINCE_1_9.22
165    * @param[in] rhs A reference to the moved handle
166    * @return A reference to this handle
167    */
168   Handle& operator=(Handle&& rhs);
169
170   /**
171    * @brief Downcasts to a handle.
172    *
173    * If not, the returned handle is left uninitialized.
174    * @SINCE_1_0.0
175    * @param[in] handle to An object
176    * @return handle or an uninitialized handle
177    */
178   static Handle DownCast(BaseHandle handle);
179
180   /**
181    * @brief Queries whether an handle supports a given capability.
182    *
183    * @SINCE_1_0.0
184    * @param[in] capability The queried capability
185    * @return True if the capability is supported
186    */
187   bool Supports(Capability capability) const;
188
189   // Properties
190
191   /**
192    * @brief Queries how many properties are provided by an handle.
193    *
194    * This may vary between instances of a class, if dynamic properties are supported.
195    * @SINCE_1_0.0
196    * @return The number of properties
197    */
198   uint32_t GetPropertyCount() const;
199
200   /**
201    * @brief Queries the name of a property.
202    *
203    * @SINCE_1_0.0
204    * @param[in] index The index of the property
205    * @return The name of the property
206    */
207   std::string GetPropertyName(Property::Index index) const;
208
209   /**
210    * @brief Query the index of a property using the given key.
211    *
212    * @SINCE_1_9.27
213    * @param[in] key The key of the property to search for. (The name or integer key provided to
214    * RegisterProperty()).
215    * @return the matching property index of the key, or Property::INVALID_INDEX if no
216    * property matches the given key.
217    */
218   Property::Index GetPropertyIndex(Property::Key key) const;
219
220   /**
221    * @brief Queries whether a property can be set using SetProperty().
222    *
223    * @SINCE_1_0.0
224    * @param[in] index The index of the property
225    * @return True if the property is writable
226    * @pre Property::INVALID_INDEX < index.
227    */
228   bool IsPropertyWritable(Property::Index index) const;
229
230   /**
231    * @brief Queries whether a writable property can be the target of an animation or constraint.
232    *
233    * @SINCE_1_0.0
234    * @param[in] index The index of the property
235    * @return True if the property is animatable
236    */
237   bool IsPropertyAnimatable(Property::Index index) const;
238
239   /**
240    * @brief Queries whether a property can be used as in input to a constraint.
241    *
242    * @SINCE_1_0.0
243    * @param[in] index The index of the property
244    * @return True if the property can be used as a constraint input
245    */
246   bool IsPropertyAConstraintInput(Property::Index index) const;
247
248   /**
249    * @brief Queries the type of a property.
250    *
251    * @SINCE_1_0.0
252    * @param[in] index The index of the property
253    * @return The type of the property
254    */
255   Property::Type GetPropertyType(Property::Index index) const;
256
257   /**
258    * @brief Sets the value of an existing property.
259    *
260    * Property should be write-able. Setting a read-only property is a no-op.
261    * @SINCE_1_0.0
262    * @param[in] index The index of the property
263    * @param[in] propertyValue The new value of the property
264    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
265    */
266   void SetProperty(Property::Index index, Property::Value propertyValue);
267
268   /**
269    * @brief Registers a new animatable property.
270    *
271    * @SINCE_1_0.0
272    * @param[in] name The name of the property
273    * @param[in] propertyValue The new value of the property
274    * @return The index of the property or Property::INVALID_INDEX if registration failed
275    * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
276    * Property names are expected to be unique, but this is not enforced.
277    * Property indices are unique to each registered custom property in a given object.
278    * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
279    * animatable property on an object that does not have scene graph object.
280    * @note Only the following types can be animated:
281    *       - Property::BOOLEAN
282    *       - Property::FLOAT
283    *       - Property::INTEGER
284    *       - Property::VECTOR2
285    *       - Property::VECTOR3
286    *       - Property::VECTOR4
287    *       - Property::MATRIX3
288    *       - Property::MATRIX
289    *       - Property::ROTATION
290    * @note If a property with the desired name already exists, then the value given is just set.
291    */
292   Property::Index RegisterProperty(std::string name, Property::Value propertyValue);
293
294   /**
295    * @brief Register a new animatable property with an integer key.
296    *
297    * @SINCE_1_9.27
298    * @param[in] key  The integer key of the property.
299    * @param[in] name The text key of the property.
300    * @param[in] propertyValue The new value of the property.
301    *
302    * @return The index of the property or Property::INVALID_INDEX if registration failed
303    *
304    * @pre The object supports dynamic properties
305    * i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.  Property names and keys
306    * are expected to be unique, but this is not enforced.  Property indices are unique
307    * to each registered custom property in a given object.
308    *
309    * @note Returns Property::INVALID_INDEX if registration failed. This can happen if
310    * you try to register animatable property on an object that does not have scene graph
311    * object.
312    *
313    * @note The returned property index is not the same as the integer key (though it
314    * shares a type)
315    *
316    * This version of RegisterProperty associates both an integer key and the text key
317    * with the property, allowing for lookup of the property index by either key or name
318    * ( which is useful when other classes know the key but not the name )
319    *
320    * @note Only the following types can be animated:
321    *       - Property::BOOLEAN
322    *       - Property::FLOAT
323    *       - Property::INTEGER
324    *       - Property::VECTOR2
325    *       - Property::VECTOR3
326    *       - Property::VECTOR4
327    *       - Property::MATRIX3
328    *       - Property::MATRIX
329    *       - Property::ROTATION
330    * @note If a property with the desired name already exists, then the value given is just set.
331    */
332   Property::Index RegisterProperty(Property::Index key,
333                                    std::string     name,
334                                    Property::Value propertyValue);
335
336   /**
337    * @brief Registers a new property.
338    *
339    * Properties can be set as non animatable using property attributes.
340    * @SINCE_1_0.0
341    * @param[in] name The name of the property
342    * @param[in] propertyValue The new value of the property
343    * @param[in] accessMode The property access mode (writable, animatable etc)
344    * @return The index of the property
345    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
346    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
347    * @note Only the following types can be animated:
348    *       - Property::BOOLEAN
349    *       - Property::FLOAT
350    *       - Property::INTEGER
351    *       - Property::VECTOR2
352    *       - Property::VECTOR3
353    *       - Property::VECTOR4
354    *       - Property::MATRIX3
355    *       - Property::MATRIX
356    *       - Property::ROTATION
357    * @note If a property with the desired name already exists, then the value given is just set.
358    */
359   Property::Index RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode);
360
361   /**
362    * @brief Retrieves a property value.
363    *
364    * @SINCE_1_0.0
365    * @param[in] index The index of the property
366    * @return The property value
367    */
368   Property::Value GetProperty(Property::Index index) const;
369
370   /**
371    * @brief Convenience function for obtaining a property of a known type.
372    *
373    * @SINCE_1_0.0
374    * @param[in] index The index of the property
375    * @return The property value
376    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
377    */
378   template<typename T>
379   T GetProperty(Property::Index index) const
380   {
381     Property::Value value = GetProperty(index);
382
383     return T(value.Get<T>());
384   }
385
386   /**
387    * @brief Retrieves the latest value of the property from the scene-graph.
388    *
389    * @SINCE_1_2.41
390    * @param[in] index The index of the property
391    * @return The property value
392    */
393   Property::Value GetCurrentProperty(Property::Index index) const;
394
395   /**
396    * @brief Convenience function for obtaining the current value of a property of a known type.
397    *
398    * @SINCE_1_2.41
399    * @param[in] index The index of the property
400    * @return The property value
401    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
402    */
403   template<typename T>
404   T GetCurrentProperty(Property::Index index) const
405   {
406     Property::Value value = GetCurrentProperty(index);
407
408     return T(value.Get<T>());
409   }
410
411   /**
412    * @brief Sets all the properties in the given property map.
413    *
414    * @SINCE_1_9.27
415    * @param[in] properties The properties to set
416    */
417   void SetProperties(const Property::Map& properties);
418
419   /**
420    * @brief Retrieves all the properties and the values for this object
421    *
422    * @SINCE_1_9.27
423    * @param[out] properties A map which is populated with the index-value pairs
424    *
425    * @note The properties map will be cleared by this method first.
426    */
427   void GetProperties(Property::Map& properties);
428
429   /**
430    * @brief Retrieves all the property indices for this object (including custom properties).
431    *
432    * @SINCE_1_0.0
433    * @param[out] indices A container of property indices for this object
434    * @note The added container is cleared.
435    */
436   void GetPropertyIndices(Property::IndexContainer& indices) const;
437
438   /**
439    * @brief Determine if the custom property index exists on this object without throwing a Dali::Exception.
440    *
441    * @SINCE_1_9.27
442    * @note This does not check default properties.
443    * @param[in] index The index of the property to test for
444    */
445   bool DoesCustomPropertyExist(Property::Index index);
446
447   /**
448    * @brief Adds a property notification to this object.
449    *
450    * @SINCE_1_0.0
451    * @param[in] index The index of the property
452    * @param[in] condition The notification will be triggered when this condition is satisfied
453    * @return A handle to the newly created PropertyNotification
454    */
455   PropertyNotification AddPropertyNotification(Property::Index          index,
456                                                const PropertyCondition& condition);
457
458   /**
459    * @brief Adds a property notification to this object.
460    *
461    * @SINCE_1_0.0
462    * @param[in] index The index of the property
463    * @param[in] componentIndex Index to the component of a complex property such as a Vector
464    * @param[in] condition The notification will be triggered when this condition is satisfied
465    * @return A handle to the newly created PropertyNotification
466    */
467   PropertyNotification AddPropertyNotification(Property::Index          index,
468                                                int                      componentIndex,
469                                                const PropertyCondition& condition);
470
471   /**
472    * @brief Removes a property notification from this object.
473    *
474    * @SINCE_1_0.0
475    * @param[in] propertyNotification The propertyNotification to be removed
476    */
477   void RemovePropertyNotification(Dali::PropertyNotification propertyNotification);
478
479   /**
480    * @brief Removes all property notifications from this object.
481    * @SINCE_1_0.0
482    */
483   void RemovePropertyNotifications();
484
485   // Constraints
486
487   /**
488    * @brief Removes all constraints from an Object.
489    *
490    * @SINCE_1_0.0
491    * @pre The object has been initialized.
492    */
493   void RemoveConstraints();
494
495   /**
496    * @brief Removes all the constraint from the Object with a matching tag.
497    *
498    * @SINCE_1_0.0
499    * @param[in] tag The tag of the constraints which will be removed
500    * @pre The Object has been initialized.
501    */
502   void RemoveConstraints(uint32_t tag);
503
504   /**
505    * @brief Index operator, using integer lookup.
506    *
507    * Returns an object that can be assigned to or cast from, enabling
508    * the indexed property to be either read or written.
509    *
510    * @param[in] index The index of the property to access.
511    * @return indirect value. Should have shorter scope than the handle
512    */
513   IndirectValue operator[](Property::Index index);
514
515   /**
516    * @brief Index operator, using name lookup.
517    *
518    * Returns an object that can be assigned to or cast from, enabling
519    * the named property to be either read or written.
520    *
521    * @param[in] name The name of the property to access.
522    * @return indirect value. Should have shorter scope than the handle
523    */
524   IndirectValue operator[](const std::string& name);
525
526 public: // Signals
527   /**
528    * @brief Get a signal when a property is set on this object through the API (i.e. not when animating)
529    *
530    * @SINCE_1_9.27
531    * @return The signal to attach a connection to.
532    */
533   PropertySetSignalType& PropertySetSignal();
534 };
535
536 /**
537  * @brief This namespace provides a convenient function to create an object with a custom "weight" property.
538  * @SINCE_1_0.0
539  */
540 namespace WeightObject
541 {
542 DALI_CORE_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
543
544 /**
545  * @brief Convenience function to create an object with a custom "weight" property.
546  *
547  * @SINCE_1_0.0
548  * @return A handle to a newly allocated object
549  */
550 DALI_CORE_API Handle New();
551
552 } // namespace WeightObject
553
554 /**
555  * @}
556  */
557 } // namespace Dali
558
559 #endif // DALI_HANDLE_H