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