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 std::string TypeRegistry::GetTypeName( uint32_t index ) const
94 if( index < mRegistryLut.size() )
96 name = 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( const 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( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
157 Dali::CSharpTypeInfo::CreateFunction createInstance )
159 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
161 // check for duplicates using uniqueTypeName
162 for( auto&& iter : mRegistryLut )
164 if( iter->GetName() == uniqueTypeName )
166 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
167 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
168 return; // never actually happening due to the assert
172 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
173 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
176 void TypeRegistry::CallInitFunctions(void) const
178 for( auto&& iter : mInitFunctions )
184 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
186 return DemangleClassName( registerType.name() );
189 void TypeRegistry::RegisterSignal( TypeRegistration& typeRegistration, const std::string& name, Dali::TypeInfo::SignalConnectorFunction func )
191 for( auto&& iter : mRegistryLut )
193 if( iter->GetName() == typeRegistration.RegisteredName() )
195 iter->AddConnectorFunction( name, func );
201 bool TypeRegistry::RegisterAction( TypeRegistration& typeRegistration, const std::string &name, Dali::TypeInfo::ActionFunction f )
203 for( auto&& iter : mRegistryLut )
205 if( iter->GetName() == typeRegistration.RegisteredName() )
207 iter->AddActionFunction( name, f );
214 bool TypeRegistry::RegisterProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
216 for( auto&& iter : mRegistryLut )
218 if( iter->GetName() == typeRegistration.RegisteredName() )
220 iter->AddProperty( name, index, type, setFunc, getFunc );
228 bool TypeRegistry::RegisterProperty( const std::string& objectName, const std::string& name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc )
230 for( auto&& iter : mRegistryLut )
232 if( iter->GetName() == objectName )
234 iter->AddProperty( name, index, type, setFunc, getFunc );
243 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
245 for( auto&& iter : mRegistryLut )
247 if( iter->GetName() == typeRegistration.RegisteredName() )
249 iter->AddAnimatableProperty( name, index, type );
257 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, const Property::Value& value )
259 for( auto&& iter : mRegistryLut )
261 if( iter->GetName() == typeRegistration.RegisteredName() )
263 iter->AddAnimatableProperty( name, index, value );
271 bool TypeRegistry::RegisterAnimatablePropertyComponent( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
273 for( auto&& iter : mRegistryLut )
275 if( iter->GetName() == typeRegistration.RegisteredName() )
277 iter->AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
285 bool TypeRegistry::RegisterChildProperty( const std::string& registeredType, const std::string& name, Property::Index index, Property::Type type )
287 for( auto&& iter : mRegistryLut )
289 if( iter->GetName() == registeredType )
291 iter->AddChildProperty( name, index, type );
299 bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
301 return RegisterChildProperty( typeRegistration.RegisteredName(), name, index, type );
304 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
308 auto&& type = GetTypeInfo( object );
310 // DoActionTo recurses through base classes
311 done = type->DoActionTo( object, actionName, properties );
315 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
321 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
323 bool connected( false );
325 auto&& type = GetTypeInfo( object );
327 // Connect iterates through base classes
328 connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
332 DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
333 // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
340 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
342 TypeInfoPointer type;
344 // test for custom actor which has another indirection to get to the type hiearchy we're after
345 const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
349 const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
350 type = GetTypeInfo( typeid( custom ) );
353 // the most derived type is a descendant of custom actor but has not registered itself
354 // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
355 type = GetTypeInfo( typeid( Dali::Internal::CustomActor ) );
360 type = GetTypeInfo( typeid( *pBaseObject ) );
366 } // namespace Internal