Merge "Remove handle::operator=(NULL) as it is duplicating handle.Reset() functionali...
[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) 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
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 namespace Internal DALI_INTERNAL
33 {
34 class TypeRegistry;
35 }
36
37 /**
38  * @brief The TypeRegistry allows registration of type instance creation functions.
39  *
40  * These can then be created later by name and down cast to the appropriate type.
41  *
42  * Usage: (Registering)
43  *
44  * In my-actor.cpp
45  * @code
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.
49  * namespace
50  * {
51  *   TypeRegistration myActorType(typeid(MyActor), typeid(Actor), CreateMyActor );
52  *
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 );
57  * }
58  * @endcode
59  *
60  * Usage: (Creation)
61  *
62  * @code
63  *   TypeRegistry::TypeInfo type = TypeRegistry::Get().GetTypeInfo("MyActor");
64  *   MyActor a = MyActor::DownCast(type.CreateInstance());
65  *   if(a)
66  *   {
67  *     ...
68  *   }
69  * @endcode
70  *
71  * CustomActor
72  *
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.
76  *
77  *  As namespaces are discarded the convention is to use a namespace ie
78  *
79  *    'class MyActor {}; namespace Internal { class MyActor {}; }'
80  *
81  *  Warning: this arrangement will silently fail
82  *
83  *    'class MyActor {}; class MyActorImpl {};'
84  *
85  * Naming Conventions
86  *
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.
89  *
90  */
91 class DALI_IMPORT_API TypeRegistry : public BaseHandle
92 {
93 public:
94   typedef std::vector<std::string> NameContainer; ///< Container of type names
95
96   /**
97    * @brief Get Type Registry handle.
98    *
99    * @return TypeRegistry handle
100    */
101   static TypeRegistry Get();
102
103   /**
104    * @brief Allows the creation of an empty typeRegistry handle.
105    */
106   TypeRegistry();
107
108   /**
109    * @brief destructor.
110    */
111   ~TypeRegistry();
112
113   /**
114    * @brief This copy constructor is required for (smart) pointer semantics.
115    *
116    * @param [in] handle A reference to the copied handle
117    */
118   TypeRegistry(const TypeRegistry& handle);
119
120   /**
121    * @brief This assignment operator is required for (smart) pointer semantics.
122    *
123    * @param [in] rhs  A reference to the copied handle
124    * @return A reference to this
125    */
126   TypeRegistry& operator=(const TypeRegistry& rhs);
127
128   /**
129    * @brief Get TypeInfo for a registered type.
130    *
131    * @param [in] uniqueTypeName A unique type name
132    * @return TypeInfo if the type exists otherwise an empty handle
133    */
134   TypeInfo GetTypeInfo( const std::string &uniqueTypeName );
135
136   /**
137    * @brief Get TypeInfo for a registered type.
138    *
139    * @param [in] registerType The registered type info
140    * @return TypeInfo if the type exists otherwise an empty handle
141    */
142   TypeInfo GetTypeInfo( const std::type_info& registerType );
143
144   /**
145    * @brief Get type names.
146    *
147    * @return list of known types by name
148    */
149   NameContainer GetTypeNames() const;
150
151 public: // Not intended for application developers
152
153   /**
154    * @brief This constructor is used by Dali Get() method.
155    *
156    * @param [in] typeRegistry A pointer to a Dali resource
157    */
158   explicit DALI_INTERNAL TypeRegistry(Internal::TypeRegistry*typeRegistry);
159 };
160
161 /**
162  * @brief Register a type from type info.
163  */
164 class DALI_IMPORT_API TypeRegistration
165 {
166 public:
167   /**
168    * @brief Constructor registers the type creation function.
169    *
170    * @param [in] registerType the type info for the type to be registered
171    * @param [in] baseType the base type info of registerType
172    * @param [in] f registerType instance creation function
173    */
174   TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
175                     TypeInfo::CreateFunction f );
176
177   /**
178    * @brief Constructor registers the type creation function.
179    *
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
183    * @param [in] callCreateOnInit If true the creation function is called as part of Dali initialisation
184    */
185   TypeRegistration( const std::type_info& registerType, const std::type_info& baseType,
186                     TypeInfo::CreateFunction f, bool callCreateOnInit );
187
188   /**
189    * @brief Constructor registers the type creation function for a named class or type.
190    *
191    * This allows types to be created dynamically from script. The name must be
192    * unique for successful registration.
193    * @param [in] name the name of 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::string& name, const std::type_info& baseType,
198                     TypeInfo::CreateFunction f );
199
200   /**
201    * @brief The name the type is registered under (derived from type_info).
202    *
203    * @return the registered name or empty if unregistered
204    */
205   const std::string RegisteredName() const;
206
207 private:
208   TypeRegistry mReference; ///< Reference to the type registry
209   std::string mName;       ///< Name of the type
210 };
211
212 /**
213  * @brief Register a signal connector function to a registered type.
214  */
215 class DALI_IMPORT_API SignalConnectorType
216 {
217 public:
218   /**
219    * @brief Constructor registers the type creation function.
220    *
221    * @param [in] typeRegistration The TypeRegistration object
222    * @param [in] name The signal name
223    * @param [in] func The signal connector function
224    */
225   SignalConnectorType( TypeRegistration& typeRegistration, const std::string& name, TypeInfo::SignalConnectorFunctionV2 func );
226 };
227
228 /**
229  * @brief Register an action function.
230  */
231 class DALI_IMPORT_API TypeAction
232 {
233 public:
234   /**
235    * @brief Constructor registers the type creation function.
236    *
237    * @param [in] registered The TypeRegistration object
238    * @param [in] name The action name
239    * @param [in] f The action function
240    */
241   TypeAction( TypeRegistration &registered, const std::string &name, TypeInfo::ActionFunction f);
242 };
243
244 /**
245  * @brief Register a property for the given type.
246  */
247 class DALI_IMPORT_API PropertyRegistration
248 {
249 public:
250
251   /**
252    * @brief This constructor registers the property with the registered type.
253    *
254    * This constructor is for event-thread only properties where the
255    * value of the property can be retrieved and set via specified
256    * functions.
257    *
258    * Functions of the following type may be used for setFunc and getFunc respectively:
259    * @code
260    *   void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
261    *   Property::Value GetProperty( BaseObject* object, Property::Index index );
262    * @endcode
263    *
264    * @param [in] registered The TypeRegistration object
265    * @param [in] name The name of the property
266    * @param [in] index The property index. Must be a value between PROPERTY_REGISTRATION_START_INDEX and PROPERTY_REGISTRATION_MAX_INDEX inclusive.
267    * @param [in] type The property value type.
268    * @param [in] setFunc The function to call when setting the property. If NULL, then the property becomes read-only.
269    * @param [in] getFunc The function to call to retrieve the current value of the property. MUST be provided.
270    *
271    * @note The "index" value must be between START_INDEX and MAX_INDEX inclusive.
272    * @note If "setFunc" is NULL, then the property becomes a read-only property.
273    * @note "getFunc" MUST be provided
274    *
275    * @pre "registered" must be registered with the TypeRegistry.
276    */
277   PropertyRegistration( TypeRegistration& registered,
278                         const std::string& name, Property::Index index, Property::Type type,
279                         TypeInfo::SetPropertyFunction setFunc, TypeInfo::GetPropertyFunction getFunc );
280 };
281
282 } // namespace Dali
283
284 #endif // header