Add Internal::IndexedMap
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-registry-impl.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/type-registry-impl.h>
20
21 // INTERNAL INCLUDES
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>
27
28 #include <dali/integration-api/debug.h>
29
30 namespace
31 {
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TYPE_REGISTRY");
34 #endif
35
36 } // namespace
37
38 namespace Dali
39 {
40 namespace Internal
41 {
42 TypeRegistry* TypeRegistry::Get()
43 {
44   static TypeRegistry* _reg(new TypeRegistry());
45   DALI_ASSERT_DEBUG(_reg);
46   return _reg;
47 }
48
49 TypeRegistry::TypeRegistry() = default;
50
51 TypeRegistry::~TypeRegistry()
52 {
53   mRegistryLut.clear();
54 }
55
56 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::string& uniqueTypeName)
57 {
58   auto iter = mRegistryLut.Get(ConstString(uniqueTypeName));
59   if(iter != mRegistryLut.end())
60   {
61     return iter->second;
62   }
63
64   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str());
65
66   return TypeRegistry::TypeInfoPointer();
67 }
68
69 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::type_info& registerType)
70 {
71   std::string typeName = DemangleClassName(registerType.name());
72
73   return GetTypeInfo(typeName);
74 }
75
76 uint32_t TypeRegistry::GetTypeNameCount() const
77 {
78   return static_cast<uint32_t>(mRegistryLut.size());
79 }
80
81 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
82 {
83   static std::string EMPTY_STRING{};
84
85   if(index < mRegistryLut.size())
86   {
87     return mRegistryLut.GetElementByIndex(index)->GetName();
88   }
89
90   return EMPTY_STRING;
91 }
92
93 std::string TypeRegistry::Register(const std::type_info&          theTypeInfo,
94                                    const std::type_info&          baseTypeInfo,
95                                    Dali::TypeInfo::CreateFunction createInstance,
96                                    bool                           callCreateOnInit)
97 {
98   std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
99
100   return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit);
101 }
102
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)
109 {
110   std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
111
112   return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount);
113 }
114
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)
121 {
122   std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
123
124   if(!mRegistryLut.Register(ConstString(uniqueTypeName), new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount)))
125   {
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
129   }
130   DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
131
132   if(callCreateOnInit)
133   {
134     mInitFunctions.push_back(createInstance);
135   }
136
137   return uniqueTypeName;
138 }
139
140 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
141 {
142   std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
143
144   if(!mRegistryLut.Register(ConstString(uniqueTypeName), TypeRegistry::TypeInfoPointer(new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance))))
145   {
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
149   }
150   DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
151 }
152
153 void TypeRegistry::CallInitFunctions(void) const
154 {
155   for(auto&& iter : mInitFunctions)
156   {
157     (*iter)();
158   }
159 }
160
161 std::string TypeRegistry::RegistrationName(const std::type_info& registerType)
162 {
163   return DemangleClassName(registerType.name());
164 }
165
166 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
167 {
168   auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
169   if(iter != mRegistryLut.end())
170   {
171     iter->second->AddConnectorFunction(std::move(name), func);
172   }
173 }
174
175 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
176 {
177   auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
178   if(iter != mRegistryLut.end())
179   {
180     iter->second->AddActionFunction(std::move(name), f);
181     return true;
182   }
183   return false;
184 }
185
186 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
187 {
188   auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
189   if(iter != mRegistryLut.end())
190   {
191     iter->second->AddProperty(std::move(name), index, type, setFunc, getFunc);
192     return true;
193   }
194   return false;
195 }
196
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)
198 {
199   auto iter = mRegistryLut.Get(ConstString(objectName));
200   if(iter != mRegistryLut.end())
201   {
202     iter->second->AddProperty(std::move(name), index, type, setFunc, getFunc);
203     return true;
204   }
205
206   return false;
207 }
208
209 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
210 {
211   auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
212   if(iter != mRegistryLut.end())
213   {
214     iter->second->AddAnimatableProperty(std::move(name), index, type);
215     return true;
216   }
217
218   return false;
219 }
220
221 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
222 {
223   auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
224   if(iter != mRegistryLut.end())
225   {
226     iter->second->AddAnimatableProperty(std::move(name), index, std::move(value));
227     return true;
228   }
229
230   return false;
231 }
232
233 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
234 {
235   auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
236   if(iter != mRegistryLut.end())
237   {
238     iter->second->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
239     return true;
240   }
241
242   return false;
243 }
244
245 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
246 {
247   auto iter = mRegistryLut.Get(ConstString(registeredType));
248   if(iter != mRegistryLut.end())
249   {
250     iter->second->AddChildProperty(std::move(name), index, type);
251     return true;
252   }
253
254   return false;
255 }
256
257 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
258 {
259   return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
260 }
261
262 bool TypeRegistry::DoActionTo(BaseObject* const object, const std::string& actionName, const Property::Map& properties)
263 {
264   bool done = false;
265
266   auto&& type = GetTypeInfo(object);
267
268   // DoActionTo recurses through base classes
269   done = type->DoActionTo(object, actionName, properties);
270
271   if(!done)
272   {
273     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
274   }
275
276   return done;
277 }
278
279 bool TypeRegistry::ConnectSignal(BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor)
280 {
281   bool connected(false);
282
283   auto&& type = GetTypeInfo(object);
284
285   // Connect iterates through base classes
286   connected = type->ConnectSignal(object, connectionTracker, signalName, functor);
287
288   if(!connected)
289   {
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
292     delete functor;
293   }
294
295   return connected;
296 }
297
298 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject* const pBaseObject)
299 {
300   TypeInfoPointer type;
301
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);
304
305   if(pCustom)
306   {
307     const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
308     type                                = GetTypeInfo(typeid(custom));
309     if(!type)
310     {
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));
314     }
315   }
316   else
317   {
318     type = GetTypeInfo(typeid(*pBaseObject));
319   }
320
321   return type;
322 }
323
324 } // namespace Internal
325
326 } // namespace Dali