2 * Copyright (c) 2014 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;
85 TypeInfo::TypeInfo(const std::string &name, const std::string &baseTypeName, Dali::TypeInfo::CreateFunction creator)
86 : mTypeName(name), mBaseTypeName(baseTypeName), mCreate(creator)
88 DALI_ASSERT_ALWAYS(!name.empty() && "Type info construction must have a name");
89 DALI_ASSERT_ALWAYS(!baseTypeName.empty() && "Type info construction must have a base type name");
96 BaseHandle TypeInfo::CreateInstance() const
106 BaseObject& handle = ret.GetBaseObject();
107 Object *object = dynamic_cast<Internal::Object*>(&handle);
111 object->SetTypeInfo( this );
118 bool TypeInfo::DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties)
122 ActionContainer::iterator iter = find_if(mActions.begin(), mActions.end(), PairFinder<std::string, ActionPair>(actionName));
124 if( iter != mActions.end() )
126 done = (iter->second)(object, actionName, properties);
130 DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", mTypeName.c_str(), actionName.c_str());
135 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
138 done = GetImplementation(base).DoActionTo(object, actionName, properties);
143 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
150 bool TypeInfo::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
152 bool connected( false );
154 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
155 PairFinder<std::string, ConnectionPair>(signalName) );
157 if( iter != mSignalConnectors.end() )
159 connected = (iter->second)( object, connectionTracker, signalName, functor );
165 const std::string& TypeInfo::GetName() const
170 const std::string& TypeInfo::GetBaseName() const
172 return mBaseTypeName;
175 Dali::TypeInfo::CreateFunction TypeInfo::GetCreator() const
180 void TypeInfo::GetActions( Dali::TypeInfo::NameContainer& ret ) const
182 for(ActionContainer::const_iterator iter = mActions.begin(); iter != mActions.end(); ++iter)
184 ret.push_back(iter->first);
187 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
190 for(ActionContainer::const_iterator iter = GetImplementation(base).mActions.begin();
191 iter != GetImplementation(base).mActions.end(); ++iter)
193 ret.push_back(iter->first);
196 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
200 void TypeInfo::GetSignals(Dali::TypeInfo::NameContainer& ret) const
202 for(ConnectorContainer::const_iterator iter = mSignalConnectors.begin(); iter != mSignalConnectors.end(); ++iter)
204 ret.push_back(iter->first);
207 Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
210 for(ConnectorContainer::const_iterator iter = GetImplementation(base).mSignalConnectors.begin();
211 iter != GetImplementation(base).mSignalConnectors.end(); ++iter)
213 ret.push_back(iter->first);
216 base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
220 void TypeInfo::GetProperties( Dali::TypeInfo::NameContainer& ret ) const
222 Property::IndexContainer indices;
223 GetPropertyIndices(indices);
225 ret.reserve(indices.size());
227 for(Property::IndexContainer::iterator iter = indices.begin(); iter != indices.end(); ++iter)
229 const std::string& name = GetPropertyName( *iter );
232 ret.push_back( name );
236 DALI_LOG_WARNING("Property had no name\n");
241 void TypeInfo::GetPropertyIndices( Property::IndexContainer& indices ) const
243 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
246 const TypeInfo& baseImpl( GetImplementation( base ) );
247 baseImpl.GetPropertyIndices( indices );
250 if ( ! mRegisteredProperties.empty() )
252 indices.reserve( indices.size() + mRegisteredProperties.size() );
254 const RegisteredPropertyContainer::const_iterator endIter = mRegisteredProperties.end();
255 for ( RegisteredPropertyContainer::const_iterator iter = mRegisteredProperties.begin(); iter != endIter; ++iter )
257 indices.push_back( iter->first );
262 const std::string& TypeInfo::GetPropertyName( Property::Index index ) const
264 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
265 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
267 if ( iter != mRegisteredProperties.end() )
269 return iter->second.name;
272 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
275 return GetImplementation(base).GetPropertyName( index );
278 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
281 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
283 if( NULL == function)
285 DALI_LOG_WARNING("Action function is empty\n");
289 ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
290 PairFinder<std::string, ActionPair>(actionName));
292 if( iter == mActions.end() )
294 mActions.push_back( ActionPair( actionName, function ) );
298 DALI_LOG_WARNING("Action already exists in TypeRegistry Type", actionName.c_str());
303 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function )
305 if( NULL == function)
307 DALI_LOG_WARNING("Connector function is empty\n");
311 ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
312 PairFinder<std::string, ConnectionPair>(signalName) );
314 if( iter == mSignalConnectors.end() )
316 mSignalConnectors.push_back( ConnectionPair( signalName, function ) );
320 DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function", signalName.c_str());
325 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
327 // The setter can be empty as a property can be read-only.
329 if ( NULL == getFunc )
331 DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
335 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
336 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
338 if ( iter == mRegisteredProperties.end() )
340 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name ) ) );
344 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
349 void TypeInfo::AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type )
351 RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
352 PairFinder< Property::Index, RegisteredPropertyPair>(index) );
354 if ( iter == mRegisteredProperties.end() )
356 mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name ) ) );
360 DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
364 unsigned int TypeInfo::GetPropertyCount() const
366 unsigned int count( mRegisteredProperties.size() );
368 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
371 const TypeInfo& baseImpl( GetImplementation(base) );
372 count += baseImpl.mRegisteredProperties.size();
373 base = TypeRegistry::Get()->GetTypeInfo( baseImpl.mBaseTypeName );
379 Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const
381 Property::Index index = Property::INVALID_INDEX;
383 // Slow but should not be done that often
384 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
385 PropertyNameFinder< RegisteredPropertyPair >( name ) );
387 if ( iter != mRegisteredProperties.end() )
393 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
396 index = GetImplementation(base).GetPropertyIndex( name );
403 bool TypeInfo::IsPropertyWritable( Property::Index index ) const
405 bool writable( false );
407 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
408 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
410 if ( iter != mRegisteredProperties.end() )
412 if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
414 writable = true; // animatable property is writable
418 writable = iter->second.setFunc ? true : false;
423 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
426 writable = GetImplementation(base).IsPropertyWritable( index );
430 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
437 Property::Type TypeInfo::GetPropertyType( Property::Index index ) const
439 Property::Type type( Property::NONE );
441 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
442 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
444 if ( iter != mRegisteredProperties.end() )
446 type = iter->second.type;
450 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
453 type = GetImplementation(base).GetPropertyType( index );
457 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
464 void TypeInfo::SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const
466 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
467 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
468 if ( iter != mRegisteredProperties.end() )
470 if( iter->second.setFunc )
472 iter->second.setFunc( object, index, value );
477 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
480 GetImplementation(base).SetProperty( object, index, value );
484 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
489 void TypeInfo::SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const
491 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
492 PropertyNameFinder< RegisteredPropertyPair >( name ) );
493 if ( iter != mRegisteredProperties.end() )
495 DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
496 iter->second.setFunc( object, iter->first, value );
500 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
503 GetImplementation(base).SetProperty( object, name, value );
507 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
512 Property::Value TypeInfo::GetProperty( const BaseObject *object, Property::Index index ) const
514 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
515 PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
516 if( iter != mRegisteredProperties.end() )
518 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
519 return iter->second.getFunc( const_cast< BaseObject* >( object ), index );
522 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
525 return GetImplementation( base ).GetProperty( object, index );
528 DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
531 Property::Value TypeInfo::GetProperty( const BaseObject *object, const std::string& name ) const
533 RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
534 PropertyNameFinder< RegisteredPropertyPair >( name ) );
535 if( iter != mRegisteredProperties.end() )
537 // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
538 return iter->second.getFunc( const_cast< BaseObject* >( object ), iter->first );
541 Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
544 return GetImplementation( base ).GetProperty( object, name );
547 DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
550 } // namespace Internal