Add Internal::IndexedMap
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-registry-impl.cpp
index 5d61e49..73fc652 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <dali/internal/event/common/type-registry-impl.h>
 
 // INTERNAL INCLUDES
-#include <dali/internal/event/common/thread-local-storage.h>
-#include <dali/public-api/object/type-registry.h>
-#include <dali/public-api/object/base-handle.h>
 #include <dali/internal/event/actors/custom-actor-internal.h>
 #include <dali/internal/event/common/demangler.h>
+#include <dali/internal/event/common/thread-local-storage.h>
+#include <dali/public-api/object/base-handle.h>
+#include <dali/public-api/object/type-registry.h>
 
 #include <dali/integration-api/debug.h>
 
 namespace
 {
-
 #if defined(DEBUG_ENABLED)
 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TYPE_REGISTRY");
 #endif
 
-} // namespace anon
+} // namespace
 
 namespace Dali
 {
-
-extern std::string Demangle(const char* symbol);
-
 namespace Internal
 {
-
-TypeRegistry *TypeRegistry::Get()
+TypeRegistry* TypeRegistry::Get()
 {
-  static TypeRegistry *_reg(new TypeRegistry());
+  static TypeRegistry_reg(new TypeRegistry());
   DALI_ASSERT_DEBUG(_reg);
   return _reg;
 }
 
-TypeRegistry::TypeRegistry()
-{
-
-}
+TypeRegistry::TypeRegistry() = default;
 
 TypeRegistry::~TypeRegistry()
 {
   mRegistryLut.clear();
 }
 
-Dali::TypeInfo TypeRegistry::GetTypeInfo( const std::string &uniqueTypeName )
+TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::string& uniqueTypeName)
 {
-  Dali::TypeInfo ret;
-
-  RegistryMap::iterator iter = mRegistryLut.find(uniqueTypeName);
-
-  if( iter != mRegistryLut.end() )
-  {
-    ret = iter->second;
-  }
-  else
+  auto iter = mRegistryLut.Get(ConstString(uniqueTypeName));
+  if(iter != mRegistryLut.end())
   {
-    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str());
+    return iter->second;
   }
 
-  return ret;
+  DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str());
+
+  return TypeRegistry::TypeInfoPointer();
 }
 
-Dali::TypeInfo TypeRegistry::GetTypeInfo( const std::type_info& registerType )
+TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const std::type_info& registerType)
 {
-  Dali::TypeInfo ret;
-
   std::string typeName = DemangleClassName(registerType.name());
 
-  RegistryMap::iterator iter = mRegistryLut.find(typeName);
-
-  if( iter != mRegistryLut.end() )
-  {
-    ret = iter->second;
-  }
-  else
-  {
-    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", registerType.name());
-  }
-
-  return ret;
+  return GetTypeInfo(typeName);
 }
 
-size_t TypeRegistry::GetTypeNameCount() const
+uint32_t TypeRegistry::GetTypeNameCount() const
 {
-  return mRegistryLut.size();
+  return static_cast<uint32_t>(mRegistryLut.size());
 }
 
-
-std::string TypeRegistry::GetTypeName(size_t index) const
+const std::string& TypeRegistry::GetTypeName(uint32_t index) const
 {
-  std::string name;
+  static std::string EMPTY_STRING{};
 
-  if( index < mRegistryLut.size() )
+  if(index < mRegistryLut.size())
   {
-    RegistryMap::const_iterator iter = mRegistryLut.begin();
-    std::advance(iter, index);
-    name = iter->first;
+    return mRegistryLut.GetElementByIndex(index)->GetName();
   }
 
-  return name;
+  return EMPTY_STRING;
 }
 
-
-bool TypeRegistry::Register( const std::type_info& theTypeInfo, const std::type_info& baseTypeInfo,
-                             Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit )
+std::string TypeRegistry::Register(const std::type_info&          theTypeInfo,
+                                   const std::type_info&          baseTypeInfo,
+                                   Dali::TypeInfo::CreateFunction createInstance,
+                                   bool                           callCreateOnInit)
 {
-  std::string uniqueTypeName  = DemangleClassName(theTypeInfo.name());
+  std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
 
-  return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit );
+  return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit);
 }
 
-bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
-                             Dali::TypeInfo::CreateFunction createInstance, bool callCreateOnInit )
+std::string TypeRegistry::Register(const std::type_info&          theTypeInfo,
+                                   const std::type_info&          baseTypeInfo,
+                                   Dali::TypeInfo::CreateFunction createInstance,
+                                   bool                           callCreateOnInit,
+                                   const Dali::PropertyDetails*   defaultProperties,
+                                   Property::Index                defaultPropertyCount)
 {
-  bool ret = false;
+  std::string uniqueTypeName = DemangleClassName(theTypeInfo.name());
 
-  std::string baseTypeName    = DemangleClassName(baseTypeInfo.name());
+  return Register(uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount);
+}
 
