1 #ifndef __DALI_TYPE_REGISTRY_H__
2 #define __DALI_TYPE_REGISTRY_H__
5 * Copyright (c) 2014 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 namespace Internal DALI_INTERNAL
38 * @brief The TypeRegistry allows registration of type instance creation functions.
40 * These can then be created later by name and down cast to the appropriate type.
42 * Usage: (Registering)
46 * // Note: object construction in namespace scope is defined in a translation unit as being
47 * // in appearance order (C++ standard 3.6/2). So TypeRegistration is declared first in
48 * // the cpp file below. Signal, action and property declarations follow in any order.
51 * TypeRegistration myActorType(typeid(MyActor), typeid(Actor), CreateMyActor );
53 * SignalConnectorType( myActorType, "highlighted", ConnectSignalForMyActor );
54 * TypeAction( myActorType, "open", DoMyActorAction );
55 * TypeAction( myActorType, "close", DoMyActorAction );
56 * PropertyRegistration( myActorType, "status", PropertyRegistration::START_INDEX, Property::BOOLEAN, SetPropertyFunction, GetPropertyFunction );
63 * TypeRegistry::TypeInfo type = TypeRegistry::Get().GetTypeInfo("MyActor");
64 * MyActor a = MyActor::DownCast(type.CreateInstance());
73 * Actors that inherit from the CustomActor framework must ensure the implementation
74 * class has an identical name to the Actor class. This is to ensure the class can be
75 * found at runtime for signals and actions. Otherwise these will silently fail.
77 * As namespaces are discarded the convention is to use a namespace ie
79 * 'class MyActor {}; namespace Internal { class MyActor {}; }'
81 * Warning: this arrangement will silently fail
83 * 'class MyActor {}; class MyActorImpl {};'
87 * Signal and action names follow properties and are by convention lower case hyphen
88 * separated ie 'next-page'. This maintains consistency with the scripted interface.
91 class DALI_IMPORT_API TypeRegistry : public BaseHandle
94 typedef std::vector<std::string> NameContainer; ///< Container of type names
97 * @brief Get Type Registry handle.
99 * @return TypeRegistry handle
101 static TypeRegistry Get();
104 * @brief Allows the creation of an empty typeRegistry handle.
114 * @brief This copy constructor is required for (smart) pointer semantics.
116 * @param [in] handle A reference to the copied handle
118 TypeRegistry(const TypeRegistry& handle);
121 * @brief This assignment operator is required for (smart) pointer semantics.
123 * @param [in] rhs A reference to the copied handle
124 * @return A reference to this
126 TypeRegistry& operator=(const TypeRegistry& rhs);
129 * @brief This method is defined to allow assignment of the NULL value,
130 * and will throw an exception if passed any other value.
132 * Assigning to NULL is an alias for Reset().
133 * @param [in] rhs A NULL pointer
134 * @return A reference to this handle
136 TypeRegistry& operator=(BaseHandle::NullType* rhs);
139 * @brief Get TypeInfo for a registered type.
141 * @param [in] uniqueTypeName A unique type name
142 * @return TypeInfo if the type exists otherwise an empty handle
144 TypeInfo GetTypeInfo( const std::string &uniqueTypeName );
147 * @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 names.
157 * @return list of known types by name
159 NameContainer GetTypeNames() const;
161 public: // Not intended for application developers
164 * @brief This constructor is used by Dali Get() method.
166 * @param [in] typeRegistry A pointer to a Dali resource
168 explicit DALI_INTERNAL TypeRegistry(Internal::TypeRegistry*typeRegistry);
172 * @brief Register a type from type info.
174 class DALI_IMPORT_API TypeRegistration
178 * @brief Constructor registers the type creation function.
180 * @param [in] registerType the type info for the type to be registered
181 * @param [in] baseType the base type info of registerType
182 * @param [in] f registerType instance creation function
184 TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
185 TypeInfo::CreateFunction f );
188 * @brief Constructor registers the type creation function.
190 * @param [in] registerType the type info for the type to be registered
191 * @param [in] baseType the base type info of registerType
192 * @param [in] f registerType instance creation function
193 * @param [in] callCreateOnInit If true the creation function is called as part of Dali initialisation
195 TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
196 TypeInfo::CreateFunction f, bool callCreateOnInit );
199 * @brief Constructor registers the type creation function for a named class or type.
201 * This allows types to be created dynamically from script. The name must be
202 * unique for successful registration.
203 * @param [in] name the name of the type to be registered
204 * @param [in] baseType the base type info of registerType
205 * @param [in] f registerType instance creation function
207 TypeRegistration( const std::string& name, const std::type_info& baseType,
208 TypeInfo::CreateFunction f );
211 * @brief The name the type is registered under (derived from type_info).
213 * @return the registered name or empty if unregistered
215 const std::string RegisteredName() const;
218 TypeRegistry mReference; ///< Reference to the type registry
219 std::string mName; ///< Name of the type
223 * @brief Register a signal connector function to a registered type.
225 class DALI_IMPORT_API SignalConnectorType
229 * @brief Constructor registers the type creation function.
231 * @param [in] typeRegistration The TypeRegistration object
232 * @param [in] name The signal name
233 * @param [in] func The signal connector function
235 SignalConnectorType( TypeRegistration& typeRegistration, const std::string& name, TypeInfo::SignalConnectorFunctionV2 func );
239 * @brief Register an action function.
241 class DALI_IMPORT_API TypeAction
245 * @brief Constructor registers the type creation function.
247 * @param [in] registered The TypeRegistration object
248 * @param [in] name The action name
249 * @param [in] f The action function
251 TypeAction( TypeRegistration ®istered, const std::string &name, TypeInfo::ActionFunction f);
255 * @brief Register a property for the given type.
257 class DALI_IMPORT_API PropertyRegistration
262 * @brief This constructor registers the property with the registered type.
264 * This constructor is for event-thread only properties where the
265 * value of the property can be retrieved and set via specified
268 * Functions of the following type may be used for setFunc and getFunc respectively:
270 * void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
271 * Property::Value GetProperty( BaseObject* object, Property::Index index );
274 * @param [in] registered The TypeRegistration object
275 * @param [in] name The name of the property
276 * @param [in] index The property index. Must be a value between PROPERTY_REGISTRATION_START_INDEX and PROPERTY_REGISTRATION_MAX_INDEX inclusive.
277 * @param [in] type The property value type.
278 * @param [in] setFunc The function to call when setting the property. If NULL, then the property becomes read-only.
279 * @param [in] getFunc The function to call to retrieve the current value of the property. MUST be provided.
281 * @note The "index" value must be between START_INDEX and MAX_INDEX inclusive.
282 * @note If "setFunc" is NULL, then the property becomes a read-only property.
283 * @note "getFunc" MUST be provided
285 * @pre "registered" must be registered with the TypeRegistry.
287 PropertyRegistration( TypeRegistration& registered,
288 const std::string& name, Property::Index index, Property::Type type,
289 TypeInfo::SetPropertyFunction setFunc, TypeInfo::GetPropertyFunction getFunc );