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;
246 GetPropertyIndices(indices);
248 ret.reserve(indices.size());
250 for(Property::IndexContainer::iterator iter = indices.begin(); iter != indices.end(); ++iter)
252 const std::string& name = GetPropertyName( *iter );
255 ret.push_back( name );
259 DALI_LOG_WARNING("Property had no name\n");
264 void TypeInfo::GetPropertyIndices( Property::IndexContainer& indices ) const
266 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
269 const TypeInfo& baseImpl( GetImplementation( base ) );
270 baseImpl.GetPropertyIndices( indices );
273 if ( ! mRegisteredProperties.empty() )
275 indices.reserve( indices.size() + mRegisteredProperties.size() );
277 const RegisteredPropertyContainer::const_iterator endIter = mRegisteredProperties.end();
278 for ( RegisteredPropertyContainer::const_iterator iter = mRegisteredProperties.begin(); iter != endIter; ++iter )
280 indices.push_back( iter->first );
285 const std::string& TypeInfo::GetPropertyName( Property::Index index ) const
287 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
288 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
290 if ( iter != mRegisteredProperties.end() )
292 return iter->second.name;
295 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
298 return GetImplementation(base).GetPropertyName( index );
301 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
304 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
306 if( NULL == function)
308 DALI_LOG_WARNING("Action function is empty\n");
312 ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
313 PairFinder<std::string, ActionPair>(actionName));
315 if( iter == mActions.end() )
317 mActions.push_back( ActionPair( actionName, function ) );
321 DALI_LOG_WARNING("Action already exists in TypeRegistry Type", actionName.c_str());
326 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function )
328 if( NULL == function)
330 DALI_LOG_WARNING("Connector function is empty\n");
334 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
335 PairFinder<std::string, ConnectionPair>(signalName) );
337 if( iter == mSignalConnectors.end() )
339 mSignalConnectors.push_back( ConnectionPair( signalName, function ) );
343 DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function", signalName.c_str());
348 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
350 // The setter can be empty as a property can be read-only.
352 if ( NULL == getFunc )
354 DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
358 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
359 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
361 if ( iter == mRegisteredProperties.end() )
363 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
367 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
372 void TypeInfo::AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type )
374 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
375 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
377 if ( iter == mRegisteredProperties.end() )
379 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
383 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
387 void TypeInfo::AddAnimatablePropertyComponent( const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
389 Property::Type type = GetPropertyType( baseIndex );
390 DALI_ASSERT_ALWAYS( ( type == Property::VECTOR2 || type == Property::VECTOR3 || type == Property::VECTOR4 ) && "Base property does not support component" );
392 bool success = false;
394 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
395 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
397 if ( iter == mRegisteredProperties.end() )
399 iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
400 PropertyComponentFinder< RegisteredPropertyPair >( baseIndex, componentIndex ) );
402 if ( iter == mRegisteredProperties.end() )
404 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name, baseIndex, componentIndex ) ) );
409 DALI_ASSERT_ALWAYS( success && "Property component already registered" );
412 unsigned int TypeInfo::GetPropertyCount() const
414 unsigned int count( mRegisteredProperties.size() );
416 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
419 const TypeInfo& baseImpl( GetImplementation(base) );
420 count += baseImpl.mRegisteredProperties.size();
421 base = TypeRegistry::Get()->GetTypeInfo( baseImpl.mBaseTypeName );
427 Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const
429 Property::Index index = Property::INVALID_INDEX;
431 // Slow but should not be done that often
432 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
433 PropertyNameFinder< RegisteredPropertyPair >( name ) );
435 if ( iter != mRegisteredProperties.end() )
441 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
444 index = GetImplementation(base).GetPropertyIndex( name );
451 Property::Index TypeInfo::GetBasePropertyIndex( Property::Index index ) const
453 Property::Index basePropertyIndex = Property::INVALID_INDEX;
455 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
456 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
458 if ( iter != mRegisteredProperties.end() )
460 basePropertyIndex = iter->second.basePropertyIndex;
464 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
467 basePropertyIndex = GetImplementation(base).GetBasePropertyIndex( index );
471 return basePropertyIndex;
474 int TypeInfo::GetComponentIndex( Property::Index index ) const
476 int componentIndex = Property::INVALID_COMPONENT_INDEX;
478 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
479 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
481 if ( iter != mRegisteredProperties.end() )
483 componentIndex = iter->second.componentIndex;
487 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
490 componentIndex = GetImplementation(base).GetComponentIndex( index );
494 return componentIndex;
497 bool TypeInfo::IsPropertyWritable( Property::Index index ) const
499 bool writable( false );
501 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
502 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
504 if ( iter != mRegisteredProperties.end() )
506 if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
508 writable = true; // animatable property is writable
512 writable = iter->second.setFunc ? true : false;
517 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
520 writable = GetImplementation(base).IsPropertyWritable( index );
524 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
531 Property::Type TypeInfo::GetPropertyType( Property::Index index ) const
533 Property::Type type( Property::NONE );
535 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
536 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
538 if ( iter != mRegisteredProperties.end() )
540 type = iter->second.type;
544 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
547 type = GetImplementation(base).GetPropertyType( index );
551 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
558 void TypeInfo::SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const
560 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
561 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
562 if ( iter != mRegisteredProperties.end() )
564 if( iter->second.setFunc )
566 iter->second.setFunc( object, index, value );
571 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
574 GetImplementation(base).SetProperty( object, index, value );
578 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
583 void TypeInfo::SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const
585 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
586 PropertyNameFinder< RegisteredPropertyPair >( name ) );
587 if ( iter != mRegisteredProperties.end() )
589 DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
590 iter->second.setFunc( object, iter->first, value );
594 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
597 GetImplementation(base).SetProperty( object, name, value );
601 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
606 Property::Value TypeInfo::GetProperty( const BaseObject *object, Property::Index index ) const
608 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
609 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
610 if( iter != mRegisteredProperties.end() )
612 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
613 return iter->second.getFunc( const_cast< BaseObject* >( object ), index );
616 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
619 return GetImplementation( base ).GetProperty( object, index );
622 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
625 Property::Value TypeInfo::GetProperty( const BaseObject *object, const std::string& name ) const
627 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
628 PropertyNameFinder< RegisteredPropertyPair >( name ) );
629 if( iter != mRegisteredProperties.end() )
631 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
632 return iter->second.getFunc( const_cast< BaseObject* >( object ), iter->first );
635 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
638 return GetImplementation( base ).GetProperty( object, name );
641 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
644 } // namespace Internal