-  RegistryMap::iterator iter = mRegistryLut.find(uniqueTypeName);
+std::string TypeRegistry::Register(std::string                    uniqueTypeName,
+                                   const std::type_info&          baseTypeInfo,
+                                   Dali::TypeInfo::CreateFunction createInstance,
+                                   bool                           callCreateOnInit,
+                                   const Dali::PropertyDetails*   defaultProperties,
+                                   Property::Index                defaultPropertyCount)
+{
+  std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
 
-  if( iter == mRegistryLut.end() )
+  if(!mRegistryLut.Register(ConstString(uniqueTypeName), new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount)))
   {
-    mRegistryLut[uniqueTypeName] = Dali::TypeInfo(new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance));
-    ret = true;
-    DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
-  }
-  else
-  {
-    DALI_LOG_WARNING("Duplicate name for TypeRegistry for '%s'\n", + uniqueTypeName.c_str());
-    DALI_ASSERT_ALWAYS(!"Duplicate type name for Type Registation");
+    DALI_LOG_WARNING("Duplicate name in TypeRegistry for '%s'\n", +uniqueTypeName.c_str());
+    DALI_ASSERT_ALWAYS(!"Duplicate type name in Type Registration");
+    return uniqueTypeName; // never actually happening due to the assert
   }
+  DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
 
-  if( callCreateOnInit )
+  if(callCreateOnInit)
   {
     mInitFunctions.push_back(createInstance);
   }
 
-  return ret;
+  return uniqueTypeName;
 }
 
-bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
-    Dali::CSharpTypeInfo::CreateFunction createInstance )
+void TypeRegistry::Register(std::string uniqueTypeName, const std::type_info& baseTypeInfo, Dali::CSharpTypeInfo::CreateFunction createInstance)
 {
+  std::string baseTypeName = DemangleClassName(baseTypeInfo.name());
 
-  bool ret = false;
-
-  std::string baseTypeName    = DemangleClassName(baseTypeInfo.name());
-
-  RegistryMap::iterator iter = mRegistryLut.find(uniqueTypeName);
-
-  if( iter == mRegistryLut.end() )
+  if(!mRegistryLut.Register(ConstString(uniqueTypeName), TypeRegistry::TypeInfoPointer(new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance))))
   {
-    mRegistryLut[uniqueTypeName] = Dali::TypeInfo(new Internal::TypeInfo(uniqueTypeName, baseTypeName, createInstance));
-    ret = true;
-    DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
+    DALI_LOG_WARNING("Duplicate name in TypeRegistry for '%s'\n", +uniqueTypeName.c_str());
+    DALI_ASSERT_ALWAYS(!"Duplicate type name in Type Registration");
+    return; // never actually happening due to the assert
   }
-  else
-  {
-    DALI_LOG_WARNING("Duplicate name for TypeRegistry for '%s'\n", + uniqueTypeName.c_str());
-    DALI_ASSERT_ALWAYS(!"Duplicate type name for Type Registation");
-  }
-
-  return ret;
-
+  DALI_LOG_INFO(gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str());
 }
 
 void TypeRegistry::CallInitFunctions(void) const
 {
-  for( InitFunctions::const_iterator iter = mInitFunctions.begin(); iter != mInitFunctions.end(); ++iter)
+  for(auto&& iter : mInitFunctions)
   {
     (*iter)();
   }
 }
 
-std::string TypeRegistry::RegistrationName( const std::type_info& registerType )
+std::string TypeRegistry::RegistrationName(const std::type_info& registerType)
 {
-  return DemangleClassName( registerType.name() );
+  return DemangleClassName(registerType.name());
 }
 
-void TypeRegistry::RegisterSignal( TypeRegistration& typeRegistration, const std::string& name, Dali::TypeInfo::SignalConnectorFunction func )
+void TypeRegistry::RegisterSignal(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::SignalConnectorFunction func)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( typeRegistration.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddConnectorFunction( name, func );
+    iter->second->AddConnectorFunction(std::move(name), func);
   }
 }
 
-bool TypeRegistry::RegisterAction( TypeRegistration &registered, const std::string &name, Dali::TypeInfo::ActionFunction f)
+bool TypeRegistry::RegisterAction(TypeRegistration& typeRegistration, std::string name, Dali::TypeInfo::ActionFunction f)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( registered.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddActionFunction( name, f );
-
+    iter->second->AddActionFunction(std::move(name), f);
     return true;
   }
-  else
-  {
-    return false;
-  }
+  return false;
 }
 
-bool TypeRegistry::RegisterProperty( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
+bool TypeRegistry::RegisterProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( registered.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddProperty( name, index, type, setFunc, getFunc );
-
+    iter->second->AddProperty(std::move(name), index, type, setFunc, getFunc);
     return true;
   }
-
   return false;
 }
 
-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 )
+bool TypeRegistry::RegisterProperty(const std::string& objectName, std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( objectName );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(objectName));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddProperty( name, index, type, setFunc, getFunc );
-
+    iter->second->AddProperty(std::move(name), index, type, setFunc, getFunc);
     return true;
   }
 
   return false;
