2 * Copyright (c) 2017 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-info-impl.h>
22 #include <algorithm> // std::find_if
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/event/common/type-registry-impl.h>
28 #include <dali/internal/event/common/object-impl.h>
36 * Functor to find by given type for vector of pairs
38 template <typename S, typename T>
41 PairFinder(const S& find)
46 bool operator()(const T& p) const
48 return p.first == mFind;
57 * Functor to find a matching property name
60 struct PropertyNameFinder
62 PropertyNameFinder( const std::string& find )
67 bool operator()(const T &p) const
69 return p.second.name == mFind;
74 const std::string& mFind;
78 * Functor to find a matching property component index
81 struct PropertyComponentFinder
83 PropertyComponentFinder( Dali::Property::Index basePropertyIndex, const int find )
84 : mBasePropertyIndex( basePropertyIndex ),
89 bool operator()(const T &p) const
91 return ( p.second.basePropertyIndex == mBasePropertyIndex && p.second.componentIndex == mFind );
96 Dali::Property::Index mBasePropertyIndex;
108 TypeInfo::TypeInfo(const std::string &name, const std::string &baseTypeName, Dali::TypeInfo::CreateFunction creator)
109 : mTypeName(name), mBaseTypeName(baseTypeName), mCSharpType(false), mCreate(creator)
111 DALI_ASSERT_ALWAYS(!name.empty() && "Type info construction must have a name");
112 DALI_ASSERT_ALWAYS(!baseTypeName.empty() && "Type info construction must have a base type name");
115 TypeInfo::TypeInfo(const std::string &name, const std::string &baseTypeName, Dali::CSharpTypeInfo::CreateFunction creator)
116 : mTypeName(name), mBaseTypeName(baseTypeName), mCSharpType(true), mCSharpCreate(creator)
118 DALI_ASSERT_ALWAYS(!name.empty() && "Type info construction must have a name");
119 DALI_ASSERT_ALWAYS(!baseTypeName.empty() && "Type info construction must have a base type name");
122 TypeInfo::~TypeInfo()
126 BaseHandle TypeInfo::CreateInstance() const
134 // CSharp currently only registers one create function for all custom controls
135 // it uses the type name to decide which one to create
136 ret = *mCSharpCreate( mTypeName.c_str() );
145 BaseObject& handle = ret.GetBaseObject();
146 Object *object = dynamic_cast<Internal::Object*>(&handle);
150 object->SetTypeInfo( this );
157 bool TypeInfo::DoActionTo(BaseObject *object, const std::string &actionName, const Property::Map &properties)
161 ActionContainer::iterator iter = find_if(mActions.begin(), mActions.end(), PairFinder<std::string, ActionPair>(actionName));
163 if( iter != mActions.end() )
165 done = (iter->second)(object, actionName, properties);
169 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", mTypeName.c_str(), actionName.c_str());
174 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
177 done = GetImplementation(base).DoActionTo(object, actionName, properties);
182 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
189 bool TypeInfo::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
191 bool connected( false );
193 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
194 PairFinder<std::string, ConnectionPair>(signalName) );
196 if( iter != mSignalConnectors.end() )
198 connected = (iter->second)( object, connectionTracker, signalName, functor );
204 const std::string& TypeInfo::GetName() const
209 const std::string& TypeInfo::GetBaseName() const
211 return mBaseTypeName;
214 Dali::TypeInfo::CreateFunction TypeInfo::GetCreator() const
219 size_t TypeInfo::GetActionCount() const
221 size_t count = mActions.size();
223 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
226 count += GetImplementation(base).mActions.size();
227 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
233 std::string TypeInfo::GetActionName(size_t index) const
237 if( index < mActions.size() )
239 name = mActions[index].first;
243 size_t count = mActions.size();
245 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
248 size_t baseCount = GetImplementation(base).mActions.size();
250 if( index < count + baseCount )
252 name = GetImplementation(base).mActions[ index - count ].first;
258 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
265 size_t TypeInfo::GetSignalCount() const
267 size_t count = mSignalConnectors.size();
269 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
272 count += GetImplementation(base).mSignalConnectors.size();
273 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
279 std::string TypeInfo::GetSignalName(size_t index) const
283 if( index < mSignalConnectors.size() )
285 name = mSignalConnectors[index].first;
289 size_t count = mSignalConnectors.size();
291 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
294 size_t baseCount = GetImplementation(base).mSignalConnectors.size();
296 if( index < count + baseCount )
298 name = GetImplementation(base).mSignalConnectors[ index - count ].first;
304 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
311 void TypeInfo::GetPropertyIndices( Property::IndexContainer& indices ) const
313 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
316 const TypeInfo& baseImpl( GetImplementation( base ) );
317 baseImpl.GetPropertyIndices( indices );
320 if ( ! mRegisteredProperties.empty() )
322 indices.Reserve( indices.Size() + mRegisteredProperties.size() );
324 const RegisteredPropertyContainer::const_iterator endIter = mRegisteredProperties.end();
325 for ( RegisteredPropertyContainer::const_iterator iter = mRegisteredProperties.begin(); iter != endIter; ++iter )
327 indices.PushBack( iter->first );
332 const std::string& TypeInfo::GetPropertyName( Property::Index index ) const
334 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
335 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
337 if ( iter != mRegisteredProperties.end() )
339 return iter->second.name;
342 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
345 return GetImplementation(base).GetPropertyName( index );
348 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
351 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
353 if( NULL == function)
355 DALI_LOG_WARNING("Action function is empty\n");
359 ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
360 PairFinder<std::string, ActionPair>(actionName));
362 if( iter == mActions.end() )
364 mActions.push_back( ActionPair( actionName, function ) );
368 DALI_LOG_WARNING("Action already exists in TypeRegistry Type\n", actionName.c_str());
373 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function )
375 if( NULL == function)
377 DALI_LOG_WARNING("Connector function is empty\n");
381 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
382 PairFinder<std::string, ConnectionPair>(signalName) );
384 if( iter == mSignalConnectors.end() )
386 mSignalConnectors.push_back( ConnectionPair( signalName, function ) );
390 DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function\n", signalName.c_str());
395 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
397 // The setter can be empty as a property can be read-only.
399 if ( NULL == getFunc )
401 DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
405 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
406 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
408 if ( iter == mRegisteredProperties.end() )
410 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
414 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
419 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc)
422 // The setter can be empty as a property can be read-only.
424 if ( NULL == getFunc )
426 DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
430 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
431 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
433 if ( iter == mRegisteredProperties.end() )
435 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
439 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
446 void TypeInfo::AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type )
448 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
449 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
451 if ( iter == mRegisteredProperties.end() )
453 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
457 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
461 void TypeInfo::AddAnimatableProperty( const std::string& name, Property::Index index, const Property::Value& defaultValue )
463 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
464 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
466 if ( iter == mRegisteredProperties.end() )
468 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( defaultValue.GetType(), name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
469 mPropertyDefaultValues.push_back( PropertyDefaultValuePair( index, defaultValue ) );
473 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
477 void TypeInfo::AddAnimatablePropertyComponent( const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
479 Property::Type type = GetPropertyType( baseIndex );
480 DALI_ASSERT_ALWAYS( ( type == Property::VECTOR2 || type == Property::VECTOR3 || type == Property::VECTOR4 ) && "Base property does not support component" );
482 bool success = false;
484 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
485 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
487 if ( iter == mRegisteredProperties.end() )
489 iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
490 PropertyComponentFinder< RegisteredPropertyPair >( baseIndex, componentIndex ) );
492 if ( iter == mRegisteredProperties.end() )
494 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, name, baseIndex, componentIndex ) ) );
499 DALI_ASSERT_ALWAYS( success && "Property component already registered" );
502 void TypeInfo::AddChildProperty( const std::string& name, Property::Index index, Property::Type type )
504 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredChildProperties.begin(), mRegisteredChildProperties.end(),
505 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
507 if ( iter == mRegisteredChildProperties.end() )
509 mRegisteredChildProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
513 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
517 size_t TypeInfo::GetPropertyCount() const
519 size_t count( mRegisteredProperties.size() );
521 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
524 const TypeInfo& baseImpl( GetImplementation(base) );
525 count += baseImpl.mRegisteredProperties.size();
526 base = TypeRegistry::Get()->GetTypeInfo( baseImpl.mBaseTypeName );
532 Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const
534 Property::Index index = Property::INVALID_INDEX;
536 // Slow but should not be done that often
537 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
538 PropertyNameFinder< RegisteredPropertyPair >( name ) );
540 if ( iter != mRegisteredProperties.end() )
546 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
549 index = GetImplementation(base).GetPropertyIndex( name );
556 Property::Index TypeInfo::GetBasePropertyIndex( Property::Index index ) const
558 Property::Index basePropertyIndex = Property::INVALID_INDEX;
560 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
561 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
563 if ( iter != mRegisteredProperties.end() )
565 basePropertyIndex = iter->second.basePropertyIndex;
569 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
572 basePropertyIndex = GetImplementation(base).GetBasePropertyIndex( index );
576 return basePropertyIndex;
579 int TypeInfo::GetComponentIndex( Property::Index index ) const
581 int componentIndex = Property::INVALID_COMPONENT_INDEX;
583 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
584 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
586 if ( iter != mRegisteredProperties.end() )
588 componentIndex = iter->second.componentIndex;
592 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
595 componentIndex = GetImplementation(base).GetComponentIndex( index );
599 return componentIndex;
602 Property::Index TypeInfo::GetChildPropertyIndex( const std::string& name ) const
604 Property::Index index = Property::INVALID_INDEX;
606 // Slow but should not be done that often
607 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredChildProperties.begin(), mRegisteredChildProperties.end(),
608 PropertyNameFinder< RegisteredPropertyPair >( name ) );
610 if ( iter != mRegisteredChildProperties.end() )
616 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
619 index = GetImplementation(base).GetChildPropertyIndex( name );
626 const std::string& TypeInfo::GetChildPropertyName( Property::Index index ) const
628 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredChildProperties.begin(), mRegisteredChildProperties.end(),
629 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
631 if ( iter != mRegisteredChildProperties.end() )
633 return iter->second.name;
636 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
639 return GetImplementation(base).GetChildPropertyName( index );
642 DALI_LOG_WARNING("Cannot find property index");
644 static std::string emptyString;
648 Property::Type TypeInfo::GetChildPropertyType( Property::Index index ) const
650 Property::Type type( Property::NONE );
652 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredChildProperties.begin(), mRegisteredChildProperties.end(),
653 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
655 if ( iter != mRegisteredChildProperties.end() )
657 type = iter->second.type;
661 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
664 type = GetImplementation(base).GetChildPropertyType( index );
668 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
675 bool TypeInfo::IsPropertyWritable( Property::Index index ) const
677 bool writable( false );
679 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
680 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
682 if ( iter != mRegisteredProperties.end() )
684 if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
686 writable = true; // animatable property is writable
690 writable = iter->second.setFunc ? true : false;
695 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
698 writable = GetImplementation(base).IsPropertyWritable( index );
702 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
709 Property::Type TypeInfo::GetPropertyType( Property::Index index ) const
711 Property::Type type( Property::NONE );
713 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
714 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
716 if ( iter != mRegisteredProperties.end() )
718 if( iter->second.componentIndex == Property::INVALID_COMPONENT_INDEX )
720 type = iter->second.type;
724 // If component index is set, then we should return FLOAT
725 type = Property::FLOAT;
730 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
733 type = GetImplementation(base).GetPropertyType( index );
737 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
744 Property::Value TypeInfo::GetPropertyDefaultValue( Property::Index index ) const
746 PropertyDefaultValueContainer::const_iterator iter = find_if( mPropertyDefaultValues.begin(), mPropertyDefaultValues.end(),
747 PairFinder< Property::Index, PropertyDefaultValuePair >( index ) );
748 if( iter != mPropertyDefaultValues.end() )
754 return Property::Value( GetPropertyType( index ) );
758 void TypeInfo::SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const
760 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
761 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
762 if ( iter != mRegisteredProperties.end() )
764 if( iter->second.setFunc )
768 // CSharp wants a property name not an index
769 const std::string& name = (iter->second).name;
771 iter->second.cSharpSetFunc( object,name.c_str(), const_cast< Property::Value* >(&value) );
775 iter->second.setFunc( object, index, value );
781 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
784 GetImplementation(base).SetProperty( object, index, value );
788 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
793 void TypeInfo::SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const
795 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
796 PropertyNameFinder< RegisteredPropertyPair >( name ) );
797 if ( iter != mRegisteredProperties.end() )
799 DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
803 // CSharp wants a property name not an index
804 iter->second.cSharpSetFunc( object,name.c_str(), const_cast< Property::Value* >(&value ));
808 iter->second.setFunc( object, iter->first, value );
813 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
816 GetImplementation(base).SetProperty( object, name, value );
820 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
825 Property::Value TypeInfo::GetProperty( const BaseObject *object, Property::Index index ) const
827 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
828 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
829 if( iter != mRegisteredProperties.end() )
831 if( mCSharpType ) // using csharp property get which returns a pointer to a Property::Value
833 // CSharp wants a property name not an index
834 // CSharp callback can't return an object by value, it can only return a pointer
835 // CSharp has ownership of the pointer contents, which is fine because we are returning by from this function by value
836 const std::string& name = (iter->second).name;
838 return *( iter->second.cSharpGetFunc( const_cast< BaseObject* >( object ), name.c_str()) );
843 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
844 return iter->second.getFunc( const_cast< BaseObject* >( object ), index );
848 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
851 return GetImplementation( base ).GetProperty( object, index );
854 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
857 Property::Value TypeInfo::GetProperty( const BaseObject *object, const std::string& name ) const
859 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
860 PropertyNameFinder< RegisteredPropertyPair >( name ) );
864 if( iter != mRegisteredProperties.end() )
866 if( mCSharpType ) // using csharp property get which returns a pointer to a Property::Value
868 // CSharp wants a property name not an index
869 // CSharp callback can't return an object by value, it can only return a pointer
870 // CSharp has ownership of the pointer contents, which is fine because we are returning by from this function by value
871 return *( iter->second.cSharpGetFunc( const_cast< BaseObject* >( object ), name.c_str() ));
876 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
877 return iter->second.getFunc( const_cast< BaseObject* >( object ), iter->first );
881 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
884 return GetImplementation( base ).GetProperty( object, name );
887 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
890 } // namespace Internal