Optimize type-info-impl and type-registry-impl
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-registry-impl.cpp
index 977cb87..d380f83 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
@@ -61,23 +61,23 @@ TypeRegistry::~TypeRegistry()
   mRegistryLut.clear();
 }
 
-Dali::TypeInfo TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
+TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::string& uniqueTypeName )
 {
   for( auto&& iter : mRegistryLut )
   {
     // Note! mRegistryLut contains Dali::TypeInfo handles, so cannot call GetTypeName()
     // as it calls us back resulting in infinite loop (GetTypeName is in BaseHandle part)
-    if( GetImplementation( iter ).GetName() == uniqueTypeName )
+    if( iter->GetName() == uniqueTypeName )
     {
       return iter;
     }
   }
   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Cannot find requested type '%s'\n", uniqueTypeName.c_str() );
 
-  return Dali::TypeInfo();
+  return TypeRegistry::TypeInfoPointer();
 }
 
-Dali::TypeInfo TypeRegistry::GetTypeInfo( const std::type_info& registerType )
+TypeRegistry::TypeInfoPointer TypeRegistry::GetTypeInfo( const std::type_info& registerType )
 {
   std::string typeName = DemangleClassName( registerType.name() );
 
@@ -89,21 +89,19 @@ size_t TypeRegistry::GetTypeNameCount() const
   return mRegistryLut.size();
 }
 
-
 std::string TypeRegistry::GetTypeName( size_t index ) const
 {
   std::string name;
 
   if( index < mRegistryLut.size() )
   {
-    name = GetImplementation( mRegistryLut[ index ] ).GetName();
+    name = mRegistryLut[ index ]->GetName();
   }
 
   return name;
 }
 
-
-bool TypeRegistry::Register( const std::type_info& theTypeInfo, const std::type_info& baseTypeInfo,
+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() );
@@ -111,23 +109,33 @@ bool TypeRegistry::Register( const std::type_info& theTypeInfo, const std::type_
   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 )
+{
+  std::string uniqueTypeName  = DemangleClassName( theTypeInfo.name() );
+
+  return Register( uniqueTypeName, baseTypeInfo, createInstance, callCreateOnInit, defaultProperties, defaultPropertyCount );
+}
+
+std::string TypeRegistry::Register( const 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() );
 
   // check for duplicates using uniqueTypeName
   for( auto&& iter : mRegistryLut )
   {
-    if( GetImplementation( iter ).GetName() == uniqueTypeName )
+    if( iter->GetName() == uniqueTypeName )
     {
       DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
       DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
-      return false;
+      return uniqueTypeName; // never actually happening due to the assert
     }
   }
 
-  mRegistryLut.push_back( Dali::TypeInfo( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
+  mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance, defaultProperties, defaultPropertyCount ) ) );
   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
 
   if( callCreateOnInit )
@@ -135,10 +143,10 @@ bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_
     mInitFunctions.push_back(createInstance);
   }
 
-  return true;
+  return uniqueTypeName;
 }
 
-bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
+void TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_info& baseTypeInfo,
     Dali::CSharpTypeInfo::CreateFunction createInstance )
 {
   std::string baseTypeName = DemangleClassName( baseTypeInfo.name() );
@@ -146,18 +154,16 @@ bool TypeRegistry::Register( const std::string& uniqueTypeName, const std::type_
   // check for duplicates using uniqueTypeName
   for( auto&& iter : mRegistryLut )
   {
-    if( GetImplementation( iter ).GetName() == uniqueTypeName )
+    if( iter->GetName() == uniqueTypeName )
     {
       DALI_LOG_WARNING( "Duplicate name in TypeRegistry for '%s'\n", + uniqueTypeName.c_str() );
       DALI_ASSERT_ALWAYS( !"Duplicate type name in Type Registration" );
-      return false;
+      return; // never actually happening due to the assert
     }
   }
 
-  mRegistryLut.push_back( Dali::TypeInfo( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
+  mRegistryLut.push_back( TypeRegistry::TypeInfoPointer( new Internal::TypeInfo( uniqueTypeName, baseTypeName, createInstance ) ) );
   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Type Registration %s(%s)\n", uniqueTypeName.c_str(), baseTypeName.c_str() );
-
-  return true;
 }
 
 void TypeRegistry::CallInitFunctions(void) const
@@ -177,10 +183,9 @@ void TypeRegistry::RegisterSignal( TypeRegistration& typeRegistration, const std
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == typeRegistration.RegisteredName() )
     {
-      impl.AddConnectorFunction( name, func );
+      iter->AddConnectorFunction( name, func );
       break;
     }
   }
@@ -190,10 +195,9 @@ bool TypeRegistry::RegisterAction( TypeRegistration& typeRegistration, const std
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == typeRegistration.RegisteredName() )
     {
-      impl.AddActionFunction( name, f );
+      iter->AddActionFunction( name, f );
       return true;
     }
   }
@@ -204,10 +208,9 @@ bool TypeRegistry::RegisterProperty( TypeRegistration& typeRegistration, const s
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == typeRegistration.RegisteredName() )
     {
-      impl.AddProperty( name, index, type, setFunc, getFunc );
+      iter->AddProperty( name, index, type, setFunc, getFunc );
       return true;
     }
   }
