[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-info-impl.h
1 #ifndef DALI_INTERNAL_TYPE_INFO_H
2 #define DALI_INTERNAL_TYPE_INFO_H
3 #define USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO /// For safety. If some problem occured, just remove this define.
4
5 /*
6  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 // EXTERNAL INCLUDES
23 #include <string>
24
25 // INTERNAL INCLUDES
26 #ifdef USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO
27 #include <dali/internal/common/indexed-const-string-map.h>
28 #include <dali/internal/common/indexed-integer-map.h>
29 #endif
30
31 #include <dali/devel-api/object/csharp-type-info.h>
32 #include <dali/internal/common/const-string.h>
33 #include <dali/internal/event/object/default-property-metadata.h>
34 #include <dali/public-api/object/base-handle.h>
35 #include <dali/public-api/object/base-object.h>
36 #include <dali/public-api/object/property.h>
37 #include <dali/public-api/object/type-info.h>
38
39 namespace Dali
40 {
41 namespace Internal
42 {
43 class PropertyDetails;
44 class TypeRegistry;
45
46 /**
47  * A TypeInfo class to support registered type creation, and introspection of available
48  * actions and signal connection.
49  * It also supports doing actions and connecting signal handlers. Note this is properly
50  * conducted through the BaseHandle interface which uses the TypeRegistry to walk
51  * all base classes.
52  */
53 class TypeInfo : public BaseObject
54 {
55 public:
56   /**
57    * Create TypeInfo
58    * @param [name] the registered name
59    * @param [baseName] the base type registered name
60    * @param [creator] the creator function for this type
61    */
62   TypeInfo(const std::string& name, const std::string& baseName, Dali::TypeInfo::CreateFunction creator, const Dali::PropertyDetails* defaultProperties, Property::Index defaultPropertyCount);
63
64   /**
65    * Create TypeInfo for a csharp object
66    * @param [name] the registered name
67    * @param [baseName] the base type registered name
68    * @param [creator] the creator function for this type
69    */
70   TypeInfo(const std::string& name, const std::string& baseName, Dali::CSharpTypeInfo::CreateFunction creator);
71
72   /**
73    * Destructor
74    */
75   ~TypeInfo() override;
76
77   /**
78    * @copydoc Dali::TypeInfo::GetName
79    */
80   const std::string& GetName() const;
81
82   /**
83    * @copydoc Dali::TypeInfo::GetBaseName
84    */
85   const std::string& GetBaseName() const;
86
87   /**
88    * @copydoc TypeInfo::CreateFunction
89    */
90   BaseHandle CreateInstance() const;
91
92   /**
93    * @copydoc Dali::TypeInfo::GetCreator
94    */
95   Dali::TypeInfo::CreateFunction GetCreator() const;
96
97   /**
98    * @copydoc Dali::TypeInfo::GetActionCount
99    */
100   uint32_t GetActionCount() const;
101
102   /**
103    * @copydoc Dali::TypeInfo::GetActionName
104    */
105   std::string GetActionName(uint32_t index) const;
106
107   /**
108    * @copydoc Dali::TypeInfo::GetSignalCount
109    */
110   uint32_t GetSignalCount() const;
111
112   /**
113    * @copydoc Dali::TypeInfo::GetSignalName
114    */
115   std::string GetSignalName(uint32_t index) const;
116
117   /**
118    * @copydoc Dali::TypeInfo::GetPropertyCount
119    */
120   uint32_t GetPropertyCount() const;
121
122   /**
123    * Adds the property indices to the container specified.
124    * @param[in/out] indices The container where the property indices are added.
125    */
126   void GetPropertyIndices(Property::IndexContainer& indices) const;
127
128   /**
129    * @copydoc Dali::TypeInfo::GetPropertyName() const
130    * this API exists to keep the old public API, which cannot be changed
131    */
132   std::string_view GetRegisteredPropertyName(Property::Index index) const;
133
134   /**
135    * Returns the property name for given index
136    *
137    * @param index of the property
138    * @return name or empty string
139    */
140   std::string_view GetPropertyName(Property::Index index) const;
141
142   /*
143    * Add an action function
144    */
145   void AddActionFunction(std::string actionName, Dali::TypeInfo::ActionFunction function);
146
147   /*
148    * Add a function for connecting a signal.
149    * @param[in] signalName The name of the signal.
150    * @param[in] function The function used for connecting to the signal.
151    */
152   void AddConnectorFunction(std::string signalName, Dali::TypeInfo::SignalConnectorFunction function);
153
154   /**
155    * Adds an event-thread only property to the type.
156    * @param[in] name The name of the property.
157    * @param[in] index The index of the property.
158    * @param[in] type The Property::Type.
159    * @param[in] setFunc The function to call to set the property (Can be nullptr).
160    * @param[in] getFunc The function to call to retrieve the value of the property.
161    */
162   void AddProperty(std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc);
163
164   /**
165    * Adds an event-thread only property to the type.
166    * @param[in] name The name of the property.
167    * @param[in] index The index of the property.
168    * @param[in] type The Property::Type.
169    * @param[in] setFunc The function to call to set the property (Can be nullptr).
170    * @param[in] getFunc The function to call to retrieve the value of the property.
171    */
172   void AddProperty(std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc);
173
174   /**
175    * Adds an animatable property to the type.
176    * @param[in] name The name of the property.
177    * @param[in] index The index of the property
178    * @param[in] type The Property::Type.
179    */
180   void AddAnimatableProperty(std::string name, Property::Index index, Property::Type type);
181
182   /**
183    * Adds an animatable property with the given default value.
184    * @param[in] name The name of the property.
185    * @param[in] index The index of the property
186    * @param[in] type The Property::Type.
187    */
188   void AddAnimatableProperty(std::string name, Property::Index index, Property::Value defaultValue);
189
190   /**
191    * Adds a component of an animatable property to the type.
192    * The animatable property must have been type-registered and must be a Vector2, Vector3 or Vector4 type.
193    * @param[in] name The name of the component.
194    * @param[in] index The index of the property
195    * @param[in] baseIndex The index of the base animatable property
196    * @param[in] component The index The index of the component.
197    */
198   void AddAnimatablePropertyComponent(std::string name, Property::Index index, Property::Index baseIndex, uint32_t componentIndex);
199
200   /**
201    * Adds a child property to the type.
202    * @param[in] name The name of the property.
203    * @param[in] index The index of the property
204    * @param[in] type The Property::Type.
205    */
206   void AddChildProperty(std::string name, Property::Index index, Property::Type type);
207
208   /**
209    * Do an action on base object
210    * @param [in] object The base object to act upon
211    * @param [in] actionName The name of the desired action
212    * @param [in] properties The arguments of the action
213    * @return bool If the action could be executed
214    */
215   bool DoActionTo(BaseObject* object, const std::string& actionName, const Property::Map& properties);
216
217   /**
218    * Connects a callback function with the object's signals.
219    * @param[in] object The object providing the signal.
220    * @param[in] tracker Used to disconnect the signal.
221    * @param[in] signalName The signal to connect to.
222    * @param[in] functor A newly allocated FunctorDelegate.
223    * @return True if the signal was connected.
224    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
225    */
226   bool ConnectSignal(BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor);
227
228   /**
229    * Given a property name, retrieve the index.
230    * @param[in] name The name of the property.
231    * @return The index associated with that name.
232    */
233   Property::Index GetPropertyIndex(ConstString name) const;
234
235   /**
236    * Given a property index, retrieve the index of its base property.
237    * @param[in] index The index of the property.
238    * @return The index of the base property associated with the given property index.
239    */
240   Property::Index GetBasePropertyIndex(Property::Index index) const;
241
242   /**
243    * Given a property index, retrieve its component index.
244    * @param[in] index The index of the property.
245    * @return The component index associated with that property index.
246    */
247   int32_t GetComponentIndex(Property::Index index) const;
248
249   /**
250    * @param[in] index The property index.
251    * @return True, if writable, false otherwise.
252    */
253   bool IsPropertyWritable(Property::Index index) const;
254
255   /**
256    * @param[in] index The property index.
257    * @return True, if animatable, false otherwise.
258    */
259   bool IsPropertyAnimatable(Property::Index index) const;
260
261   /**
262    * @param[in] index The property index.
263    * @return True, if a constraint input, false otherwise.
264    */
265   bool IsPropertyAConstraintInput(Property::Index index) const;
266
267   /**
268    * Retrieve the Property::Type of the property at the given index.
269    * @param[in] index The property index.
270    * @return The Property::Type at that index.
271    */
272   Property::Type GetPropertyType(Property::Index index) const;
273
274   /**
275    * Given a child property name, retrieve the index.
276    * @param[in] name The name of the child property.
277    * @return The index associated with that name.
278    */
279   Property::Index GetChildPropertyIndex(ConstString name) const;
280
281   /**
282    * Retrieve the name of the child property at the given index.
283    * @param[in] index The property index.
284    * @return The name of the child property.
285    */
286   std::string_view GetChildPropertyName(Property::Index index) const;
287
288   /**
289    * Retrieve the Property::Type of the child property at the given index.
290    * @param[in] index The property index.
291    * @return The Property::Type at that index.
292    */
293   Property::Type GetChildPropertyType(Property::Index index) const;
294
295   /**
296    * Retrive the child indices into the given container.
297    * @param[in,out] indices The container to put the child indices into
298    */
299   void GetChildPropertyIndices(Property::IndexContainer& indices) const;
300
301   /**
302    * Retrieve the default value of the property at the given index.
303    * @param[in] index The property index.
304    * @return The default property value at that index.
305    */
306   Property::Value GetPropertyDefaultValue(Property::Index index) const;
307
308   /**
309    * Sets the value of a property at the index specified for the given object.
310    * @param[in] object The object whose property is to be set.
311    * @param[in] index The property index.
312    * @param[in] value The value to set.
313    */
314   void SetProperty(BaseObject* object, Property::Index index, Property::Value value) const;
315
316   /**
317    * Sets the value of a property with the name specified for the given object.
318    * @param[in] object The object whose property is to be set.
319    * @param[in] name The property name.
320    * @param[in] value The value to set.
321    */
322   void SetProperty(BaseObject* object, const std::string& name, Property::Value value) const;
323
324   /**
325    * Retrieves the value of a property at the index specified for the given object.
326    * @param[in] object The object whose property is to be queried.
327    * @param[in] index The property index.
328    * @return The current value of the property.
329    */
330   Property::Value GetProperty(const BaseObject* object, Property::Index index) const;
331
332   /**
333    * Retrieves the value of a property with the name specified for the given object.
334    * @param[in] object The object whose property is to be queried.
335    * @param[in] name The property name.
336    * @return The current value of the property.
337    */
338   Property::Value GetProperty(const BaseObject* object, const std::string& name) const;
339
340 private:
341   struct RegisteredProperty
342   {
343     RegisteredProperty(Property::Type propType, ConstString propName, Property::Index basePropertyIndex, int32_t componentIndex)
344     : type(propType),
345       setFunc(nullptr),
346       getFunc(nullptr),
347       name(propName),
348       basePropertyIndex(basePropertyIndex),
349       componentIndex(componentIndex)
350     {
351     }
352
353     RegisteredProperty(Property::Type propType, Dali::TypeInfo::SetPropertyFunction set, Dali::TypeInfo::GetPropertyFunction get, ConstString propName, Property::Index basePropertyIndex, int componentIndex)
354     : type(propType),
355       setFunc(set),
356       getFunc(get),
357       name(propName),
358       basePropertyIndex(basePropertyIndex),
359       componentIndex(componentIndex)
360     {
361     }
362
363     RegisteredProperty(Property::Type propType, Dali::CSharpTypeInfo::SetPropertyFunction set, Dali::CSharpTypeInfo::GetPropertyFunction get, ConstString propName, Property::Index basePropertyIndex, int componentIndex)
364     : type(propType),
365       cSharpSetFunc(set),
366       cSharpGetFunc(get),
367       name(propName),
368       basePropertyIndex(basePropertyIndex),
369       componentIndex(componentIndex)
370     {
371     }
372
373     Property::Type type = Property::NONE;
374     union
375     {
376       Dali::TypeInfo::SetPropertyFunction       setFunc = nullptr;
377       Dali::CSharpTypeInfo::SetPropertyFunction cSharpSetFunc; // only one field can be initialized but this will have same value anyways
378     };
379     union
380     {
381       Dali::TypeInfo::GetPropertyFunction       getFunc = nullptr;
382       Dali::CSharpTypeInfo::GetPropertyFunction cSharpGetFunc; // only one field can be initialized but this will have same value anyways
383     };
384     ConstString     name;
385     Property::Index basePropertyIndex = Property::INVALID_INDEX;
386     int32_t         componentIndex    = Property::INVALID_COMPONENT_INDEX;
387   };
388
389   using ActionPair     = std::pair<std::string, Dali::TypeInfo::ActionFunction>;
390   using ConnectionPair = std::pair<std::string, Dali::TypeInfo::SignalConnectorFunction>;
391 #ifdef USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO
392   using RegisteredPropertyPair   = std::pair<std::uint32_t, RegisteredProperty>;
393   using PropertyDefaultValuePair = std::pair<std::uint32_t, Property::Value>;
394 #else
395   using RegisteredPropertyPair        = std::pair<Property::Index, RegisteredProperty>;
396   using PropertyDefaultValuePair      = std::pair<Property::Index, Property::Value>;
397 #endif
398
399 #ifdef USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO
400   using ActionContainer               = Dali::Internal::IndexedConstStringMap<Dali::TypeInfo::ActionFunction>;
401   using ConnectorContainer            = Dali::Internal::IndexedConstStringMap<Dali::TypeInfo::SignalConnectorFunction>;
402   using RegisteredPropertyContainer   = Dali::Internal::IndexedIntegerMap<RegisteredProperty>;
403   using PropertyDefaultValueContainer = Dali::Internal::IndexedIntegerMap<Property::Value>;
404 #else
405   using ActionContainer               = std::vector<ActionPair>;
406   using ConnectorContainer            = std::vector<ConnectionPair>;
407   using RegisteredPropertyContainer   = std::vector<RegisteredPropertyPair>;
408   using PropertyDefaultValueContainer = std::vector<PropertyDefaultValuePair>;
409 #endif
410
411   /**
412    * Append properties from registeredProperties onto indices.
413    * @param[in,out] indices The vector to append indices onto
414    * @param[in] registeredProperties The container to retrive indices from
415    */
416   void AppendProperties(Dali::Property::IndexContainer&              indices,
417                         const TypeInfo::RegisteredPropertyContainer& registeredProperties) const;
418
419 private:
420   TypeRegistry&               mTypeRegistry;
421   mutable Internal::TypeInfo* mBaseType; // allow changing from const methods, initialised inside constructor
422   std::string                 mTypeName;
423   std::string                 mBaseTypeName;
424   union
425   {
426     Dali::TypeInfo::CreateFunction       mCreate = nullptr;
427     Dali::CSharpTypeInfo::CreateFunction mCSharpCreate; // only one field can be initialized but this will have same value anyways
428   };
429   ActionContainer               mActions;
430   ConnectorContainer            mSignalConnectors;
431   RegisteredPropertyContainer   mRegisteredProperties;
432   RegisteredPropertyContainer   mRegisteredChildProperties;
433   PropertyDefaultValueContainer mPropertyDefaultValues;
434   const Dali::PropertyDetails*  mDefaultProperties    = nullptr;
435   Property::Index               mDefaultPropertyCount = 0;
436   bool                          mCSharpType           = false; ///< Whether this type info is for a CSharp control (instead of C++)
437 };
438
439 } // namespace Internal
440
441 // Helpers for public-api forwarding methods
442
443 inline Internal::TypeInfo& GetImplementation(Dali::TypeInfo& typeInfo)
444 {
445   DALI_ASSERT_ALWAYS(typeInfo);
446
447   BaseObject& handle = typeInfo.GetBaseObject();
448
449   return static_cast<Internal::TypeInfo&>(handle);
450 }
451
452 inline const Internal::TypeInfo& GetImplementation(const Dali::TypeInfo& typeInfo)
453 {
454   DALI_ASSERT_ALWAYS(typeInfo);
455
456   const BaseObject& handle = typeInfo.GetBaseObject();
457
458   return static_cast<const Internal::TypeInfo&>(handle);
459 }
460
461 } // namespace Dali
462
463 #endif // DALI_INTERNAL_TYPE_INFO_H