Decrease property registration time
[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) 2022 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, const 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_view name, Property::Value propertyValue);
293
294   /**
295    * @brief Registers a new animatable property.
296    *
297    * @SINCE_2_1.6
298    * @param[in] name The name of the property
299    * @param[in] propertyValue The new value of the property
300    * @return The index of the property or Property::INVALID_INDEX if registration failed
301    * @pre The object supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
302    * Property names are expected to be unique, but this is DEFINITELY not enforced. It is up to the
303    * caller to enforce uniqueness.
304    *
305    * Property indices are unique to each registered custom property in a given object.
306    * returns Property::INVALID_INDEX if registration failed. This can happen if you try to register
307    * animatable property on an object that does not have scene graph object.
308    * @note Only the following types can be animated:
309    *       - Property::BOOLEAN
310    *       - Property::FLOAT
311    *       - Property::INTEGER
312    *       - Property::VECTOR2
313    *       - Property::VECTOR3
314    *       - Property::VECTOR4
315    *       - Property::MATRIX3
316    *       - Property::MATRIX
317    *       - Property::ROTATION
318    * @note If a property with the desired name already exists, then this creates a secondary
319    * entry to a different scene graph property; Access by index works as expected, but uniform
320    * values will use the last registered version, not the existing version, so things may break.
321    */
322   Property::Index RegisterUniqueProperty(std::string_view name, Property::Value propertyValue);
323
324   /**
325    * @brief Register a new animatable property with an integer key.
326    *
327    * @SINCE_1_9.27
328    * @param[in] key  The integer key of the property.
329    * @param[in] name The text key of the property.
330    * @param[in] propertyValue The new value of the property.
331    *
332    * @return The index of the property or Property::INVALID_INDEX if registration failed
333    *
334    * @pre The object supports dynamic properties
335    * i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.  Property names and keys
336    * are expected to be unique, but this is not enforced.  Property indices are unique
337    * to each registered custom property in a given object.
338    * @todo CHECK THIS!
339    *
340    * @note Returns Property::INVALID_INDEX if registration failed. This can happen if
341    * you try to register animatable property on an object that does not have scene graph
342    * object.
343    *
344    * @note The returned property index is not the same as the integer key (though it
345    * shares a type)
346    *
347    * This version of RegisterProperty associates both an integer key and the text key
348    * with the property, allowing for lookup of the property index by either key or name
349    * ( which is useful when other classes know the key but not the name )
350    *
351    * @note Only the following types can be animated:
352    *       - Property::BOOLEAN
353    *       - Property::FLOAT
354    *       - Property::INTEGER
355    *       - Property::VECTOR2
356    *       - Property::VECTOR3
357    *       - Property::VECTOR4
358    *       - Property::MATRIX3
359    *       - Property::MATRIX
360    *       - Property::ROTATION
361    * @note If a property with the desired name already exists, then the value given is just set.
362    */
363   Property::Index RegisterProperty(Property::Index  key,
364                                    std::string_view name,
365                                    Property::Value  propertyValue);
366
367   /**
368    * @brief Register a new animatable property with an integer key.
369    *
370    * @SINCE_2_1.6
371    * @param[in] key  The integer key of the property.
372    * @param[in] name The text key of the property.
373    * @param[in] propertyValue The new value of the property.
374    *
375    * @return The index of the property or Property::INVALID_INDEX if registration failed
376    * It is up to the caller to guarantee that the property is unique. This allows many
377    * checks to be skipped.
378    *
379    * @pre The object supports dynamic properties
380    * i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.  Property names and keys
381    * are expected to be unique, and are therefore just added without any checks.
382    * Property indices are unique to each registered custom property in a given object.
383    *
384    * @note Returns Property::INVALID_INDEX if registration failed. This can happen if
385    * you try to register animatable property on an object that does not have scene graph
386    * object.
387    *
388    * @note The returned property index is not the same as the integer key (though it
389    * shares a type)
390    *
391    * This version of RegisterProperty associates both an integer key and the text key
392    * with the property, allowing for lookup of the property index by either key or name
393    * ( which is useful when other classes know the key but not the name )
394    *
395    * @note Only the following types can be animated:
396    *       - Property::BOOLEAN
397    *       - Property::FLOAT
398    *       - Property::INTEGER
399    *       - Property::VECTOR2
400    *       - Property::VECTOR3
401    *       - Property::VECTOR4
402    *       - Property::MATRIX3
403    *       - Property::MATRIX
404    *       - Property::ROTATION
405    *
406    * @note If a property with the desired name already exists, then this will create a second entry with
407    * the same name, and may cause problems. It is up to the caller to prevent this happening. Possible side
408    * effects are: lookup by name always finds the first such property, not the second; whereas, writing
409    * uniform value to shader will use the second, not the first;
410    * Using the returned Property::Index for future reference will always access the correct property.
411    */
412   Property::Index RegisterUniqueProperty(Property::Index  key,
413                                          std::string_view name,
414                                          Property::Value  propertyValue);
415
416   /**
417    * @brief Registers a new property.
418    *
419    * Properties can be set as non animatable using property attributes.
420    * @SINCE_1_0.0
421    * @param[in] name The name of the property
422    * @param[in] propertyValue The new value of the property
423    * @param[in] accessMode The property access mode (writable, animatable etc)
424    * @return The index of the property
425    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
426    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
427    * @note Only the following types can be animated:
428    *       - Property::BOOLEAN
429    *       - Property::FLOAT
430    *       - Property::INTEGER
431    *       - Property::VECTOR2
432    *       - Property::VECTOR3
433    *       - Property::VECTOR4
434    *       - Property::MATRIX3
435    *       - Property::MATRIX
436    *       - Property::ROTATION
437    * @note If a property with the desired name already exists, then the value given is just set.
438    */
439   Property::Index RegisterProperty(std::string_view name, Property::Value propertyValue, Property::AccessMode accessMode);
440
441   /**
442    * @brief Retrieves a property value.
443    *
444    * @SINCE_1_0.0
445    * @param[in] index The index of the property
446    * @return The property value
447    * @note This returns the value set by SetProperty() or the animation target value if it is being animated.
448    * @note To get the current value on the scene-graph, use GetCurrentProperty().
449    */
450   Property::Value GetProperty(Property::Index index) const;
451
452   /**
453    * @brief Convenience function for obtaining a property of a known type.
454    *
455    * @SINCE_1_0.0
456    * @param[in] index The index of the property
457    * @return The property value
458    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
459    * @see GetProperty()
460    */
461   template<typename T>
462   T GetProperty(Property::Index index) const
463   {
464     Property::Value value = GetProperty(index);
465
466     return T(value.Get<T>());
467   }
468
469   /**
470    * @brief Retrieves the latest value of the property from the scene-graph.
471    *
472    * @SINCE_1_2.41
473    * @param[in] index The index of the property
474    * @return The property value
475    * @note This returns the value of the property in the last rendered frame so can be different to that
476    *       set by SetProperty() if the set-message has not been processed by the scene-graph yet.
477    * @note To retrieve the value set by SetProperty(), use GetProperty().
478    */
479   Property::Value GetCurrentProperty(Property::Index index) const;
480
481   /**
482    * @brief Convenience function for obtaining the current value of a property of a known type.
483    *
484    * @SINCE_1_2.41
485    * @param[in] index The index of the property
486    * @return The property value
487    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
488    * @see GetCurrentProperty()
489    */
490   template<typename T>
491   T GetCurrentProperty(Property::Index index) const
492   {
493     Property::Value value = GetCurrentProperty(index);
494
495     return T(value.Get<T>());
496   }
497
498   /**
499    * @brief Sets all the properties in the given property map.
500    *
501    * @SINCE_1_9.27
502    * @param[in] properties The properties to set
503    */
504   void SetProperties(const Property::Map& properties);
505
506   /**
507    * @brief Retrieves all the properties and the values for this object
508    *
509    * @SINCE_1_9.27
510    * @param[out] properties A map which is populated with the index-value pairs
511    *
512    * @note The properties map will be cleared by this method first.
513    */
514   void GetProperties(Property::Map& properties);
515
516   /**
517    * @brief Retrieves all the property indices for this object (including custom properties).
518    *
519    * @SINCE_1_0.0
520    * @param[out] indices A container of property indices for this object
521    * @note The added container is cleared.
522    */
523   void GetPropertyIndices(Property::IndexContainer& indices) const;
524
525   /**
526    * @brief Determine if the custom property index exists on this object without throwing a Dali::Exception.
527    *
528    * @SINCE_1_9.27
529    * @note This does not check default properties.
530    * @param[in] index The index of the property to test for
531    */
532   bool DoesCustomPropertyExist(Property::Index index);
533
534   /**
535    * @brief Adds a property notification to this object.
536    *
537    * @SINCE_1_0.0
538    * @param[in] index The index of the property
539    * @param[in] condition The notification will be triggered when this condition is satisfied
540    * @return A handle to the newly created PropertyNotification
541    */
542   PropertyNotification AddPropertyNotification(Property::Index          index,
543                                                const PropertyCondition& condition);
544
545   /**
546    * @brief Adds a property notification to this object.
547    *
548    * @SINCE_1_0.0
549    * @param[in] index The index of the property
550    * @param[in] componentIndex Index to the component of a complex property such as a Vector
551    * @param[in] condition The notification will be triggered when this condition is satisfied
552    * @return A handle to the newly created PropertyNotification
553    */
554   PropertyNotification AddPropertyNotification(Property::Index          index,
555                                                int                      componentIndex,
556                                                const PropertyCondition& condition);
557
558   /**
559    * @brief Removes a property notification from this object.
560    *
561    * @SINCE_1_0.0
562    * @param[in] propertyNotification The propertyNotification to be removed
563    */
564   void RemovePropertyNotification(Dali::PropertyNotification propertyNotification);
565
566   /**
567    * @brief Removes all property notifications from this object.
568    * @SINCE_1_0.0
569    */
570   void RemovePropertyNotifications();
571
572   // Constraints
573
574   /**
575    * @brief Removes all constraints from an Object.
576    *
577    * @SINCE_1_0.0
578    * @pre The object has been initialized.
579    */
580   void RemoveConstraints();
581
582   /**
583    * @brief Removes all the constraint from the Object with a matching tag.
584    *
585    * @SINCE_1_0.0
586    * @param[in] tag The tag of the constraints which will be removed
587    * @pre The Object has been initialized.
588    */
589   void RemoveConstraints(uint32_t tag);
590
591   /**
592    * @brief Index operator, using integer lookup.
593    *
594    * Returns an object that can be assigned to or cast from, enabling
595    * the indexed property to be either read or written.
596    *
597    * @param[in] index The index of the property to access.
598    * @return indirect value. Should have shorter scope than the handle
599    */
600   IndirectValue operator[](Property::Index index);
601
602   /**
603    * @brief Index operator, using name lookup.
604    *
605    * Returns an object that can be assigned to or cast from, enabling
606    * the named property to be either read or written.
607    *
608    * @param[in] name The name of the property to access.
609    * @return indirect value. Should have shorter scope than the handle
610    */
611   IndirectValue operator[](const std::string& name);
612
613 public: // Signals
614   /**
615    * @brief Get a signal when a property is set on this object through the API (i.e. not when animating)
616    *
617    * @SINCE_1_9.27
618    * @return The signal to attach a connection to.
619    */
620   PropertySetSignalType& PropertySetSignal();
621 };
622
623 /**
624  * @brief This namespace provides a convenient function to create an object with a custom "weight" property.
625  * @SINCE_1_0.0
626  */
627 namespace WeightObject
628 {
629 DALI_CORE_API extern const Property::Index WEIGHT; ///< name "weight", type FLOAT
630
631 /**
632  * @brief Convenience function to create an object with a custom "weight" property.
633  *
634  * @SINCE_1_0.0
635  * @return A handle to a newly allocated object
636  */
637 DALI_CORE_API Handle New();
638
639 } // namespace WeightObject
640
641 /**
642  * @}
643  */
644 } // namespace Dali
645
646 #endif // DALI_HANDLE_H