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