@@ -219,10 +222,9 @@ bool TypeRegistry::RegisterProperty( const std::string& objectName, const std::s
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == objectName )
+    if( iter->GetName() == objectName )
     {
-      impl.AddProperty( name, index, type, setFunc, getFunc );
+      iter->AddProperty( name, index, type, setFunc, getFunc );
       return true;
     }
   }
@@ -235,10 +237,9 @@ bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistratio
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == typeRegistration.RegisteredName() )
     {
-      impl.AddAnimatableProperty( name, index, type );
+      iter->AddAnimatableProperty( name, index, type );
       return true;
     }
   }
@@ -250,10 +251,9 @@ bool TypeRegistry::RegisterAnimatableProperty( TypeRegistration& typeRegistratio
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == typeRegistration.RegisteredName() )
     {
-      impl.AddAnimatableProperty( name, index, value );
+      iter->AddAnimatableProperty( name, index, value );
       return true;
     }
   }
@@ -265,10 +265,9 @@ bool TypeRegistry::RegisterAnimatablePropertyComponent( TypeRegistration& typeRe
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == typeRegistration.RegisteredName() )
     {
-      impl.AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
+      iter->AddAnimatablePropertyComponent( name, index, baseIndex, componentIndex );
       return true;
     }
   }
@@ -276,14 +275,13 @@ bool TypeRegistry::RegisterAnimatablePropertyComponent( TypeRegistration& typeRe
   return false;
 }
 
-bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
+bool TypeRegistry::RegisterChildProperty( const std::string& registeredType, const std::string& name, Property::Index index, Property::Type type )
 {
   for( auto&& iter : mRegistryLut )
   {
-    auto&& impl = GetImplementation( iter );
-    if( impl.GetName() == typeRegistration.RegisteredName() )
+    if( iter->GetName() == registeredType )
     {
-      impl.AddChildProperty( name, index, type );
+      iter->AddChildProperty( name, index, type );
       return true;
     }
   }
@@ -291,21 +289,23 @@ bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, co
   return false;
 }
 
+bool TypeRegistry::RegisterChildProperty( TypeRegistration& typeRegistration, const std::string& name, Property::Index index, Property::Type type )
+{
+  return RegisterChildProperty( typeRegistration.RegisteredName(), 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 )
   {
-    auto&& impl = GetImplementation( type );
-    if( impl.DoActionTo( object, actionName, properties ) )
-    {
-      done = true;
-      break;
-    }
-    type = GetTypeInfo( impl.GetBaseName() );
+    DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", type->GetName().c_str(), actionName.c_str());
   }
 
   return done;
@@ -315,21 +315,14 @@ bool TypeRegistry::ConnectSignal( BaseObject* object, ConnectionTrackerInterface
 {
   bool connected( false );
 
-  Dali::TypeInfo type = GetTypeInfo( object );
+  auto&& type = GetTypeInfo( object );
 
-  while( type )
-  {
-    auto&& impl = GetImplementation( type );
-    connected = impl.ConnectSignal( object, connectionTracker, signalName, functor );
-    if( connected )
-    {
-      break;
-    }
-    type = GetTypeInfo( impl.GetBaseName() );
-  }
+  // Connect iterates through base classes
+  connected = type->ConnectSignal( object, connectionTracker, signalName, functor );
 
   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;
   }
@@ -337,17 +330,23 @@ 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);
 
-  if(pCustom)
+  if( pCustom )
   {
     const Dali::CustomActorImpl& custom = pCustom->GetImplementation();
     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
   {