Merge "Making DALi core internals typesafe using guaranteed types; uint8_t, uint32_t...
[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    * Destructor
64    */
65   virtual ~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   uint32_t GetPropertyCount() const;
111
112   /**
113    * Adds the property indices to the container specified.
114    * @param[in/out] indices The container where the property indices are added.
115    */
116   void GetPropertyIndices( Property::IndexContainer& indices ) const;
117
118   /**
119    * @copydoc Dali::TypeInfo::GetPropertyName() const
120    */
121   const std::string& GetPropertyName( Property::Index index ) const;
122
123   /*
124    * Add an action function
125    */
126   void AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function );
127
128   /*
129    * Add a function for connecting a signal.
130    * @param[in] signalName The name of the signal.
131    * @param[in] function The function used for connecting to the signal.
132    */
133   void AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function );
134
135   /**
136    * Adds an event-thread only property to the type.
137    * @param[in] name The name of the property.
138    * @param[in] index The index of the property.
139    * @param[in] type The Property::Type.
140    * @param[in] setFunc The function to call to set the property (Can be NULL).
141    * @param[in] getFunc The function to call to retrieve the value of the property.
142    */
143   void AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc );
144
145   /**
146    * Adds an event-thread only property to the type.
147    * @param[in] name The name of the property.
148    * @param[in] index The index of the property.
149    * @param[in] type The Property::Type.
150    * @param[in] setFunc The function to call to set the property (Can be NULL).
151    * @param[in] getFunc The function to call to retrieve the value of the property.
152    */
153   void AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc);
154
155   /**
156    * Adds an animatable property to the type.
157    * @param[in] name The name of the property.
158    * @param[in] index The index of the property
159    * @param[in] type The Property::Type.
160    */
161   void AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type );
162
163   /**
164    * Adds an animatable property with the given default value.
165    * @param[in] name The name of the property.
166    * @param[in] index The index of the property
167    * @param[in] type The Property::Type.
168    */
169   void AddAnimatableProperty( const std::string& name, Property::Index index, const Property::Value& defaultValue );
170
171   /**
172    * Adds a component of an animatable property to the type.
173    * The animatable property must have been type-registered and must be a Vector2, Vector3 or Vector4 type.
174    * @param[in] name The name of the component.
175    * @param[in] index The index of the property
176    * @param[in] baseIndex The index of the base animatable property
177    * @param[in] component The index The index of the component.
178    */
179   void AddAnimatablePropertyComponent( const std::string& name, Property::Index index, Property::Index baseIndex, uint32_t componentIndex );
180
181   /**
182    * Adds a child property to the type.
183    * @param[in] name The name of the property.
184    * @param[in] index The index of the property
185    * @param[in] type The Property::Type.
186    */
187   void AddChildProperty( const std::string& name, Property::Index index, Property::Type type );
188
189   /**
190    * Do an action on base object
191    * @param [in] object The base object to act upon
192    * @param [in] actionName The name of the desired action
193    * @param [in] properties The arguments of the action
194    * @return bool If the action could be executed
195    */
196   bool DoActionTo(BaseObject *object, const std::string &actionName, const Property::Map &properties);
197
198   /**
199    * Connects a callback function with the object's signals.
200    * @param[in] object The object providing the signal.
201    * @param[in] tracker Used to disconnect the signal.
202    * @param[in] signalName The signal to connect to.
203    * @param[in] functor A newly allocated FunctorDelegate.
204    * @return True if the signal was connected.
205    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
206    */
207   bool ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor );
208
209   /**
210    * Given a property name, retrieve the index.
211    * @param[in] name The name of the property.
212    * @return The index associated with that name.
213    */
214   Property::Index GetPropertyIndex( const std::string& name ) const;
215
216   /**
217    * Given a property index, retrieve the index of its base property.
218    * @param[in] index The index of the property.
219    * @return The index of the base property associated with the given property index.
220    */
221   Property::Index GetBasePropertyIndex( Property::Index index ) const;
222
223   /**
224    * Given a property index, retrieve its component index.
225    * @param[in] index The index of the property.
226    * @return The component index associated with that property index.
227    */
228   int32_t GetComponentIndex( Property::Index index ) const;
229
230   /**
231    * Checks if there is a setter for the property. If there is then it is writable.
232    * @param[in] index The property index.
233    * @return True, if writable, false otherwise.
234    */
235   bool IsPropertyWritable( Property::Index index ) const;
236
237   /**
238    * Retrieve the Property::Type of the property at the given index.
239    * @param[in] index The property index.
240    * @return The Property::Type at that index.
241    */
242   Property::Type GetPropertyType( Property::Index index ) const;
243
244   /**
245    * Given a child property name, retrieve the index.
246    * @param[in] name The name of the child property.
247    * @return The index associated with that name.
248    */
249   Property::Index GetChildPropertyIndex( const std::string& name ) const;
250
251   /**
252    * Retrieve the name of the child property at the given index.
253    * @param[in] index The property index.
254    * @return The name of the child property.
255    */
256   const std::string& GetChildPropertyName( Property::Index index ) const;
257
258   /**
259    * Retrieve the Property::Type of the child property at the given index.
260    * @param[in] index The property index.
261    * @return The Property::Type at that index.
262    */
263   Property::Type GetChildPropertyType( Property::Index index ) const;
264
265   /**
266    * Retrive the child indices into the given container.
267    * @param[in,out] indices The container to put the child indices into
268    */
269   void GetChildPropertyIndices( Property::IndexContainer& indices ) const;
270
271   /**
272    * Retrieve the default value of the property at the given index.
273    * @param[in] index The property index.
274    * @return The default property value at that index.
275    */
276   Property::Value GetPropertyDefaultValue( Property::Index index ) const;
277
278   /**
279    * Sets the value of a property at the index specified for the given object.
280    * @param[in] object The object whose property is to be set.
281    * @param[in] index The property index.
282    * @param[in] value The value to set.
283    */
284   void SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const;
285
286   /**
287    * Sets the value of a property with the name specified for the given object.
288    * @param[in] object The object whose property is to be set.
289    * @param[in] name The property name.
290    * @param[in] value The value to set.
291    */
292   void SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const;
293
294   /**
295    * Retrieves the value of a property at the index specified for the given object.
296    * @param[in] object The object whose property is to be queried.
297    * @param[in] index The property index.
298    * @return The current value of the property.
299    */
300   Property::Value GetProperty( const BaseObject *object, Property::Index index ) const;
301
302   /**
303    * Retrieves the value of a property with the name specified for the given object.
304    * @param[in] object The object whose property is to be queried.
305    * @param[in] name The property name.
306    * @return The current value of the property.
307    */
308   Property::Value GetProperty( const BaseObject *object, const std::string& name ) const;
309
310 private:
311
312   struct RegisteredProperty
313   {
314     RegisteredProperty()
315     : type( Property::NONE ),
316       setFunc( NULL ),
317       getFunc( NULL ),
318       name(),
319       basePropertyIndex(Property::INVALID_INDEX),
320       componentIndex(Property::INVALID_COMPONENT_INDEX)
321     {
322     }
323
324     RegisteredProperty( Property::Type propType, const std::string& propName, Property::Index basePropertyIndex, int32_t componentIndex )
325         : type( propType ),
326           setFunc( NULL ),
327           getFunc( NULL ),
328           name( propName ),
329           basePropertyIndex(basePropertyIndex),
330           componentIndex(componentIndex)
331     {
332     }
333
334
335     RegisteredProperty( Property::Type propType, Dali::TypeInfo::SetPropertyFunction set, Dali::TypeInfo::GetPropertyFunction get, const std::string& propName, Property::Index basePropertyIndex, int componentIndex )
336     : type( propType ),
337       setFunc( set ),
338       getFunc( get ),
339       name( propName ),
340       basePropertyIndex(basePropertyIndex),
341       componentIndex(componentIndex)
342     {
343     }
344
345     RegisteredProperty( Property::Type propType, Dali::CSharpTypeInfo::SetPropertyFunction set, Dali::CSharpTypeInfo::GetPropertyFunction get, const std::string& propName, Property::Index basePropertyIndex, int componentIndex )
346     : type( propType ),
347       cSharpSetFunc( set ),
348       cSharpGetFunc( get ),
349       name( propName ),
350       basePropertyIndex(basePropertyIndex),
351       componentIndex(componentIndex)
352     {
353     }
354
355
356     Property::Type type;
357     union
358     {
359       Dali::TypeInfo::SetPropertyFunction setFunc;
360       Dali::CSharpTypeInfo::SetPropertyFunction cSharpSetFunc;
361     };
362     union
363     {
364       Dali::TypeInfo::GetPropertyFunction getFunc;
365       Dali::CSharpTypeInfo::GetPropertyFunction cSharpGetFunc;
366     };
367     std::string name;
368     Property::Index basePropertyIndex;
369     int32_t componentIndex;
370   };
371
372   typedef std::pair<std::string, Dali::TypeInfo::SignalConnectorFunction > ConnectionPair;
373   typedef std::pair<std::string, Dali::TypeInfo::ActionFunction > ActionPair;
374   typedef std::pair<Property::Index, RegisteredProperty> RegisteredPropertyPair;
375   typedef std::pair<Property::Index, Property::Value> PropertyDefaultValuePair;
376
377   typedef std::vector< ActionPair > ActionContainer;
378   typedef std::vector< ConnectionPair > ConnectorContainer;
379   typedef std::vector< RegisteredPropertyPair > RegisteredPropertyContainer;
380   typedef std::vector< PropertyDefaultValuePair > PropertyDefaultValueContainer;
381
382
383   /**
384    * Append properties from registeredProperties onto indices.
385    * @param[in,out] indices The vector to append indices onto
386    * @param[in] registeredProperties The container to retrive indices from
387    */
388   void AppendProperties( Dali::Property::IndexContainer& indices,
389                          const TypeInfo::RegisteredPropertyContainer& registeredProperties ) const;
390
391 private:
392
393   std::string mTypeName;
394   std::string mBaseTypeName;
395   bool        mCSharpType:1;    ///< Whether this type info is for a CSharp control (instead of C++)
396   union
397   {
398     Dali::TypeInfo::CreateFunction mCreate;
399     Dali::CSharpTypeInfo::CreateFunction mCSharpCreate;
400   };
401   ActionContainer mActions;
402   ConnectorContainer mSignalConnectors;
403   RegisteredPropertyContainer mRegisteredProperties;
404   RegisteredPropertyContainer mRegisteredChildProperties;
405   PropertyDefaultValueContainer mPropertyDefaultValues;
406 };
407
408 } // namespace Internal
409
410 // Helpers for public-api forwarding methods
411
412 inline Internal::TypeInfo& GetImplementation(Dali::TypeInfo& typeInfo)
413 {
414   DALI_ASSERT_ALWAYS(typeInfo);
415
416   BaseObject& handle = typeInfo.GetBaseObject();
417
418   return static_cast<Internal::TypeInfo&>(handle);
419 }
420
421 inline const Internal::TypeInfo& GetImplementation(const Dali::TypeInfo& typeInfo)
422 {
423   DALI_ASSERT_ALWAYS(typeInfo);
424
425   const BaseObject& handle = typeInfo.GetBaseObject();
426
427   return static_cast<const Internal::TypeInfo&>(handle);
428 }
429
430 } // namespace Dali
431
432 #endif // header