4f1d93c4a4c8b4b8053857a689603511ad9ee17c
[platform/core/uifw/dali-core.git] / dali / public-api / object / type-registry.h
1 #ifndef __DALI_TYPE_REGISTRY_H__
2 #define __DALI_TYPE_REGISTRY_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
22 // EXTERNAL INCLUDES
23 #include <typeinfo>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/object/base-handle.h>
27 #include <dali/public-api/object/type-info.h>
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_object
33  * @{
34  */
35
36 namespace Internal DALI_INTERNAL
37 {
38 class TypeRegistry;
39 }
40
41 /**
42  * @brief The TypeRegistry allows registration of type instance creation functions.
43  *
44  * These can then be created later by name and down cast to the appropriate type.
45  *
46  * Usage: (Registering)
47  *
48  * In my-actor.cpp
49  * @code
50  * // Note: object construction in namespace scope is defined in a translation unit as being
51  * //       in appearance order (C++ standard 3.6/2). So TypeRegistration is declared first in
52  * //       the cpp file below. Signal, action and property declarations follow in any order.
53  * namespace
54  * {
55  *   TypeRegistration myActorType(typeid(MyActor), typeid(Actor), CreateMyActor );
56  *
57  *   SignalConnectorType( myActorType, "highlighted", ConnectSignalForMyActor );
58  *   TypeAction( myActorType, "open", DoMyActorAction );
59  *   TypeAction( myActorType, "close", DoMyActorAction );
60  *   PropertyRegistration( myActorType, "status", PropertyRegistration::START_INDEX, Property::BOOLEAN, SetPropertyFunction, GetPropertyFunction );
61  * }
62  * @endcode
63  *
64  * Usage: (Creation)
65  *
66  * @code
67  *   TypeRegistry::TypeInfo type = TypeRegistry::Get().GetTypeInfo("MyActor");
68  *   MyActor a = MyActor::DownCast(type.CreateInstance());
69  *   if(a)
70  *   {
71  *     ...
72  *   }
73  * @endcode
74  *
75  * CustomActor
76  *
77  *  Actors that inherit from the CustomActor framework must ensure the implementation
78  *  class has an identical name to the Actor class. This is to ensure the class can be
79  *  found at runtime for signals and actions. Otherwise these will silently fail.
80  *
81  *  As namespaces are discarded the convention is to use a namespace ie
82  *
83  *    'class MyActor {}; namespace Internal { class MyActor {}; }'
84  *
85  *  Warning: this arrangement will silently fail
86  *
87  *    'class MyActor {}; class MyActorImpl {};'
88  *
89  * Naming Conventions
90  *
91  *   Signal and action names follow properties and are by convention lower case hyphen
92  *   separated ie 'next-page'. This maintains consistency with the scripted interface.
93  *
94  * @SINCE_1_0.0
95  */
96 class DALI_IMPORT_API TypeRegistry : public BaseHandle
97 {
98 public:
99   /**
100    * @brief Get Type Registry handle.
101    *
102    * @SINCE_1_0.0
103    * @return TypeRegistry handle
104    */
105   static TypeRegistry Get();
106
107   /**
108    * @brief Allows the creation of an empty typeRegistry handle.
109    * @SINCE_1_0.0
110    */
111   TypeRegistry();
112
113   /**
114    * @brief destructor.
115    * @SINCE_1_0.0
116    */
117   ~TypeRegistry();
118
119   /**
120    * @brief This copy constructor is required for (smart) pointer semantics.
121    *
122    * @SINCE_1_0.0
123    * @param [in] handle A reference to the copied handle
124    */
125   TypeRegistry(const TypeRegistry& handle);
126
127   /**
128    * @brief This assignment operator is required for (smart) pointer semantics.
129    *
130    * @SINCE_1_0.0
131    * @param [in] rhs  A reference to the copied handle
132    * @return A reference to this
133    */
134   TypeRegistry& operator=(const TypeRegistry& rhs);
135
136   /**
137    * @brief Get TypeInfo for a registered type.
138    *
139    * @SINCE_1_0.0
140    * @param [in] uniqueTypeName A unique type name
141    * @return TypeInfo if the type exists otherwise an empty handle
142    */
143   TypeInfo GetTypeInfo( const std::string &uniqueTypeName );
144
145   /**
146    * @brief Get TypeInfo for a registered type.
147    *
148    * @SINCE_1_0.0
149    * @param [in] registerType The registered type info
150    * @return TypeInfo if the type exists otherwise an empty handle
151    */
152   TypeInfo GetTypeInfo( const std::type_info& registerType );
153
154   /**
155    * @brief Get type name count.
156    *
157    * @SINCE_1_0.0
158    * @return The count
159    */
160   size_t GetTypeNameCount() const;
161
162   /**
163    * @brief Get type names by index.
164    *
165    * @SINCE_1_0.0
166    * @return The type name or an empty string when index is not valid
167    */
168   std::string GetTypeName(size_t index) const;
169
170 public: // Not intended for application developers
171
172   /**
173    * @internal
174    * @brief This constructor is used by Dali Get() method.
175    *
176    * @SINCE_1_0.0
177    * @param [in] typeRegistry A pointer to a Dali resource
178    */
179   explicit DALI_INTERNAL TypeRegistry(Internal::TypeRegistry*typeRegistry);
180 };
181
182 /**
183  * @brief Register a type from type info.
184  * @SINCE_1_0.0
185  */
186 class DALI_IMPORT_API TypeRegistration
187 {
188 public:
189   /**
190    * @brief Constructor registers the type creation function.
191    *
192    * @SINCE_1_0.0
193    * @param [in] registerType the type info for the type to be registered
194    * @param [in] baseType the base type info of registerType
195    * @param [in] f registerType instance creation function
196    */
197   TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
198                     TypeInfo::CreateFunction f );
199
200   /**
201    * @brief Constructor registers the type creation function.
202    *
203    * @SINCE_1_0.0
204    * @param [in] registerType the type info for the type to be registered
205    * @param [in] baseType the base type info of registerType
206    * @param [in] f registerType instance creation function
207    * @param [in] callCreateOnInit If true the creation function is called as part of Dali initialisation
208    */
209   TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
210                     TypeInfo::CreateFunction f, bool callCreateOnInit );
211
212   /**
213    * @brief Constructor registers the type creation function for a named class or type.
214    *
215    * This allows types to be created dynamically from script. The name must be
216    * unique for successful registration.
217    * @SINCE_1_0.0
218    * @param [in] name the name of the type to be registered
219    * @param [in] baseType the base type info of registerType
220    * @param [in] f registerType instance creation function
221    */
222   TypeRegistration( const std::string& name, const std::type_info& baseType,
223                     TypeInfo::CreateFunction f );
224
225   /**
226    * @brief The name the type is registered under (derived from type_info).
227    *
228    * @SINCE_1_0.0
229    * @return the registered name or empty if unregistered
230    */
231   const std::string RegisteredName() const;
232
233 private:
234   TypeRegistry mReference; ///< Reference to the type registry
235   std::string mName;       ///< Name of the type
236 };
237
238 /**
239  * @brief Register a signal connector function to a registered type.
240  * @SINCE_1_0.0
241  */
242 class DALI_IMPORT_API SignalConnectorType
243 {
244 public:
245   /**
246    * @brief Constructor registers the type creation function.
247    *
248    * @SINCE_1_0.0
249    * @param [in] typeRegistration The TypeRegistration object
250    * @param [in] name The signal name
251    * @param [in] func The signal connector function
252    */
253   SignalConnectorType( TypeRegistration& typeRegistration, const std::string& name, TypeInfo::SignalConnectorFunction func );
254 };
255
256 /**
257  * @brief Register an action function.
258  * @SINCE_1_0.0
259  */
260 class DALI_IMPORT_API TypeAction
261 {
262 public:
263   /**
264    * @brief Constructor registers the type creation function.
265    *
266    * @SINCE_1_0.0
267    * @param [in] registered The TypeRegistration object
268    * @param [in] name The action name
269    * @param [in] f The action function
270    */
271   TypeAction( TypeRegistration &registered, const std::string &name, TypeInfo::ActionFunction f);
272 };
273
274 /**
275  * @brief Register a property for the given type.
276  * @SINCE_1_0.0
277  */
278 class DALI_IMPORT_API PropertyRegistration
279 {
280 public:
281
282   /**
283    * @brief This constructor registers the property with the registered type.
284    *
285    * This constructor is for event-thread only properties where the
286    * value of the property can be retrieved and set via specified
287    * functions.
288    *
289    * Functions of the following type may be used for setFunc and getFunc respectively:
290    * @code
291    *   void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
292    *   Property::Value GetProperty( BaseObject* object, Property::Index index );
293    * @endcode
294    *
295    * @SINCE_1_0.0
296    * @param [in] registered The TypeRegistration object
297    * @param [in] name The name of the property
298    * @param [in] index The property index. Must be a value between PROPERTY_REGISTRATION_START_INDEX and PROPERTY_REGISTRATION_MAX_INDEX inclusive.
299    * @param [in] type The property value type.
300    * @param [in] setFunc The function to call when setting the property. If NULL, then the property becomes read-only.
301    * @param [in] getFunc The function to call to retrieve the current value of the property. MUST be provided.
302    *
303    * @pre "registered" must be registered with the TypeRegistry.
304    * @note The "index" value must be between START_INDEX and MAX_INDEX inclusive.
305    * @note If "setFunc" is NULL, then the property becomes a read-only property.
306    * @note "getFunc" MUST be provided
307    *
308    */
309   PropertyRegistration( TypeRegistration& registered,
310                         const std::string& name, Property::Index index, Property::Type type,
311                         TypeInfo::SetPropertyFunction setFunc, TypeInfo::GetPropertyFunction getFunc );
312 };
313
314 /**
315  * @brief Register an animatable property for the given type.
316  * @SINCE_1_0.0
317  */
318 class DALI_IMPORT_API AnimatablePropertyRegistration
319 {
320 public:
321
322   /**
323    * @brief This constructor registers the animatable property with the registered type.
324    *
325    * This constructor is for scene-graph only properties where the
326    * value of the property can be retrieved and set via specified
327    * functions.
328    *
329    * @SINCE_1_0.0
330    * @param [in] registered The TypeRegistration object
331    * @param [in] name The name of the property
332    * @param [in] index The property index. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
333    * @param [in] type The property value type.
334    *
335    * @pre "registered" must be registered with the TypeRegistry.
336    */
337   AnimatablePropertyRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type );
338
339   /**
340    * @brief This constructor registers the animatable property with the registered default value.
341    *
342    * This constructor is for scene-graph only properties where the
343    * value of the property can be retrieved and set via specified
344    * functions.
345    *
346    * @SINCE_1_1.18
347    * @param [in] registered The TypeRegistration object
348    * @param [in] name The name of the property
349    * @param [in] index The property index. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
350    * @param [in] value The property default value.
351    *
352    * @pre "registered" must be registered with the TypeRegistry.
353    */
354   AnimatablePropertyRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, const Property::Value& value );
355 };
356
357 /**
358  * @brief Register a component of animatable property for the given component index.
359  * @SINCE_1_0.0
360  */
361 class DALI_IMPORT_API AnimatablePropertyComponentRegistration
362 {
363 public:
364
365   /**
366    * @brief This constructor registers a component of an animatable property where
367    * the base animatable property must be a property that supports property component
368    * (i.e. Vector2, Vector3 or Vector4) and the base animatable property must have
369    * been registered.
370    *
371    * This constructor is for a component of scene-graph only properties where the
372    * value of the property can be retrieved and set via specified functions.
373    *
374    * @SINCE_1_0.0
375    * @param [in] registered The TypeRegistration object
376    * @param [in] name The name of the component
377    * @param [in] index The property index. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
378    * @param [in] baseIndex The index of the base animatable property. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
379    * @param [in] componentIndex The index of the component (e.g. 0 for the x component of a Vector2 property and 1 for the y component of a Vector2 property).
380    *
381    * @pre "registered" must be registered with the TypeRegistry.
382    */
383   AnimatablePropertyComponentRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex );
384 };
385
386 /**
387  * @brief Register a child property for the given type.
388  * @SINCE_1_1.35
389  */
390 class DALI_IMPORT_API ChildPropertyRegistration
391 {
392 public:
393
394   /**
395    * @brief This constructor registers an event-thread only child property (i.e. a property
396    * that the parent supports in its children) with the registered type.
397    *
398    * @SINCE_1_1.35
399    * @param [in] registered The TypeRegistration object
400    * @param [in] name The name of the property
401    * @param [in] index The property index. Must be a value between CHILD_PROPERTY_REGISTRATION_START_INDEX and CHILD_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
402    * @param [in] type The property value type.
403    *
404    * @pre "registered" must be registered with the TypeRegistry.
405    */
406   ChildPropertyRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type );
407 };
408
409
410 /**
411  * @}
412  */
413 } // namespace Dali
414
415 #endif // header