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