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