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/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, const std::type_info& baseTypeInfo,
103 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit )
105 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
107 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
110 std::string TypeRegistry::Register( const std::type_info& theTypeInfo, const std::type_info& baseTypeInfo,
111 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit,
112 const Dali::PropertyDetails* defaultProperties, Property::Index defaultPropertyCount )
114 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
116 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
119 std::string TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
120 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit,
121 const Dali::PropertyDetails* defaultProperties, Property::Index defaultPropertyCount )
123 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
125 // check for duplicates using uniqueTypeName
126 for( auto&& iter : mRegistryLut )
128 if( iter->GetName() == uniqueTypeName )
130 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
131 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
132 return uniqueTypeName; // never actually happening due to the assert
136 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount ) ) );
137 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
139 if( callCreateOnInit )
141 mInitFunctions.push_back(createInstance);
144 return uniqueTypeName;
147 void TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
148 Dali::CSharpTypeInfo::CreateFunction createInstance )
150 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
152 // check for duplicates using uniqueTypeName
153 for( auto&& iter : mRegistryLut )
155 if( iter->GetName() == uniqueTypeName )
157 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
158 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
159 return; // never actually happening due to the assert
163 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
164 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
167 void TypeRegistry::CallInitFunctions(void) const
169 for( auto&& iter : mInitFunctions )
175 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
177 return DemangleClassName( registerType.name() );
180 void TypeRegistry::RegisterSignal( TypeRegistration& typeRegistration, const std::string& name, Dali::TypeInfo::SignalConnectorFunction func )
182 for( auto&& iter : mRegistryLut )
184 if( iter->GetName() == typeRegistration.RegisteredName() )
186 iter->AddConnectorFunction( name, func );
192 bool TypeRegistry::RegisterAction( TypeRegistration& typeRegistration, const std::string &name, Dali::TypeInfo::ActionFunction f )
194 for( auto&& iter : mRegistryLut )
196 if( iter->GetName() == typeRegistration.RegisteredName() )
198 iter->AddActionFunction( name, f );
205 bool TypeRegistry::RegisterProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
207 for( auto&& iter : mRegistryLut )
209 if( iter->GetName() == typeRegistration.RegisteredName() )
211 iter->AddProperty( name, index, type, setFunc, getFunc );
219 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 )
221 for( auto&& iter : mRegistryLut )
223 if( iter->GetName() == objectName )
225 iter->AddProperty( name, index, type, setFunc, getFunc );
234 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
236 for( auto&& iter : mRegistryLut )
238 if( iter->GetName() == typeRegistration.RegisteredName() )
240 iter->AddAnimatableProperty( name, index, type );
248 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, const Property::Value& value )
250 for( auto&& iter : mRegistryLut )
252 if( iter->GetName() == typeRegistration.RegisteredName() )
254 iter->AddAnimatableProperty( name, index, value );
262 bool TypeRegistry::RegisterAnimatablePropertyComponent( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
264 for( auto&& iter : mRegistryLut )
266 if( iter->GetName() == typeRegistration.RegisteredName() )
268 iter->AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
276 bool TypeRegistry::RegisterChildProperty( const std::string& registeredType, const std::string& name, Property::Index index, Property::Type type )
278 for( auto&& iter : mRegistryLut )
280 if( iter->GetName() == registeredType )
282 iter->AddChildProperty( name, index, type );
290 bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
292 return RegisterChildProperty( typeRegistration.RegisteredName(), name, index, type );
295 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
299 auto&& type = GetTypeInfo( object );
301 // DoActionTo recurses through base classes
302 done = type->DoActionTo( object, actionName, properties );
306 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
312 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
314 bool connected( false );
316 auto&& type = GetTypeInfo( object );
318 // Connect iterates through base classes
319 connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
323 DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
324 // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
331 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
333 TypeInfoPointer type;
335 // test for custom actor which has another indirection to get to the type hiearchy we're after
336 const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
340 const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
341 type = GetTypeInfo( typeid( custom ) );
344 // the most derived type is a descendant of custom actor but has not registered itself
345 // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
346 type = GetTypeInfo( typeid( Dali::Internal::CustomActor ) );
351 type = GetTypeInfo( typeid( *pBaseObject ) );
357 } // namespace Internal