Conversion to Apache 2.0 license
[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) 2014 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();
62
63   /**
64    * @copydoc Dali::TypeInfo::GetBaseName
65    */
66   const std::string& GetBaseName();
67
68   /**
69    * @copydoc TypeInfo::CreateFunction
70    */
71   BaseHandle CreateInstance();
72
73   /**
74    * @copydoc Dali::TypeInfo::GetCreator
75    */
76   Dali::TypeInfo::CreateFunction GetCreator();
77
78   /**
79    * @copydoc Dali::TypeInfo::GetActions
80    */
81   Dali::TypeInfo::NameContainer GetActions();
82
83   /**
84    * @copydoc Dali::TypeInfo::GetSignals
85    */
86   Dali::TypeInfo::NameContainer GetSignals();
87
88   /**
89    * Adds the property indices to the container specified.
90    * @param[in/out] indices The container where the property indices are added.
91    */
92   void GetPropertyIndices( Property::IndexContainer& indices ) const;
93
94   /**
95    * @copydoc Dali::TypeInfo::GetPropertyName() const
96    */
97   const std::string& GetPropertyName( Property::Index index ) const;
98
99   /*
100    * Add an action function
101    */
102   void AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function );
103
104   /*
105    * Add a function for connecting a signal.
106    * @param[in] signalName The name of the signal.
107    * @param[in] function The function used for connecting to the signal.
108    */
109   void AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunctionV2 function );
110
111   /**
112    * Adds an event-thread only property to the type.
113    * @param[in] name The name of the property.
114    * @param[in] index The index of the property.
115    * @param[in] type The Property::Type.
116    * @param[in] setFunc The function to call to set the property (Can be NULL).
117    * @param[in] getFunc The function to call to retrieve the value of the property.
118    */
119   void AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc );
120
121   /**
122    * Do an action on base object
123    * @param [in] object The base object to act upon
124    * @param [in] actionName The name of the desired action
125    * @param [in] properties The arguments of the action
126    * @return bool If the action could be executed
127    */
128   bool DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties);
129
130   /**
131    * Connects a callback function with the object's signals.
132    * @param[in] object The object providing the signal.
133    * @param[in] tracker Used to disconnect the signal.
134    * @param[in] signalName The signal to connect to.
135    * @param[in] functor A newly allocated FunctorDelegate.
136    * @return True if the signal was connected.
137    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
138    */
139   bool ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor );
140
141   /**
142    * Retrieve the property count for this type.
143    * @return The total number of properties.
144    */
145   unsigned int GetPropertyCount() const;
146
147   /**
148    * Given a property name, retrieve the index.
149    * @param[in] name The name of the property.
150    * @return The index associated with that name.
151    */
152   Property::Index GetPropertyIndex( const std::string& name ) const;
153
154   /**
155    * Checks if there is a setter for the property. If there is then it is writable.
156    * @param[in] index The property index.
157    * @return True, if writable, false otherwise.
158    */
159   bool IsPropertyWritable( Property::Index index ) const;
160
161   /**
162    * Retrieve the Property::Type of the property at the given index.
163    * @param[in] index The property index.
164    * @return The Property::Type at that index.
165    */
166   Property::Type GetPropertyType( Property::Index index ) const;
167
168   /**
169    * Sets the value of a property at the index specified for the given object.
170    * @param[in] object The object whose property is to be set.
171    * @param[in] index The property index.
172    * @param[in] value The value to set.
173    */
174   void SetProperty( BaseObject *object, Property::Index index, const Property::Value& value );
175
176   /**
177    * Sets the value of a property with the name specified for the given object.
178    * @param[in] object The object whose property is to be set.
179    * @param[in] name The property name.
180    * @param[in] value The value to set.
181    */
182   void SetProperty( BaseObject *object, const std::string& name, const Property::Value& value );
183
184   /**
185    * Retrieves the value of a property at the index specified for the given object.
186    * @param[in] object The object whose property is to be queried.
187    * @param[in] index The property index.
188    * @return The current value of the property.
189    */
190   Property::Value GetProperty( const BaseObject *object, Property::Index index );
191
192   /**
193    * Retrieves the value of a property with the name specified for the given object.
194    * @param[in] object The object whose property is to be queried.
195    * @param[in] name The property name.
196    * @return The current value of the property.
197    */
198   Property::Value GetProperty( const BaseObject *object, const std::string& name );
199
200 private:
201
202   struct RegisteredProperty
203   {
204     RegisteredProperty()
205     : type( Property::NONE ),
206       setFunc( NULL ),
207       getFunc( NULL ),
208       name()
209     {
210     }
211
212     RegisteredProperty( Property::Type propType, Dali::TypeInfo::SetPropertyFunction set, Dali::TypeInfo::GetPropertyFunction get, const std::string& propName )
213     : type( propType ),
214       setFunc( set ),
215       getFunc( get ),
216       name( propName )
217     {
218     }
219
220     Property::Type type;
221     Dali::TypeInfo::SetPropertyFunction setFunc;
222     Dali::TypeInfo::GetPropertyFunction getFunc;
223     std::string name;
224   };
225
226   typedef std::pair<std::string, Dali::TypeInfo::SignalConnectorFunctionV2 > ConnectionPairV2;
227   typedef std::pair<std::string, Dali::TypeInfo::ActionFunction > ActionPair;
228   typedef std::pair<Property::Index, RegisteredProperty> RegisteredPropertyPair;
229
230   typedef std::vector< ActionPair > ActionContainer;
231   typedef std::vector< ConnectionPairV2 > ConnectorContainerV2;
232   typedef std::vector< RegisteredPropertyPair > RegisteredPropertyContainer;
233
234   std::string mTypeName;
235   std::string mBaseTypeName;
236   Dali::TypeInfo::CreateFunction mCreate;
237   ActionContainer mActions;
238   ConnectorContainerV2 mSignalConnectors;
239   RegisteredPropertyContainer mRegisteredProperties;
240 };
241
242 } // namespace Internal
243
244 // Helpers for public-api forwarding methods
245
246 inline Internal::TypeInfo& GetImplementation(Dali::TypeInfo& typeInfo)
247 {
248   DALI_ASSERT_ALWAYS(typeInfo);
249
250   BaseObject& handle = typeInfo.GetBaseObject();
251
252   return static_cast<Internal::TypeInfo&>(handle);
253 }
254
255 inline const Internal::TypeInfo& GetImplementation(const Dali::TypeInfo& typeInfo)
256 {
257   DALI_ASSERT_ALWAYS(typeInfo);
258
259   const BaseObject& handle = typeInfo.GetBaseObject();
260
261   return static_cast<const Internal::TypeInfo&>(handle);
262 }
263
264 } // namespace Dali
265
266 #endif // header