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() = default;
54 TypeRegistry::~TypeRegistry()
59 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
61 for( auto&& iter : mRegistryLut )
63 // Note! mRegistryLut contains Dali::TypeInfo handles, so cannot call GetTypeName()
64 // as it calls us back resulting in infinite loop (GetTypeName is in BaseHandle part)
65 if( iter->GetName() == uniqueTypeName )
70 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
72 return TypeRegistry::TypeInfoPointer();
75 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::type_info& registerType )
77 std::string typeName = DemangleClassName( registerType.name() );
79 return GetTypeInfo( typeName );
82 uint32_t TypeRegistry::GetTypeNameCount() const
84 return static_cast<uint32_t>( mRegistryLut.size() );
87 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
89 static std::string EMPTY_STRING{};
91 if( index < mRegistryLut.size() )
93 return mRegistryLut[index]->GetName();
99 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
100 const std::type_info& baseTypeInfo,
101 Dali::TypeInfo::CreateFunction createInstance,
102 bool callCreateOnInit )
104 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
106 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
109 std::string TypeRegistry::Register( const std::type_info& theTypeInfo,
110 const std::type_info& baseTypeInfo,
111 Dali::TypeInfo::CreateFunction createInstance,
112 bool callCreateOnInit,
113 const Dali::PropertyDetails* defaultProperties,
114 Property::Index defaultPropertyCount )
116 std::string uniqueTypeName = DemangleClassName( theTypeInfo.name() );
118 return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
121 std::string TypeRegistry::Register(std::string uniqueTypeName,
122 const std::type_info& baseTypeInfo,
123 Dali::TypeInfo::CreateFunction createInstance,
124 bool callCreateOnInit,
125 const Dali::PropertyDetails* defaultProperties,
126 Property::Index defaultPropertyCount)
128 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
130 // check for duplicates using uniqueTypeName
131 for( auto&& iter : mRegistryLut )
133 if( iter->GetName() == uniqueTypeName )
135 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
136 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
137 return uniqueTypeName; // never actually happening due to the assert
141 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer(
142 new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount ) ) );
143 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
145 if( callCreateOnInit )
147 mInitFunctions.push_back(createInstance);
150 return uniqueTypeName;
153 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
155 std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
157 // check for duplicates using uniqueTypeName
158 for( auto&& iter : mRegistryLut )
160 if( iter->GetName() == uniqueTypeName )
162 DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
163 DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
164 return; // never actually happening due to the assert
168 mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
169 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
172 void TypeRegistry::CallInitFunctions(void) const
174 for( auto&& iter : mInitFunctions )
180 std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
182 return DemangleClassName( registerType.name() );
185 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
187 for( auto&& iter : mRegistryLut )
189 if( iter->GetName() == typeRegistration.RegisteredName() )
191 iter->AddConnectorFunction(std::move(name), func);
197 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
199 for( auto&& iter : mRegistryLut )
201 if( iter->GetName() == typeRegistration.RegisteredName() )
203 iter->AddActionFunction(std::move(name), f);
210 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
212 for( auto&& iter : mRegistryLut )
214 if( iter->GetName() == typeRegistration.RegisteredName() )
216 iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
224 bool TypeRegistry::RegisterProperty(const std::string& objectName, std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
226 for( auto&& iter : mRegistryLut )
228 if( iter->GetName() == objectName )
230 iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
238 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
240 for( auto&& iter : mRegistryLut )
242 if( iter->GetName() == typeRegistration.RegisteredName() )
244 iter->AddAnimatableProperty(std::move(name), index, type);
252 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
254 for( auto&& iter : mRegistryLut )
256 if( iter->GetName() == typeRegistration.RegisteredName() )
258 iter->AddAnimatableProperty(std::move(name), index, std::move(value));
266 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
268 for( auto&& iter : mRegistryLut )
270 if( iter->GetName() == typeRegistration.RegisteredName() )
272 iter->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
280 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
282 for( auto&& iter : mRegistryLut )
284 if( iter->GetName() == registeredType )
286 iter->AddChildProperty(std::move(name), index, type);
294 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
296 return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
299 bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string& actionName, const Property::Map& properties )
303 auto&& type = GetTypeInfo( object );
305 // DoActionTo recurses through base classes
306 done = type->DoActionTo( object, actionName, properties );
310 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
316 bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
318 bool connected( false );
320 auto&& type = GetTypeInfo( object );
322 // Connect iterates through base classes
323 connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
327 DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
328 // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
335 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
337 TypeInfoPointer type;
339 // test for custom actor which has another indirection to get to the type hiearchy we're after
340 const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
344 const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
345 type = GetTypeInfo( typeid( custom ) );
348 // the most derived type is a descendant of custom actor but has not registered itself
349 // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
350 type = GetTypeInfo( typeid( Dali::Internal::CustomActor ) );
355 type = GetTypeInfo( typeid( *pBaseObject ) );
361 } // namespace Internal