1 #ifndef __DALI_TYPE_REGISTRY_H__
2 #define __DALI_TYPE_REGISTRY_H__
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <dali/public-api/object/base-handle.h>
27 #include <dali/public-api/object/type-info.h>
32 * @addtogroup dali_core_object
36 namespace Internal DALI_INTERNAL
42 * @brief The TypeRegistry allows registration of type instance creation functions.
44 * These can then be created later by name and down cast to the appropriate type.
46 * Usage: (Registering)
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.
55 * TypeRegistration myActorType(typeid(MyActor), typeid(Actor), CreateMyActor );
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 );
67 * TypeRegistry::TypeInfo type = TypeRegistry::Get().GetTypeInfo("MyActor");
68 * MyActor a = MyActor::DownCast(type.CreateInstance());
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.
81 * As namespaces are discarded the convention is to use a namespace ie
83 * 'class MyActor {}; namespace Internal { class MyActor {}; }'
85 * Warning: this arrangement will silently fail
87 * 'class MyActor {}; class MyActorImpl {};'
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.
96 class DALI_IMPORT_API TypeRegistry : public BaseHandle
100 * @brief Get Type Registry handle.
103 * @return TypeRegistry handle
105 static TypeRegistry Get();
108 * @brief Allows the creation of an empty typeRegistry handle.
120 * @brief This copy constructor is required for (smart) pointer semantics.
123 * @param [in] handle A reference to the copied handle
125 TypeRegistry(const TypeRegistry& handle);
128 * @brief This assignment operator is required for (smart) pointer semantics.
131 * @param [in] rhs A reference to the copied handle
132 * @return A reference to this
134 TypeRegistry& operator=(const TypeRegistry& rhs);
137 * @brief Get TypeInfo for a registered type.
140 * @param [in] uniqueTypeName A unique type name
141 * @return TypeInfo if the type exists otherwise an empty handle
143 TypeInfo GetTypeInfo( const std::string &uniqueTypeName );
146 * @brief Get TypeInfo for a registered type.
149 * @param [in] registerType The registered type info
150 * @return TypeInfo if the type exists otherwise an empty handle
152 TypeInfo GetTypeInfo( const std::type_info& registerType );
155 * @brief Get type name count.
160 size_t GetTypeNameCount() const;
163 * @brief Get type names by index.
166 * @return The type name or an empty string when index is not valid
168 std::string GetTypeName(size_t index) const;
170 public: // Not intended for application developers
173 * @brief This constructor is used by Dali Get() method.
176 * @param [in] typeRegistry A pointer to a Dali resource
178 explicit DALI_INTERNAL TypeRegistry(Internal::TypeRegistry*typeRegistry);
182 * @brief Register a type from type info.
185 class DALI_IMPORT_API TypeRegistration
189 * @brief Constructor registers the type creation function.
192 * @param [in] registerType the type info for the type to be registered
193 * @param [in] baseType the base type info of registerType
194 * @param [in] f registerType instance creation function
196 TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
197 TypeInfo::CreateFunction f );
200 * @brief Constructor registers the type creation function.
203 * @param [in] registerType the type info for the type to be registered
204 * @param [in] baseType the base type info of registerType
205 * @param [in] f registerType instance creation function
206 * @param [in] callCreateOnInit If true the creation function is called as part of Dali initialisation
208 TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
209 TypeInfo::CreateFunction f, bool callCreateOnInit );
212 * @brief Constructor registers the type creation function for a named class or type.
214 * This allows types to be created dynamically from script. The name must be
215 * unique for successful registration.
217 * @param [in] name the name of the type to be registered
218 * @param [in] baseType the base type info of registerType
219 * @param [in] f registerType instance creation function
221 TypeRegistration( const std::string& name, const std::type_info& baseType,
222 TypeInfo::CreateFunction f );
225 * @brief The name the type is registered under (derived from type_info).
228 * @return the registered name or empty if unregistered
230 const std::string RegisteredName() const;
233 TypeRegistry mReference; ///< Reference to the type registry
234 std::string mName; ///< Name of the type
238 * @brief Register a signal connector function to a registered type.
241 class DALI_IMPORT_API SignalConnectorType
245 * @brief Constructor registers the type creation function.
248 * @param [in] typeRegistration The TypeRegistration object
249 * @param [in] name The signal name
250 * @param [in] func The signal connector function
252 SignalConnectorType( TypeRegistration& typeRegistration, const std::string& name, TypeInfo::SignalConnectorFunction func );
256 * @brief Register an action function.
259 class DALI_IMPORT_API TypeAction
263 * @brief Constructor registers the type creation function.
266 * @param [in] registered The TypeRegistration object
267 * @param [in] name The action name
268 * @param [in] f The action function
270 TypeAction( TypeRegistration ®istered, const std::string &name, TypeInfo::ActionFunction f);
274 * @brief Register a property for the given type.
277 class DALI_IMPORT_API PropertyRegistration
282 * @brief This constructor registers the property with the registered type.
284 * This constructor is for event-thread only properties where the
285 * value of the property can be retrieved and set via specified
288 * Functions of the following type may be used for setFunc and getFunc respectively:
290 * void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
291 * Property::Value GetProperty( BaseObject* object, Property::Index index );
295 * @param [in] registered The TypeRegistration object
296 * @param [in] name The name of the property
297 * @param [in] index The property index. Must be a value between PROPERTY_REGISTRATION_START_INDEX and PROPERTY_REGISTRATION_MAX_INDEX inclusive.
298 * @param [in] type The property value type.
299 * @param [in] setFunc The function to call when setting the property. If NULL, then the property becomes read-only.
300 * @param [in] getFunc The function to call to retrieve the current value of the property. MUST be provided.
302 * @pre "registered" must be registered with the TypeRegistry.
303 * @note The "index" value must be between START_INDEX and MAX_INDEX inclusive.
304 * @note If "setFunc" is NULL, then the property becomes a read-only property.
305 * @note "getFunc" MUST be provided
308 PropertyRegistration( TypeRegistration& registered,
309 const std::string& name, Property::Index index, Property::Type type,
310 TypeInfo::SetPropertyFunction setFunc, TypeInfo::GetPropertyFunction getFunc );
314 * @brief Register an animatable property for the given type.
317 class DALI_IMPORT_API AnimatablePropertyRegistration
322 * @brief This constructor registers the animatable property with the registered type.
324 * This constructor is for scene-graph only properties where the
325 * value of the property can be retrieved and set via specified
329 * @param [in] registered The TypeRegistration object
330 * @param [in] name The name of the property
331 * @param [in] index The property index. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
332 * @param [in] type The property value type.
334 * @pre "registered" must be registered with the TypeRegistry.
336 AnimatablePropertyRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type );
339 * @brief This constructor registers the animatable property with the registered default value.
341 * This constructor is for scene-graph only properties where the
342 * value of the property can be retrieved and set via specified
346 * @param [in] registered The TypeRegistration object
347 * @param [in] name The name of the property
348 * @param [in] index The property index. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
349 * @param [in] value The property default value.
351 * @pre "registered" must be registered with the TypeRegistry.
353 AnimatablePropertyRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, const Property::Value& value );
357 * @brief Register a component of animatable property for the given component index.
360 class DALI_IMPORT_API AnimatablePropertyComponentRegistration
365 * @brief This constructor registers a component of an animatable property where
366 * the base animatable property must be a property that supports property component
367 * (i.e. Vector2, Vector3 or Vector4) and the base animatable property must have
370 * This constructor is for a component of scene-graph only properties where the
371 * value of the property can be retrieved and set via specified functions.
374 * @param [in] registered The TypeRegistration object
375 * @param [in] name The name of the component
376 * @param [in] index The property index. Must be a value between ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX and ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
377 * @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.
378 * @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 * @pre "registered" must be registered with the TypeRegistry.
382 AnimatablePropertyComponentRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex );
386 * @brief Register a child property for the given type.
389 class DALI_IMPORT_API ChildPropertyRegistration
394 * @brief This constructor registers an event-thread only child property (i.e. a property
395 * that the parent supports in its children) with the registered type.
398 * @param [in] registered The TypeRegistration object
399 * @param [in] name The name of the property
400 * @param [in] index The property index. Must be a value between CHILD_PROPERTY_REGISTRATION_START_INDEX and CHILD_PROPERTY_REGISTRATION_MAX_INDEX inclusive.
401 * @param [in] type The property value type.
403 * @pre "registered" must be registered with the TypeRegistry.
405 ChildPropertyRegistration( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type );