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