Ignore insert/erase function during PreRender's itemsDirtyRects
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-registry-impl.cpp
1 /*
2  * Copyright (c) 2021 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   for(auto&& iter : mRegistryLut)
59   {
60     // Note! mRegistryLut contains Dali::TypeInfo handles, so cannot call GetTypeName()
61     // as it calls us back resulting in infinite loop (GetTypeName is in BaseHandle part)
62     if(iter->GetName() == uniqueTypeName)
63     {
64       return iter;
65     }
66   }
67   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str());
68
69   return TypeRegistry::TypeInfoPointer();
70 }
71
72 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::type_info& registerType)
73 {
74   std::string typeName = DemangleClassName(registerType.name());
75
76   return GetTypeInfo(typeName);
77 }
78
79 uint32_t TypeRegistry::GetTypeNameCount() const
80 {
81   return static_cast<uint32_t>(mRegistryLut.size());
82 }
83
84 const std::string& TypeRegistry::GetTypeName(uint32_t index) const
85 {
86   static std::string EMPTY_STRING{};
87
88   if(index < mRegistryLut.size())
89   {
90     return mRegistryLut[index]->GetName();
91   }
92
93   return EMPTY_STRING;
94 }
95
96 std::string TypeRegistry::Register(const std::type_info&          theTypeInfo,
97                                    const std::type_info&          baseTypeInfo,
98                                    Dali::TypeInfo::CreateFunction createInstance,
99                                    bool                           callCreateOnInit)
100 {
101   std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
102
103   return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit);
104 }
105
106 std::string TypeRegistry::Register(const std::type_info&          theTypeInfo,
107                                    const std::type_info&          baseTypeInfo,
108                                    Dali::TypeInfo::CreateFunction createInstance,
109                                    bool                           callCreateOnInit,
110                                    const Dali::PropertyDetails*   defaultProperties,
111                                    Property::Index                defaultPropertyCount)
112 {
113   std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
114
115   return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount);
116 }
117
118 std::string TypeRegistry::Register(std::string                    uniqueTypeName,
119                                    const std::type_info&          baseTypeInfo,
120                                    Dali::TypeInfo::CreateFunction createInstance,
121                                    bool                           callCreateOnInit,
122                                    const Dali::PropertyDetails*   defaultProperties,
123                                    Property::Index                defaultPropertyCount)
124 {
125   std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
126
127   // check for duplicates using uniqueTypeName
128   for(auto&& iter : mRegistryLut)
129   {
130     if(iter->GetName() == uniqueTypeName)
131     {
132       DALI_LOG_WARNING("Duplicate name in TypeRegistry for '%s'\n", +uniqueTypeName.c_str());
133       DALI_ASSERT_ALWAYS(!"Duplicate type name in Type Registration");
134       return uniqueTypeName; // never actually happening due to the assert
135     }
136   }
137
138   mRegistryLut.push_back(TypeRegistry::TypeInfoPointer(
139     new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount)));
140   DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
141
142   if(callCreateOnInit)
143   {
144     mInitFunctions.push_back(createInstance);
145   }
146
147   return uniqueTypeName;
148 }
149
150 void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
151 {
152   std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
153
154   // check for duplicates using uniqueTypeName
155   for(auto&& iter : mRegistryLut)
156   {
157     if(iter->GetName() == uniqueTypeName)
158     {
159       DALI_LOG_WARNING("Duplicate name in TypeRegistry for '%s'\n", +uniqueTypeName.c_str());
160       DALI_ASSERT_ALWAYS(!"Duplicate type name in Type Registration");
161       return; // never actually happening due to the assert
162     }
163   }
164
165   mRegistryLut.push_back(TypeRegistry::TypeInfoPointer(new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance)));
166   DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
167 }
168
169 void TypeRegistry::CallInitFunctions(void) const
170 {
171   for(auto&& iter : mInitFunctions)
172   {
173     (*iter)();
174   }
175 }
176
177 std::string TypeRegistry::RegistrationName(const std::type_info& registerType)
178 {
179   return DemangleClassName(registerType.name());
180 }
181
182 void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
183 {
184   for(auto&& iter : mRegistryLut)
185   {
186     if(iter->GetName() == typeRegistration.RegisteredName())
187     {
188       iter->AddConnectorFunction(std::move(name), func);
189       break;
190     }
191   }
192 }
193
194 bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
195 {
196   for(auto&& iter : mRegistryLut)
197   {
198     if(iter->GetName() == typeRegistration.RegisteredName())
199     {
200       iter->AddActionFunction(std::move(name), f);
201       return true;
202     }
203   }
204   return false;
205 }
206
207 bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
208 {
209   for(auto&& iter : mRegistryLut)
210   {
211     if(iter->GetName() == typeRegistration.RegisteredName())
212     {
213       iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
214       return true;
215     }
216   }
217
218   return false;
219 }
220
221 bool TypeRegistry::RegisterProperty(const std::string& objectName, std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
222 {
223   for(auto&& iter : mRegistryLut)
224   {
225     if(iter->GetName() == objectName)
226     {
227       iter->AddProperty(std::move(name), index, type, setFunc, getFunc);
228       return true;
229     }
230   }
231
232   return false;
233 }
234
235 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
236 {
237   for(auto&& iter : mRegistryLut)
238   {
239     if(iter->GetName() == typeRegistration.RegisteredName())
240     {
241       iter->AddAnimatableProperty(std::move(name), index, type);
242       return true;
243     }
244   }
245
246   return false;
247 }
248
249 bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
250 {
251   for(auto&& iter : mRegistryLut)
252   {
253     if(iter->GetName() == typeRegistration.RegisteredName())
254     {
255       iter->AddAnimatableProperty(std::move(name), index, std::move(value));
256       return true;
257     }
258   }
259
260   return false;
261 }
262
263 bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
264 {
265   for(auto&& iter : mRegistryLut)
266   {
267     if(iter->GetName() == typeRegistration.RegisteredName())
268     {
269       iter->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
270       return true;
271     }
272   }
273
274   return false;
275 }
276
277 bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
278 {
279   for(auto&& iter : mRegistryLut)
280   {
281     if(iter->GetName() == registeredType)
282     {
283       iter->AddChildProperty(std::move(name), index, type);
284       return true;
285     }
286   }
287
288   return false;
289 }
290
291 bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
292 {
293   return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
294 }
295
296 bool TypeRegistry::DoActionTo(BaseObject* const object, const std::string& actionName, const Property::Map& properties)
297 {
298   bool done = false;
299
300   auto&& type = GetTypeInfo(object);
301
302   // DoActionTo recurses through base classes
303   done = type->DoActionTo(object, actionName, properties);
304
305   if(!done)
306   {
307     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
308   }
309
310   return done;
311 }
312
313 bool TypeRegistry::ConnectSignal(BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor)
314 {
315   bool connected(false);
316
317   auto&& type = GetTypeInfo(object);
318
319   // Connect iterates through base classes
320   connected = type->ConnectSignal(object, connectionTracker, signalName, functor);
321
322   if(!connected)
323   {
324     DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
325     // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
326     delete functor;
327   }
328
329   return connected;
330 }
331
332 TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject* const pBaseObject)
333 {
334   TypeInfoPointer type;
335
336   // test for custom actor which has another indirection to get to the type hiearchy we're after
337   const Dali::Internal::CustomActor* const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
338
339   if(pCustom)
340   {
341     const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
342     type                                = GetTypeInfo(typeid(custom));
343     if(!type)
344     {
345       // the most derived type is a descendant of custom actor but has not registered itself
346       // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
347       type = GetTypeInfo(typeid(Dali::Internal::CustomActor));
348     }
349   }
350   else
351   {
352     type = GetTypeInfo(typeid(*pBaseObject));
353   }
354
355   return type;
356 }
357
358 } // namespace Internal
359
360 } // namespace Dali