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