Clean up the code to build successfully on macOS
[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()
53 {
54
55 }
56
57 TypeRegistry::~TypeRegistry()
58 {
59   mRegistryLut.clear();
60 }
61
62 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
63 {
64   for( auto&& iter : mRegistryLut )
65   {
66     // Note! mRegistryLut contains Dali::TypeInfo handles, so cannot call GetTypeName()
67     // as it calls us back resulting in infinite loop (GetTypeName is in BaseHandle part)
68     if( iter->GetName() == uniqueTypeName )
69     {
70       return iter;
71     }
72   }
73   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
74
75   return TypeRegistry::TypeInfoPointer();
76 }
77
78 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::type_info& registerType )
79 {
80   std::string typeName = DemangleClassName( registerType.name() );
81
82   return GetTypeInfo( typeName );
83 }
84
85 uint32_t TypeRegistry::GetTypeNameCount() const
86 {
87   return static_cast<uint32_t>( mRegistryLut.size() );
88 }
89
90 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
91 {
92   static std::string EMPTY_STRING{};
93
94   if( index < mRegistryLut.size() )
95   {
96     return mRegistryLut[index]->GetName();
97   }
98
99   return EMPTY_STRING;
100 }
101
102 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
103                                     const std::type_info& baseTypeInfo,
104                                     Dali::TypeInfo::CreateFunction createInstance,
105                                     bool callCreateOnInit )
106 {
107   std::string uniqueTypeName  = DemangleClassName( theTypeInfo.name() );
108
109   return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
110 }
111
112 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
113                                     const std::type_info& baseTypeInfo,
114                                     Dali::TypeInfo::CreateFunction createInstance,
115                                     bool callCreateOnInit,
116                                     const Dali::PropertyDetails* defaultProperties,
117                                     Property::Index defaultPropertyCount )
118 {
119   std::string uniqueTypeName  = DemangleClassName( theTypeInfo.name() );
120
121   return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
122 }
123
124 std::string TypeRegistry::Register(std::string                    uniqueTypeName,
125                                    const std::type_info&          baseTypeInfo,
126                                    Dali::TypeInfo::CreateFunction createInstance,
127                                    bool                           callCreateOnInit,
128                                    const Dali::PropertyDetails*   defaultProperties,
129                                    Property::Index                defaultPropertyCount)
130 {
131   std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
132
133   // check for duplicates using uniqueTypeName
134   for( auto&& iter : mRegistryLut )
135   {
136     if( iter->GetName() == uniqueTypeName )
137     {
138       DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
139       DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
140       return uniqueTypeName; // never actually happening due to the assert
141     }
142   }
143
144   mRegistryLut.push_back( TypeRegistry::TypeInfoPointer(
145                             new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount ) ) );
146   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
147
148   if( callCreateOnInit )
149   {
150     mInitFunctions.push_back(createInstance);
151   }
152
153   return uniqueTypeName;
154 }
155
156 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
157 {
158   std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
159
160   // check for duplicates using uniqueTypeName
161   for( auto&& iter : mRegistryLut )
162   {
163     if( iter->GetName() == uniqueTypeName )
164     {
165       DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
166       DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
167       return; // never actually happening due to the assert
168     }
169   }
170
171   mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
172   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
173 }
174
175 void TypeRegistry::CallInitFunctions(void) const
176 {
177   for( auto&& iter : mInitFunctions )
178   {
179     (*iter)();
180   }
181 }
182
183 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
184 {
185   return DemangleClassName( registerType.name() );
186 }
187
188 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
189 {
190   for( auto&& iter : mRegistryLut )
191   {
192     if( iter->GetName() == typeRegistration.RegisteredName() )
193     {
194       iter->AddConnectorFunction(std::move(name), func);
195       break;
196     }
197   }
198 }
199
200 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
201 {
202   for( auto&& iter : mRegistryLut )
203   {
204     if( iter->GetName() == typeRegistration.RegisteredName() )
205     {
206       iter->AddActionFunction(std::move(name), f);
207       return true;
208     }
209   }
210   return false;
211 }
212
213 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
214 {
215   for( auto&& iter : mRegistryLut )
216   {
217     if( iter->GetName() == typeRegistration.RegisteredName() )
218     {
219       iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
220       return true;
221     }
222   }
223
224   return false;
225 }
226
227 bool TypeRegistry::RegisterProperty(const std::string& objectName, std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
228 {
229   for( auto&& iter : mRegistryLut )
230   {
231     if( iter->GetName() == objectName )
232     {
233       iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
234       return true;
235     }
236   }
237
238   return false;
239 }
240
241 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
242 {
243   for( auto&& iter : mRegistryLut )
244   {
245     if( iter->GetName() == typeRegistration.RegisteredName() )
246     {
247       iter->AddAnimatableProperty(std::move(name), index, type);
248       return true;
249     }
250   }
251
252   return false;
253 }
254
255 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
256 {
257   for( auto&& iter : mRegistryLut )
258   {
259     if( iter->GetName() == typeRegistration.RegisteredName() )
260     {
261       iter->AddAnimatableProperty(std::move(name), index, std::move(value));
262       return true;
263     }
264   }
265
266   return false;
267 }
268
269 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
270 {
271   for( auto&& iter : mRegistryLut )
272   {
273     if( iter->GetName() == typeRegistration.RegisteredName() )
274     {
275       iter->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
276       return true;
277     }
278   }
279
280   return false;
281 }
282
283 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
284 {
285   for( auto&& iter : mRegistryLut )
286   {
287     if( iter->GetName() == registeredType )
288     {
289       iter->AddChildProperty(std::move(name), index, type);
290       return true;
291     }
292   }
293
294   return false;
295 }
296
297 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
298 {
299   return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
300 }
301
302 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
303 {
304   bool done = false;
305
306   auto&& type = GetTypeInfo( object );
307
308   // DoActionTo recurses through base classes
309   done = type->DoActionTo( object, actionName, properties );
310
311   if( !done )
312   {
313     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
314   }
315
316   return done;
317 }
318
319 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
320 {
321   bool connected( false );
322
323   auto&& type = GetTypeInfo( object );
324
325   // Connect iterates through base classes
326   connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
327
328   if( !connected )
329   {
330     DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
331     // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
332     delete functor;
333   }
334
335   return connected;
336 }
337
338 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
339 {
340   TypeInfoPointer type;
341
342   // test for custom actor which has another indirection to get to the type hiearchy we're after
343   const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
344
345   if( pCustom )
346   {
347     const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
348     type = GetTypeInfo( typeid( custom ) );
349     if( !type )
350     {
351       // the most derived type is a descendant of custom actor but has not registered itself
352       // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
353       type = GetTypeInfo( typeid( Dali::Internal::CustomActor ) );
354     }
355   }
356   else
357   {
358     type = GetTypeInfo( typeid( *pBaseObject ) );
359   }
360
361   return type;
362 }
363
364 } // namespace Internal
365
366 } // namespace Dali