2 * Copyright (c) 2022 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/actors/custom-actor-internal.h>
23 #include <dali/internal/event/common/demangler.h>
24 #include <dali/internal/event/common/thread-local-storage.h>
25 #include <dali/public-api/object/base-handle.h>
26 #include <dali/public-api/object/type-registry.h>
28 #include <dali/integration-api/debug.h>
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TYPE_REGISTRY");
42 TypeRegistry* TypeRegistry::Get()
44 static TypeRegistry* _reg(new TypeRegistry());
45 DALI_ASSERT_DEBUG(_reg);
49 TypeRegistry::TypeRegistry() = default;
51 TypeRegistry::~TypeRegistry()
56 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::string& uniqueTypeName)
58 auto iter = mRegistryLut.Get(ConstString(uniqueTypeName));
59 if(iter != mRegistryLut.end())
64 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str());
66 return TypeRegistry::TypeInfoPointer();
69 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::type_info& registerType)
71 std::string typeName = DemangleClassName(registerType.name());
73 return GetTypeInfo(typeName);
76 uint32_t TypeRegistry::GetTypeNameCount() const
78 return static_cast<uint32_t>(mRegistryLut.size());
81 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
83 static std::string EMPTY_STRING{};
85 if(index < mRegistryLut.size())
87 return mRegistryLut.GetElementByIndex(index)->GetName();
93 std::string TypeRegistry::Register(const std::type_info& theTypeInfo,
94 const std::type_info& baseTypeInfo,
95 Dali::TypeInfo::CreateFunction createInstance,
96 bool callCreateOnInit)
98 std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
100 return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit);
103 std::string TypeRegistry::Register(const std::type_info& theTypeInfo,
104 const std::type_info& baseTypeInfo,
105 Dali::TypeInfo::CreateFunction createInstance,
106 bool callCreateOnInit,
107 const Dali::PropertyDetails* defaultProperties,
108 Property::Index defaultPropertyCount)
110 std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
112 return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount);
115 std::string TypeRegistry::Register(std::string uniqueTypeName,
116 const std::type_info& baseTypeInfo,
117 Dali::TypeInfo::CreateFunction createInstance,
118 bool callCreateOnInit,
119 const Dali::PropertyDetails* defaultProperties,
120 Property::Index defaultPropertyCount)
122 std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
124 if(!mRegistryLut.Register(ConstString(uniqueTypeName), new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount)))
126 DALI_LOG_WARNING("Duplicate name in TypeRegistry for '%s'\n", +uniqueTypeName.c_str());
127 DALI_ASSERT_ALWAYS(!"Duplicate type name in Type Registration");
128 return uniqueTypeName; // never actually happening due to the assert
130 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
134 mInitFunctions.push_back(createInstance);
137 return uniqueTypeName;
140 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
142 std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
144 if(!mRegistryLut.Register(ConstString(uniqueTypeName), TypeRegistry::TypeInfoPointer(new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance))))
146 DALI_LOG_WARNING("Duplicate name in TypeRegistry for '%s'\n", +uniqueTypeName.c_str());
147 DALI_ASSERT_ALWAYS(!"Duplicate type name in Type Registration");
148 return; // never actually happening due to the assert
150 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
153 void TypeRegistry::CallInitFunctions(void) const
155 for(auto&& iter : mInitFunctions)
161 std::string TypeRegistry::RegistrationName(const std::type_info& registerType)
163 return DemangleClassName(registerType.name());
166 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
168 auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
169 if(iter != mRegistryLut.end())
171 iter->second->AddConnectorFunction(std::move(name), func);
175 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
177 auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
178 if(iter != mRegistryLut.end())
180 iter->second->AddActionFunction(std::move(name), f);
186 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
188 auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
189 if(iter != mRegistryLut.end())
191 iter->second->AddProperty(std::move(name), index, type, setFunc, getFunc);
197 bool TypeRegistry::RegisterProperty(const std::string& objectName, std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
199 auto iter = mRegistryLut.Get(ConstString(objectName));
200 if(iter != mRegistryLut.end())
202 iter->second->AddProperty(std::move(name), index, type, setFunc, getFunc);
209 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
211 auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
212 if(iter != mRegistryLut.end())
214 iter->second->AddAnimatableProperty(std::move(name), index, type);
221 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
223 auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
224 if(iter != mRegistryLut.end())
226 iter->second->AddAnimatableProperty(std::move(name), index, std::move(value));
233 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
235 auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
236 if(iter != mRegistryLut.end())
238 iter->second->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
245 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
247 auto iter = mRegistryLut.Get(ConstString(registeredType));
248 if(iter != mRegistryLut.end())
250 iter->second->AddChildProperty(std::move(name), index, type);
257 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
259 return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
262 bool TypeRegistry::DoActionTo(BaseObject* const object, const std::string& actionName, const Property::Map& properties)
266 auto&& type = GetTypeInfo(object);
268 // DoActionTo recurses through base classes
269 done = type->DoActionTo(object, actionName, properties);
273 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
279 bool TypeRegistry::ConnectSignal(BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor)
281 bool connected(false);
283 auto&& type = GetTypeInfo(object);
285 // Connect iterates through base classes
286 connected = type->ConnectSignal(object, connectionTracker, signalName, functor);
290 DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
291 // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
298 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject* const pBaseObject)
300 TypeInfoPointer type;
302 // test for custom actor which has another indirection to get to the type hiearchy we're after
303 const Dali::Internal::CustomActor* const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
307 const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
308 type = GetTypeInfo(typeid(custom));
311 // the most derived type is a descendant of custom actor but has not registered itself
312 // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
313 type = GetTypeInfo(typeid(Dali::Internal::CustomActor));
318 type = GetTypeInfo(typeid(*pBaseObject));
324 } // namespace Internal