2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/common/type-registry-impl.h>
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>
28 #include <dali/integration-api/debug.h>
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TYPE_REGISTRY");
45 TypeRegistry *TypeRegistry::Get()
47 static TypeRegistry *_reg(new TypeRegistry());
48 DALI_ASSERT_DEBUG(_reg);
52 TypeRegistry::TypeRegistry()
57 TypeRegistry::~TypeRegistry()
62 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
64 for( auto&& iter : mRegistryLut )
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 )
73 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
75 return TypeRegistry::TypeInfoPointer();
78 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::type_info& registerType )
80 std::string typeName = DemangleClassName( registerType.name() );
82 return GetTypeInfo( typeName );
85 uint32_t TypeRegistry::GetTypeNameCount() const
87 return static_cast<uint32_t>( mRegistryLut.size() );
90 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
92 static std::string EMPTY_STRING{};
94 if( index < mRegistryLut.size() )
96 return mRegistryLut[index]->GetName();
102 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
103 const std::type_info& baseTypeInfo,
104 Dali::TypeInfo::CreateFunction createInstance,
105 bool callCreateOnInit )
107 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
109 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
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 )
119 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
121 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
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)
131 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
133 // check for duplicates using uniqueTypeName
134 for( auto&& iter : mRegistryLut )
136 if( iter->GetName() == uniqueTypeName )
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
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() );
148 if( callCreateOnInit )
150 mInitFunctions.push_back(createInstance);
153 return uniqueTypeName;
156 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
158 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
160 // check for duplicates using uniqueTypeName
161 for( auto&& iter : mRegistryLut )
163 if( iter->GetName() == uniqueTypeName )
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
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() );
175 void TypeRegistry::CallInitFunctions(void) const
177 for( auto&& iter : mInitFunctions )
183 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
185 return DemangleClassName( registerType.name() );
188 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
190 for( auto&& iter : mRegistryLut )
192 if( iter->GetName() == typeRegistration.RegisteredName() )
194 iter->AddConnectorFunction(std::move(name), func);
200 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
202 for( auto&& iter : mRegistryLut )
204 if( iter->GetName() == typeRegistration.RegisteredName() )
206 iter->AddActionFunction(std::move(name), f);
213 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
215 for( auto&& iter : mRegistryLut )
217 if( iter->GetName() == typeRegistration.RegisteredName() )
219 iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
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)
229 for( auto&& iter : mRegistryLut )
231 if( iter->GetName() == objectName )
233 iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
241 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
243 for( auto&& iter : mRegistryLut )
245 if( iter->GetName() == typeRegistration.RegisteredName() )
247 iter->AddAnimatableProperty(std::move(name), index, type);
255 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
257 for( auto&& iter : mRegistryLut )
259 if( iter->GetName() == typeRegistration.RegisteredName() )
261 iter->AddAnimatableProperty(std::move(name), index, std::move(value));
269 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
271 for( auto&& iter : mRegistryLut )
273 if( iter->GetName() == typeRegistration.RegisteredName() )
275 iter->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
283 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
285 for( auto&& iter : mRegistryLut )
287 if( iter->GetName() == registeredType )
289 iter->AddChildProperty(std::move(name), index, type);
297 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
299 return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
302 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
306 auto&& type = GetTypeInfo( object );
308 // DoActionTo recurses through base classes
309 done = type->DoActionTo( object, actionName, properties );
313 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
319 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
321 bool connected( false );
323 auto&& type = GetTypeInfo( object );
325 // Connect iterates through base classes
326 connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
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
338 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
340 TypeInfoPointer type;
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);
347 const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
348 type = GetTypeInfo( typeid( custom ) );
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 ) );
358 type = GetTypeInfo( typeid( *pBaseObject ) );
364 } // namespace Internal