2 * Copyright (c) 2018 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/internal/event/common/thread-local-storage.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/base-handle.h>
25 #include <dali/internal/event/actors/custom-actor-internal.h>
26 #include <dali/internal/event/common/demangler.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");
42 extern std::string Demangle(const char* symbol);
47 TypeRegistry *TypeRegistry::Get()
49 static TypeRegistry *_reg(new TypeRegistry());
50 DALI_ASSERT_DEBUG(_reg);
54 TypeRegistry::TypeRegistry()
59 TypeRegistry::~TypeRegistry()
64 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
66 for( auto&& iter : mRegistryLut )
68 // Note! mRegistryLut contains Dali::TypeInfo handles, so cannot call GetTypeName()
69 // as it calls us back resulting in infinite loop (GetTypeName is in BaseHandle part)
70 if( iter->GetName() == uniqueTypeName )
75 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
77 return TypeRegistry::TypeInfoPointer();
80 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::type_info& registerType )
82 std::string typeName = DemangleClassName( registerType.name() );
84 return GetTypeInfo( typeName );
87 uint32_t TypeRegistry::GetTypeNameCount() const
89 return static_cast<uint32_t>( mRegistryLut.size() );
92 std::string TypeRegistry::GetTypeName( uint32_t index ) const
96 if( index < mRegistryLut.size() )
98 name = mRegistryLut[ index ]->GetName();
104 std::string TypeRegistry::Register( const std::type_info& theTypeInfo, const std::type_info& baseTypeInfo,
105 Dali::TypeInfo::CreateFunction createInstance, 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, const std::type_info& baseTypeInfo,
113 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit,
114 const Dali::PropertyDetails* defaultProperties, Property::Index defaultPropertyCount )
116 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
118 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
121 std::string TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
122 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit,
123 const Dali::PropertyDetails* defaultProperties, Property::Index defaultPropertyCount )
125 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
127 // check for duplicates using uniqueTypeName
128 for( auto&& iter : mRegistryLut )
130 if( iter->GetName() == uniqueTypeName )
132 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
133 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
134 return uniqueTypeName; // never actually happening due to the assert
138 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount ) ) );
139 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
141 if( callCreateOnInit )
143 mInitFunctions.push_back(createInstance);
146 return uniqueTypeName;
149 void TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
150 Dali::CSharpTypeInfo::CreateFunction createInstance )
152 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
154 // check for duplicates using uniqueTypeName
155 for( auto&& iter : mRegistryLut )
157 if( iter->GetName() == uniqueTypeName )
159 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
160 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
161 return; // never actually happening due to the assert
165 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
166 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
169 void TypeRegistry::CallInitFunctions(void) const
171 for( auto&& iter : mInitFunctions )
177 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
179 return DemangleClassName( registerType.name() );
182 void TypeRegistry::RegisterSignal( TypeRegistration& typeRegistration, const std::string& name, Dali::TypeInfo::SignalConnectorFunction func )
184 for( auto&& iter : mRegistryLut )
186 if( iter->GetName() == typeRegistration.RegisteredName() )
188 iter->AddConnectorFunction( name, func );
194 bool TypeRegistry::RegisterAction( TypeRegistration& typeRegistration, const std::string &name, Dali::TypeInfo::ActionFunction f )
196 for( auto&& iter : mRegistryLut )
198 if( iter->GetName() == typeRegistration.RegisteredName() )
200 iter->AddActionFunction( name, f );
207 bool TypeRegistry::RegisterProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
209 for( auto&& iter : mRegistryLut )
211 if( iter->GetName() == typeRegistration.RegisteredName() )
213 iter->AddProperty( name, index, type, setFunc, getFunc );
221 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 )
223 for( auto&& iter : mRegistryLut )
225 if( iter->GetName() == objectName )
227 iter->AddProperty( name, index, type, setFunc, getFunc );
236 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
238 for( auto&& iter : mRegistryLut )
240 if( iter->GetName() == typeRegistration.RegisteredName() )
242 iter->AddAnimatableProperty( name, index, type );
250 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, const Property::Value& value )
252 for( auto&& iter : mRegistryLut )
254 if( iter->GetName() == typeRegistration.RegisteredName() )
256 iter->AddAnimatableProperty( name, index, value );
264 bool TypeRegistry::RegisterAnimatablePropertyComponent( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
266 for( auto&& iter : mRegistryLut )
268 if( iter->GetName() == typeRegistration.RegisteredName() )
270 iter->AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
278 bool TypeRegistry::RegisterChildProperty( const std::string& registeredType, const std::string& name, Property::Index index, Property::Type type )
280 for( auto&& iter : mRegistryLut )
282 if( iter->GetName() == registeredType )
284 iter->AddChildProperty( name, index, type );
292 bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
294 return RegisterChildProperty( typeRegistration.RegisteredName(), name, index, type );
297 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
301 auto&& type = GetTypeInfo( object );
303 // DoActionTo recurses through base classes
304 done = type->DoActionTo( object, actionName, properties );
308 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
314 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
316 bool connected( false );
318 auto&& type = GetTypeInfo( object );
320 // Connect iterates through base classes
321 connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
325 DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
326 // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
333 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
335 TypeInfoPointer type;
337 // test for custom actor which has another indirection to get to the type hiearchy we're after
338 const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
342 const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
343 type = GetTypeInfo( typeid( custom ) );
346 // the most derived type is a descendant of custom actor but has not registered itself
347 // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
348 type = GetTypeInfo( typeid( Dali::Internal::CustomActor ) );
353 type = GetTypeInfo( typeid( *pBaseObject ) );
359 } // namespace Internal