Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-registry-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/type-registry-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/base-handle.h>
24 #include <dali/internal/event/actors/custom-actor-internal.h>
25 #include <dali/internal/event/common/demangler.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27
28 #include <dali/integration-api/debug.h>
29
30 namespace
31 {
32
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TYPE_REGISTRY");
35 #endif
36
37 } // namespace anon
38
39 namespace Dali
40 {
41
42 namespace Internal
43 {
44
45 TypeRegistry *TypeRegistry::Get()
46 {
47   static TypeRegistry *_reg(new TypeRegistry());
48   DALI_ASSERT_DEBUG(_reg);
49   return _reg;
50 }
51
52 TypeRegistry::TypeRegistry() = default;
53
54 TypeRegistry::~TypeRegistry()
55 {
56   mRegistryLut.clear();
57 }
58
59 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
60 {
61   for( auto&& iter : mRegistryLut )
62   {
63     // Note! mRegistryLut contains Dali::TypeInfo handles, so cannot call GetTypeName()
64     // as it calls us back resulting in infinite loop (GetTypeName is in BaseHandle part)
65     if( iter->GetName() == uniqueTypeName )
66     {
67       return iter;
68     }
69   }
70   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
71
72   return TypeRegistry::TypeInfoPointer();
73 }
74
75 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::type_info& registerType )
76 {
77   std::string typeName = DemangleClassName( registerType.name() );
78
79   return GetTypeInfo( typeName );
80 }
81
82 uint32_t TypeRegistry::GetTypeNameCount() const
83 {
84   return static_cast<uint32_t>( mRegistryLut.size() );
85 }
86
87 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
88 {
89   static std::string EMPTY_STRING{};
90
91   if( index < mRegistryLut.size() )
92   {
93     return mRegistryLut[index]->GetName();
94   }
95
96   return EMPTY_STRING;
97 }
98
99 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
100                                     const std::type_info& baseTypeInfo,
101                                     Dali::TypeInfo::CreateFunction createInstance,
102                                     bool callCreateOnInit )
103 {
104   std::string uniqueTypeName  = DemangleClassName( theTypeInfo.name() );
105
106   return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
107 }
108
109 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
110                                     const std::type_info& baseTypeInfo,
111                                     Dali::TypeInfo::CreateFunction createInstance,
112                                     bool callCreateOnInit,
113                                     const Dali::PropertyDetails* defaultProperties,
114                                     Property::Index defaultPropertyCount )
115 {
116   std::string uniqueTypeName  = DemangleClassName( theTypeInfo.name() );
117
118   return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
119 }
120
121 std::string TypeRegistry::Register(std::string                    uniqueTypeName,
122                                    const std::type_info&          baseTypeInfo,
123                                    Dali::TypeInfo::CreateFunction createInstance,
124                                    bool                           callCreateOnInit,
125                                    const Dali::PropertyDetails*   defaultProperties,
126                                    Property::Index                defaultPropertyCount)
127 {
128   std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
129
130   // check for duplicates using uniqueTypeName
131   for( auto&& iter : mRegistryLut )
132   {
133     if( iter->GetName() == uniqueTypeName )
134     {
135       DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
136       DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
137       return uniqueTypeName; // never actually happening due to the assert
138     }
139   }
140
141   mRegistryLut.push_back( TypeRegistry::TypeInfoPointer(
142                             new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount ) ) );
143   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
144
145   if( callCreateOnInit )
146   {
147     mInitFunctions.push_back(createInstance);
148   }
149
150   return uniqueTypeName;
151 }
152
153 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
154 {
155   std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
156
157   // check for duplicates using uniqueTypeName
158   for( auto&& iter : mRegistryLut )
159   {
160     if( iter->GetName() == uniqueTypeName )
161     {
162       DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
163       DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
164       return; // never actually happening due to the assert
165     }
166   }
167
168   mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
169   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
170 }
171
172 void TypeRegistry::CallInitFunctions(void) const
173 {
174   for( auto&& iter : mInitFunctions )
175   {
176     (*iter)();
177   }
178 }
179
180 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
181 {
182   return DemangleClassName( registerType.name() );
183 }
184
185 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
186 {
187   for( auto&& iter : mRegistryLut )
188   {
189     if( iter->GetName() == typeRegistration.RegisteredName() )
190     {
191       iter->AddConnectorFunction(std::move(name), func);
192       break;
193     }
194   }
195 }
196
197 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
198 {
199   for( auto&& iter : mRegistryLut )
200   {
201     if( iter->GetName() == typeRegistration.RegisteredName() )
202     {
203       iter->AddActionFunction(std::move(name), f);
204       return true;
205     }
206   }
207   return false;
208 }
209
210 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
211 {
212   for( auto&& iter : mRegistryLut )
213   {
214     if( iter->GetName() == typeRegistration.RegisteredName() )
215     {
216       iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
217       return true;
218     }
219   }
220
221   return false;
222 }
223
224 bool TypeRegistry::RegisterProperty(const std::string& objectName, std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
225 {
226   for( auto&& iter : mRegistryLut )
227   {
228     if( iter->GetName() == objectName )
229     {
230       iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
231       return true;
232     }
233   }
234
235   return false;
236 }
237
238 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
239 {
240   for( auto&& iter : mRegistryLut )
241   {
242     if( iter->GetName() == typeRegistration.RegisteredName() )
243     {
244       iter->AddAnimatableProperty(std::move(name), index, type);
245       return true;
246     }
247   }
248
249   return false;
250 }
251
252 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
253 {
254   for( auto&& iter : mRegistryLut )
255   {
256     if( iter->GetName() == typeRegistration.RegisteredName() )
257     {
258       iter->AddAnimatableProperty(std::move(name), index, std::move(value));
259       return true;
260     }
261   }
262
263   return false;
264 }
265
266 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
267 {
268   for( auto&& iter : mRegistryLut )
269   {
270     if( iter->GetName() == typeRegistration.RegisteredName() )
271     {
272       iter->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
273       return true;
274     }
275   }
276
277   return false;
278 }
279
280 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
281 {
282   for( auto&& iter : mRegistryLut )
283   {
284     if( iter->GetName() == registeredType )
285     {
286       iter->AddChildProperty(std::move(name), index, type);
287       return true;
288     }
289   }
290
291   return false;
292 }
293
294 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
295 {
296   return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
297 }
298
299 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
300 {
301   bool done = false;
302
303   auto&& type = GetTypeInfo( object );
304
305   // DoActionTo recurses through base classes
306   done = type->DoActionTo( object, actionName, properties );
307
308   if( !done )
309   {
310     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
311   }
312
313   return done;
314 }
315
316 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
317 {
318   bool connected( false );
319
320   auto&& type = GetTypeInfo( object );
321
322   // Connect iterates through base classes
323   connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
324
325   if( !connected )
326   {
327     DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
328     // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
329     delete functor;
330   }
331
332   return connected;
333 }
334
335 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
336 {
337   TypeInfoPointer type;
338
339   // test for custom actor which has another indirection to get to the type hiearchy we're after
340   const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
341
342   if( pCustom )
343   {
344     const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
345     type = GetTypeInfo( typeid( custom ) );
346     if( !type )
347     {
348       // the most derived type is a descendant of custom actor but has not registered itself
349       // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
350       type = GetTypeInfo( typeid( Dali::Internal::CustomActor ) );
351     }
352   }
353   else
354   {
355     type = GetTypeInfo( typeid( *pBaseObject ) );
356   }
357
358   return type;
359 }
360
361 } // namespace Internal
362
363 } // namespace Dali