[dali_1.0.9] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-info-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/type-info-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm> // std::find_if
23 #include <string>
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/event/common/type-registry-impl.h>
28 #include <dali/internal/event/common/proxy-object.h>
29
30 using std::find_if;
31
32 namespace
33 {
34
35 /*
36  * Functor to find by given type for vector of pairs
37  */
38 template <typename S, typename T>
39 struct PairFinder
40 {
41   PairFinder(const S& find)
42   : mFind(find)
43   {
44   }
45
46   bool operator()(const T& p) const
47   {
48     return p.first == mFind;
49   }
50
51 private:
52
53   const S& mFind;
54 };
55
56 /**
57  * Functor to find a matching property name
58  */
59 template <typename T>
60 struct PropertyNameFinder
61 {
62   PropertyNameFinder( const std::string& find )
63   : mFind( find )
64   {
65   }
66
67   bool operator()(const T &p) const
68   {
69     return p.second.name == mFind;
70   }
71
72 private:
73
74   const std::string& mFind;
75 };
76
77 } // namespace anon
78
79 namespace Dali
80 {
81
82 namespace Internal
83 {
84
85 TypeInfo::TypeInfo(const std::string &name, const std::string &baseTypeName, Dali::TypeInfo::CreateFunction creator)
86   : mTypeName(name), mBaseTypeName(baseTypeName), mCreate(creator)
87 {
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");
90 }
91
92 TypeInfo::~TypeInfo()
93 {
94 }
95
96 BaseHandle TypeInfo::CreateInstance() const
97 {
98   BaseHandle ret;
99
100   if(mCreate)
101   {
102     ret = mCreate();
103
104     if ( ret )
105     {
106       BaseObject& handle = ret.GetBaseObject();
107       ProxyObject *proxyObject = dynamic_cast<Internal::ProxyObject*>(&handle);
108
109       if ( proxyObject )
110       {
111         proxyObject->SetTypeInfo( this );
112       }
113     }
114   }
115   return ret;
116 }
117
118 bool TypeInfo::DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties)
119 {
120   bool done = false;
121
122   ActionContainer::iterator iter = find_if(mActions.begin(), mActions.end(), PairFinder<std::string, ActionPair>(actionName));
123
124   if( iter != mActions.end() )
125   {
126     done = (iter->second)(object, actionName, properties);
127   }
128   else
129   {
130     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", mTypeName.c_str(), actionName.c_str());
131   }
132
133   if(!done)
134   {
135     Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
136     while( base )
137     {
138       done = GetImplementation(base).DoActionTo(object, actionName, properties);
139       if( done )
140       {
141         break;
142       }
143       base =  Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
144     }
145   }
146
147   return done;
148 }
149
150 bool TypeInfo::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
151 {
152   bool connected( false );
153
154   ConnectorContainerV2::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
155                                                  PairFinder<std::string, ConnectionPairV2>(signalName) );
156
157   if( iter != mSignalConnectors.end() )
158   {
159     connected = (iter->second)( object, connectionTracker, signalName, functor );
160   }
161
162   return connected;
163 }
164
165 const std::string& TypeInfo::GetName() const
166 {
167   return mTypeName;
168 }
169
170 const std::string& TypeInfo::GetBaseName() const
171 {
172   return mBaseTypeName;
173 }
174
175 Dali::TypeInfo::CreateFunction TypeInfo::GetCreator() const
176 {
177   return mCreate;
178 }
179
180 void TypeInfo::GetActions( Dali::TypeInfo::NameContainer& ret ) const
181 {
182   for(ActionContainer::const_iterator iter = mActions.begin(); iter != mActions.end(); ++iter)
183   {
184     ret.push_back(iter->first);
185   }
186
187   Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
188   while( base )
189   {
190     for(ActionContainer::const_iterator iter = GetImplementation(base).mActions.begin();
191         iter != GetImplementation(base).mActions.end(); ++iter)
192     {
193       ret.push_back(iter->first);
194     }
195
196     base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
197   }
198 }
199
200 void TypeInfo::GetSignals(Dali::TypeInfo::NameContainer& ret) const
201 {
202   for(ConnectorContainerV2::const_iterator iter = mSignalConnectors.begin(); iter != mSignalConnectors.end(); ++iter)
203   {
204     ret.push_back(iter->first);
205   }
206
207   Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
208   while( base )
209   {
210     for(ConnectorContainerV2::const_iterator iter = GetImplementation(base).mSignalConnectors.begin();
211         iter != GetImplementation(base).mSignalConnectors.end(); ++iter)
212     {
213       ret.push_back(iter->first);
214     }
215
216     base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
217   }
218 }
219
220 void TypeInfo::GetProperties( Dali::TypeInfo::NameContainer& ret ) const
221 {
222   Property::IndexContainer indices;
223
224   GetPropertyIndices(indices);
225
226   ret.reserve(indices.size());
227
228   for(Property::IndexContainer::iterator iter = indices.begin(); iter != indices.end(); ++iter)
229   {
230     const std::string& name = GetPropertyName( *iter );
231     if(name.size())
232     {
233       ret.push_back( name );
234     }
235     else
236     {
237       DALI_LOG_WARNING("Property had no name\n");
238     }
239   }
240 }
241
242 void TypeInfo::GetPropertyIndices( Property::IndexContainer& indices ) const
243 {
244   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
245   if ( base )
246   {
247     const TypeInfo& baseImpl( GetImplementation( base ) );
248     baseImpl.GetPropertyIndices( indices );
249   }
250
251   if ( ! mRegisteredProperties.empty() )
252   {
253     indices.reserve( indices.size() + mRegisteredProperties.size() );
254
255     const RegisteredPropertyContainer::const_iterator endIter = mRegisteredProperties.end();
256     for ( RegisteredPropertyContainer::const_iterator iter = mRegisteredProperties.begin(); iter != endIter; ++iter )
257     {
258       indices.push_back( iter->first );
259     }
260   }
261 }
262
263 const std::string& TypeInfo::GetPropertyName( Property::Index index ) const
264 {
265   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
266                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
267
268   if ( iter != mRegisteredProperties.end() )
269   {
270     return iter->second.name;
271   }
272
273   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
274   if ( base )
275   {
276     return GetImplementation(base).GetPropertyName( index );
277   }
278
279   DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as ProxyObject
280
281   // Property not found, return reference to invalid property string.
282   static const std::string EMPTY_PROPERTY_NAME;
283   return EMPTY_PROPERTY_NAME;
284 }
285
286 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
287 {
288   if( NULL == function)
289   {
290     DALI_LOG_WARNING("Action function is empty\n");
291   }
292   else
293   {
294     ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
295                                                   PairFinder<std::string, ActionPair>(actionName));
296
297     if( iter == mActions.end() )
298     {
299       mActions.push_back( ActionPair( actionName, function ) );
300     }
301     else
302     {
303       DALI_LOG_WARNING("Action already exists in TypeRegistry Type", actionName.c_str());
304     }
305   }
306 }
307
308 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunctionV2 function )
309 {
310   if( NULL == function)
311   {
312     DALI_LOG_WARNING("Connector function is empty\n");
313   }
314   else
315   {
316     ConnectorContainerV2::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
317                                                    PairFinder<std::string, ConnectionPairV2>(signalName) );
318
319     if( iter == mSignalConnectors.end() )
320     {
321       mSignalConnectors.push_back( ConnectionPairV2( signalName, function ) );
322     }
323     else
324     {
325       DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function", signalName.c_str());
326     }
327   }
328 }
329
330 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
331 {
332   // The setter can be empty as a property can be read-only.
333
334   if ( NULL == getFunc )
335   {
336     DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
337   }
338   else
339   {
340     RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
341                                                           PairFinder< Property::Index, RegisteredPropertyPair>(index) );
342
343     if ( iter == mRegisteredProperties.end() )
344     {
345       mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name ) ) );
346     }
347     else
348     {
349       DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
350     }
351   }
352 }
353
354 unsigned int TypeInfo::GetPropertyCount() const
355 {
356   unsigned int count( mRegisteredProperties.size() );
357
358   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
359   while ( base )
360   {
361     const TypeInfo& baseImpl( GetImplementation(base) );
362     count += baseImpl.mRegisteredProperties.size();
363     base = TypeRegistry::Get()->GetTypeInfo( baseImpl.mBaseTypeName );
364   }
365
366   return count;
367 }
368
369 Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const
370 {
371   Property::Index index = Property::INVALID_INDEX;
372
373   // Slow but should not be done that often
374   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
375                                                           PropertyNameFinder< RegisteredPropertyPair >( name ) );
376
377   if ( iter != mRegisteredProperties.end() )
378   {
379     index = iter->first;
380   }
381   else
382   {
383     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
384     if ( base )
385     {
386       index = GetImplementation(base).GetPropertyIndex( name );
387     }
388   }
389
390   return index;
391 }
392
393 bool TypeInfo::IsPropertyWritable( Property::Index index ) const
394 {
395   bool writable( false );
396
397   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
398                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
399
400   if ( iter != mRegisteredProperties.end() )
401   {
402     writable = iter->second.setFunc ? true : false;
403   }
404   else
405   {
406     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
407     if ( base )
408     {
409       writable = GetImplementation(base).IsPropertyWritable( index );
410     }
411     else
412     {
413       DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as ProxyObject
414     }
415   }
416
417   return writable;
418 }
419
420 Property::Type TypeInfo::GetPropertyType( Property::Index index ) const
421 {
422   Property::Type type( Property::NONE );
423
424   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
425                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
426
427   if ( iter != mRegisteredProperties.end() )
428   {
429     type = iter->second.type;
430   }
431   else
432   {
433     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
434     if ( base )
435     {
436       type = GetImplementation(base).GetPropertyType( index );
437     }
438     else
439     {
440       DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as ProxyObject
441     }
442   }
443
444   return type;
445 }
446
447 void TypeInfo::SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const
448 {
449   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
450                                                               PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
451   if ( iter != mRegisteredProperties.end() )
452   {
453     DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
454     iter->second.setFunc( object, index, value );
455   }
456   else
457   {
458     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
459     if ( base )
460     {
461       GetImplementation(base).SetProperty( object, index, value );
462     }
463     else
464     {
465       DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as ProxyObject
466     }
467   }
468 }
469
470 void TypeInfo::SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const
471 {
472   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
473                                                               PropertyNameFinder< RegisteredPropertyPair >( name ) );
474   if ( iter != mRegisteredProperties.end() )
475   {
476     DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
477     iter->second.setFunc( object, iter->first, value );
478   }
479   else
480   {
481     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
482     if ( base )
483     {
484       GetImplementation(base).SetProperty( object, name, value );
485     }
486     else
487     {
488       DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
489     }
490   }
491 }
492
493 Property::Value TypeInfo::GetProperty( const BaseObject *object, Property::Index index ) const
494 {
495   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
496                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
497   if( iter != mRegisteredProperties.end() )
498   {
499     // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
500     return iter->second.getFunc( const_cast< BaseObject* >( object ), index );
501   }
502
503   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
504   if ( base )
505   {
506     return GetImplementation( base ).GetProperty( object, index );
507   }
508
509   DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as ProxyObject
510
511   return Property::Value();
512 }
513
514 Property::Value TypeInfo::GetProperty( const BaseObject *object, const std::string& name ) const
515 {
516   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
517                                                           PropertyNameFinder< RegisteredPropertyPair >( name ) );
518   if( iter != mRegisteredProperties.end() )
519   {
520     // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
521     return iter->second.getFunc( const_cast< BaseObject* >( object ), iter->first );
522   }
523
524   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
525   if ( base )
526   {
527     return GetImplementation( base ).GetProperty( object, name );
528   }
529
530   DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
531
532   return Property::Value();
533 }
534
535 } // namespace Internal
536
537 } // namespace Dali