2 * Copyright (c) 2015 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), 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()
119 BaseHandle TypeInfo::CreateInstance() const
129 BaseObject& handle = ret.GetBaseObject();
130 Object *object = dynamic_cast<Internal::Object*>(&handle);
134 object->SetTypeInfo( this );
141 bool TypeInfo::DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties)
145 ActionContainer::iterator iter = find_if(mActions.begin(), mActions.end(), PairFinder<std::string, ActionPair>(actionName));
147 if( iter != mActions.end() )
149 done = (iter->second)(object, actionName, properties);
153 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", mTypeName.c_str(), actionName.c_str());
158 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
161 done = GetImplementation(base).DoActionTo(object, actionName, properties);
166 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
173 bool TypeInfo::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
175 bool connected( false );
177 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
178 PairFinder<std::string, ConnectionPair>(signalName) );
180 if( iter != mSignalConnectors.end() )
182 connected = (iter->second)( object, connectionTracker, signalName, functor );
188 const std::string& TypeInfo::GetName() const
193 const std::string& TypeInfo::GetBaseName() const
195 return mBaseTypeName;
198 Dali::TypeInfo::CreateFunction TypeInfo::GetCreator() const
203 void TypeInfo::GetActions( Dali::TypeInfo::NameContainer& ret ) const
205 for(ActionContainer::const_iterator iter = mActions.begin(); iter != mActions.end(); ++iter)
207 ret.push_back(iter->first);
210 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
213 for(ActionContainer::const_iterator iter = GetImplementation(base).mActions.begin();
214 iter != GetImplementation(base).mActions.end(); ++iter)
216 ret.push_back(iter->first);
219 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
223 void TypeInfo::GetSignals(Dali::TypeInfo::NameContainer& ret) const
225 for(ConnectorContainer::const_iterator iter = mSignalConnectors.begin(); iter != mSignalConnectors.end(); ++iter)
227 ret.push_back(iter->first);
230 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
233 for(ConnectorContainer::const_iterator iter = GetImplementation(base).mSignalConnectors.begin();
234 iter != GetImplementation(base).mSignalConnectors.end(); ++iter)
236 ret.push_back(iter->first);
239 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
243 void TypeInfo::GetProperties( Dali::TypeInfo::NameContainer& ret ) const
245 Property::IndexContainer indices;
247 GetPropertyIndices(indices);
249 ret.reserve(indices.size());
251 for(Property::IndexContainer::iterator iter = indices.begin(); iter != indices.end(); ++iter)
253 const std::string& name = GetPropertyName( *iter );
256 ret.push_back( name );
260 DALI_LOG_WARNING("Property had no name\n");
265 void TypeInfo::GetPropertyIndices( Property::IndexContainer& indices ) const
267 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
270 const TypeInfo& baseImpl( GetImplementation( base ) );
271 baseImpl.GetPropertyIndices( indices );
274 if ( ! mRegisteredProperties.empty() )
276 indices.reserve( indices.size() + mRegisteredProperties.size() );
278 const RegisteredPropertyContainer::const_iterator endIter = mRegisteredProperties.end();
279 for ( RegisteredPropertyContainer::const_iterator iter = mRegisteredProperties.begin(); iter != endIter; ++iter )
281 indices.push_back( iter->first );
286 const std::string& TypeInfo::GetPropertyName( Property::Index index ) const
288 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
289 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
291 if ( iter != mRegisteredProperties.end() )
293 return iter->second.name;
296 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
299 return GetImplementation(base).GetPropertyName( index );
302 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
305 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
307 if( NULL == function)
309 DALI_LOG_WARNING("Action function is empty\n");
313 ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
314 PairFinder<std::string, ActionPair>(actionName));
316 if( iter == mActions.end() )
318 mActions.push_back( ActionPair( actionName, function ) );
322 DALI_LOG_WARNING("Action already exists in TypeRegistry Type", actionName.c_str());
327 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function )
329 if( NULL == function)
331 DALI_LOG_WARNING("Connector function is empty\n");
335 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
336 PairFinder<std::string, ConnectionPair>(signalName) );
338 if( iter == mSignalConnectors.end() )
340 mSignalConnectors.push_back( ConnectionPair( signalName, function ) );
344 DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function", signalName.c_str());
349 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
351 // The setter can be empty as a property can be read-only.
353 if ( NULL == getFunc )
355 DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
359 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
360 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
362 if ( iter == mRegisteredProperties.end() )
364 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
368 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
373 void TypeInfo::AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type )
375 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
376 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
378 if ( iter == mRegisteredProperties.end() )
380 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
384 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
388 void TypeInfo::AddAnimatablePropertyComponent( const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
390 Property::Type type = GetPropertyType( baseIndex );
391 DALI_ASSERT_ALWAYS( ( type == Property::VECTOR2 || type == Property::VECTOR3 || type == Property::VECTOR4 ) && "Base property does not support component" );
393 bool success = false;
395 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
396 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
398 if ( iter == mRegisteredProperties.end() )
400 iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
401 PropertyComponentFinder< RegisteredPropertyPair >( baseIndex, componentIndex ) );
403 if ( iter == mRegisteredProperties.end() )
405 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name, baseIndex, componentIndex ) ) );
410 DALI_ASSERT_ALWAYS( success && "Property component already registered" );
413 unsigned int TypeInfo::GetPropertyCount() const
415 unsigned int count( mRegisteredProperties.size() );
417 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
420 const TypeInfo& baseImpl( GetImplementation(base) );
421 count += baseImpl.mRegisteredProperties.size();
422 base = TypeRegistry::Get()->GetTypeInfo( baseImpl.mBaseTypeName );
428 Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const
430 Property::Index index = Property::INVALID_INDEX;
432 // Slow but should not be done that often
433 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
434 PropertyNameFinder< RegisteredPropertyPair >( name ) );
436 if ( iter != mRegisteredProperties.end() )
442 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
445 index = GetImplementation(base).GetPropertyIndex( name );
452 Property::Index TypeInfo::GetBasePropertyIndex( Property::Index index ) const
454 Property::Index basePropertyIndex = Property::INVALID_INDEX;
456 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
457 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
459 if ( iter != mRegisteredProperties.end() )
461 basePropertyIndex = iter->second.basePropertyIndex;
465 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
468 basePropertyIndex = GetImplementation(base).GetBasePropertyIndex( index );
472 return basePropertyIndex;
475 int TypeInfo::GetComponentIndex( Property::Index index ) const
477 int componentIndex = Property::INVALID_COMPONENT_INDEX;
479 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
480 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
482 if ( iter != mRegisteredProperties.end() )
484 componentIndex = iter->second.componentIndex;
488 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
491 componentIndex = GetImplementation(base).GetComponentIndex( index );
495 return componentIndex;
498 bool TypeInfo::IsPropertyWritable( Property::Index index ) const
500 bool writable( false );
502 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
503 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
505 if ( iter != mRegisteredProperties.end() )
507 if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
509 writable = true; // animatable property is writable
513 writable = iter->second.setFunc ? true : false;
518 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
521 writable = GetImplementation(base).IsPropertyWritable( index );
525 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
532 Property::Type TypeInfo::GetPropertyType( Property::Index index ) const
534 Property::Type type( Property::NONE );
536 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
537 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
539 if ( iter != mRegisteredProperties.end() )
541 type = iter->second.type;
545 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
548 type = GetImplementation(base).GetPropertyType( index );
552 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
559 void TypeInfo::SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const
561 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
562 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
563 if ( iter != mRegisteredProperties.end() )
565 if( iter->second.setFunc )
567 iter->second.setFunc( object, index, value );
572 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
575 GetImplementation(base).SetProperty( object, index, value );
579 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
584 void TypeInfo::SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const
586 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
587 PropertyNameFinder< RegisteredPropertyPair >( name ) );
588 if ( iter != mRegisteredProperties.end() )
590 DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
591 iter->second.setFunc( object, iter->first, value );
595 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
598 GetImplementation(base).SetProperty( object, name, value );
602 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
607 Property::Value TypeInfo::GetProperty( const BaseObject *object, Property::Index index ) const
609 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
610 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
611 if( iter != mRegisteredProperties.end() )
613 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
614 return iter->second.getFunc( const_cast< BaseObject* >( object ), index );
617 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
620 return GetImplementation( base ).GetProperty( object, index );
623 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
626 Property::Value TypeInfo::GetProperty( const BaseObject *object, const std::string& name ) const
628 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
629 PropertyNameFinder< RegisteredPropertyPair >( name ) );
630 if( iter != mRegisteredProperties.end() )
632 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
633 return iter->second.getFunc( const_cast< BaseObject* >( object ), iter->first );
636 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
639 return GetImplementation( base ).GetProperty( object, name );
642 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
645 } // namespace Internal