-
 }
 
-
-bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type )
+bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( registered.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddAnimatableProperty( name, index, type );
-
+    iter->second->AddAnimatableProperty(std::move(name), index, type);
     return true;
   }
 
   return false;
 }
 
-bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& registered, const std::string& name, Property::Index index, const Property::Value& value )
+bool TypeRegistry::RegisterAnimatableProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Value value)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( registered.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddAnimatableProperty( name, index, value );
-
+    iter->second->AddAnimatableProperty(std::move(name), index, std::move(value));
     return true;
   }
 
   return false;
 }
 
-bool TypeRegistry::RegisterAnimatablePropertyComponent( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
+bool TypeRegistry::RegisterAnimatablePropertyComponent(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( registered.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(typeRegistration.RegisteredName()));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
-
+    iter->second->AddAnimatablePropertyComponent(std::move(name), index, baseIndex, componentIndex);
     return true;
   }
 
   return false;
 }
 
-bool TypeRegistry::RegisterChildProperty( TypeRegistration& registered, const std::string& name, Property::Index index, Property::Type type )
+bool TypeRegistry::RegisterChildProperty(const std::string& registeredType, std::string name, Property::Index index, Property::Type type)
 {
-  RegistryMap::iterator iter = mRegistryLut.find( registered.RegisteredName() );
-
-  if( iter != mRegistryLut.end() )
+  auto iter = mRegistryLut.Get(ConstString(registeredType));
+  if(iter != mRegistryLut.end())
   {
-    DALI_ASSERT_DEBUG(iter->second);
-
-    GetImplementation(iter->second).AddChildProperty( name, index, type );
-
+    iter->second->AddChildProperty(std::move(name), index, type);
     return true;
   }
 
   return false;
 }
 
-bool TypeRegistry::DoActionTo( BaseObject * const object, const std::string &actionName, const Property::Map &properties)
+bool TypeRegistry::RegisterChildProperty(TypeRegistration& typeRegistration, std::string name, Property::Index index, Property::Type type)
+{
+  return RegisterChildProperty(typeRegistration.RegisteredName(), std::move(name), index, type);
+}
+
+bool TypeRegistry::DoActionTo(BaseObject* const object, const std::string& actionName, const Property::Map& properties)
 {
   bool done = false;
 
-  Dali::TypeInfo type = GetTypeInfo( object );
+  auto&& type = GetTypeInfo(object);
+
+  // DoActionTo recurses through base classes
+  done = type->DoActionTo(object, actionName, properties);
 
-  while( type )
+  if(!done)
   {
-    if(GetImplementation(type).DoActionTo(object, actionName, properties))
-    {
-      done = true;
-      break;
-    }
-    type = GetTypeInfo( type.GetBaseName() );
+    DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
   }
 
   return done;
 }
 
-bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
+bool TypeRegistry::ConnectSignal(BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor)
 {
-  bool connected( false );
+  bool connected(false);
 
-  Dali::TypeInfo type = GetTypeInfo( object );
+  auto&& type = GetTypeInfo(object);
 
-  while( type )
-  {
-    connected = GetImplementation(type).ConnectSignal( object, connectionTracker, signalName, functor );
-    if( connected )
-    {
-      break;
-    }
-    type = GetTypeInfo( type.GetBaseName() );
-  }
+  // Connect iterates through base classes
+  connected = type->ConnectSignal(object, connectionTracker, signalName, functor);
 
-  if( !connected )
+  if(!connected)
   {
+    DALI_LOG_WARNING("Type '%s' signal '%s' connection failed \n", type->GetName().c_str(), signalName.c_str());
     // Ownership of functor was not passed to Dali::CallbackBase, so clean-up now
     delete functor;
   }
@@ -368,21 +295,27 @@ bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface
   return connected;
 }
 
-Dali::TypeInfo TypeRegistry::GetTypeInfo(const Dali::BaseObject * const pBaseObject)
+TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo(const Dali::BaseObject* const pBaseObject)
 {
-  Dali::TypeInfo type;
+  TypeInfoPointer type;
 
   // test for custom actor which has another indirection to get to the type hiearchy we're after
-  const Dali::Internal::CustomActor * const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
+  const Dali::Internal::CustomActor* const pCustom = dynamic_cast<const Dali::Internal::CustomActor*>(pBaseObject);
 
   if(pCustom)
   {
     const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
-    type = GetTypeInfo( typeid( custom ) );
+    type                                = GetTypeInfo(typeid(custom));
+    if(!type)
+    {
+      // the most derived type is a descendant of custom actor but has not registered itself
+      // so we'll just treat it as a custom actor for now so it "inherits" all of actors properties, actions and signals
+      type = GetTypeInfo(typeid(Dali::Internal::CustomActor));
+    }
   }
   else
   {
-    type = GetTypeInfo( typeid( *pBaseObject ) );
+    type = GetTypeInfo(typeid(*pBaseObject));
   }
 
   return type;