2 * Copyright (c) 2017 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 Dali::TypeInfo 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( GetImplementation( iter ).GetName() == uniqueTypeName )
75 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
77 return Dali::TypeInfo();
80 Dali::TypeInfo TypeRegistry::GetTypeInfo( const std::type_info& registerType )
82 std::string typeName = DemangleClassName( registerType.name() );
84 return GetTypeInfo( typeName );
87 size_t TypeRegistry::GetTypeNameCount() const
89 return mRegistryLut.size();
93 std::string TypeRegistry::GetTypeName( size_t index ) const
97 if( index < mRegistryLut.size() )
99 name = GetImplementation( mRegistryLut[ index ] ).GetName();
106 bool TypeRegistry::Register( const std::type_info& theTypeInfo, const std::type_info& baseTypeInfo,
107 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit )
109 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
111 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
114 bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
115 Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit )
117 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
119 // check for duplicates using uniqueTypeName
120 for( auto&& iter : mRegistryLut )
122 if( GetImplementation( iter ).GetName() == uniqueTypeName )
124 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
125 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
130 mRegistryLut.push_back( Dali::TypeInfo( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
131 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
133 if( callCreateOnInit )
135 mInitFunctions.push_back(createInstance);
141 bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
142 Dali::CSharpTypeInfo::CreateFunction createInstance )
144 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
146 // check for duplicates using uniqueTypeName
147 for( auto&& iter : mRegistryLut )
149 if( GetImplementation( iter ).GetName() == uniqueTypeName )
151 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
152 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
157 mRegistryLut.push_back( Dali::TypeInfo( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
158 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
163 void TypeRegistry::CallInitFunctions(void) const
165 for( auto&& iter : mInitFunctions )
171 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
173 return DemangleClassName( registerType.name() );
176 void TypeRegistry::RegisterSignal( TypeRegistration& typeRegistration, const std::string& name, Dali::TypeInfo::SignalConnectorFunction func )
178 for( auto&& iter : mRegistryLut )
180 auto&& impl = GetImplementation( iter );
181 if( impl.GetName() == typeRegistration.RegisteredName() )
183 impl.AddConnectorFunction( name, func );
189 bool TypeRegistry::RegisterAction( TypeRegistration& typeRegistration, const std::string &name, Dali::TypeInfo::ActionFunction f )
191 for( auto&& iter : mRegistryLut )
193 auto&& impl = GetImplementation( iter );
194 if( impl.GetName() == typeRegistration.RegisteredName() )
196 impl.AddActionFunction( name, f );
203 bool TypeRegistry::RegisterProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
205 for( auto&& iter : mRegistryLut )
207 auto&& impl = GetImplementation( iter );
208 if( impl.GetName() == typeRegistration.RegisteredName() )
210 impl.AddProperty( name, index, type, setFunc, getFunc );
218 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 )
220 for( auto&& iter : mRegistryLut )
222 auto&& impl = GetImplementation( iter );
223 if( impl.GetName() == objectName )
225 impl.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 auto&& impl = GetImplementation( iter );
239 if( impl.GetName() == typeRegistration.RegisteredName() )
241 impl.AddAnimatableProperty( name, index, type );
249 bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, const Property::Value& value )
251 for( auto&& iter : mRegistryLut )
253 auto&& impl = GetImplementation( iter );
254 if( impl.GetName() == typeRegistration.RegisteredName() )
256 impl.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 auto&& impl = GetImplementation( iter );
269 if( impl.GetName() == typeRegistration.RegisteredName() )
271 impl.AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
279 bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
281 for( auto&& iter : mRegistryLut )
283 auto&& impl = GetImplementation( iter );
284 if( impl.GetName() == typeRegistration.RegisteredName() )
286 impl.AddChildProperty( name, index, type );
294 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
298 Dali::TypeInfo type = GetTypeInfo( object );
302 auto&& impl = GetImplementation( type );
303 if( impl.DoActionTo( object, actionName, properties ) )
308 type = GetTypeInfo( impl.GetBaseName() );
314 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
316 bool connected( false );
318 Dali::TypeInfo type = GetTypeInfo( object );
322 auto&& impl = GetImplementation( type );
323 connected = impl.ConnectSignal( object, connectionTracker, signalName, functor );
328 type = GetTypeInfo( impl.GetBaseName() );
333 // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
340 Dali::TypeInfo TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
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 ) );
354 type = GetTypeInfo( typeid( *pBaseObject ) );
360 } // namespace Internal