[SRUK] Initial copy from Tizen 2.2 version
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/common/type-info-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <algorithm> // std::find_if
22 #include <string>
23 #include <dali/public-api/object/type-registry.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/debug.h>
27
28 using std::find_if;
29
30 namespace
31 {
32
33 /*
34  * Functor to find by name for vector of pairs
35  */
36 template <typename T>
37 struct PairNameListFinder
38 {
39   PairNameListFinder(const std::string &find)
40   : mFind(find)
41   {
42   }
43
44   bool operator()(T &p)
45   {
46     return p.first == mFind;
47   }
48
49 private:
50
51   const std::string& mFind;
52 };
53
54 } // namespace anon
55
56 namespace Dali
57 {
58
59 namespace Internal
60 {
61
62 TypeInfo::TypeInfo(const std::string &name, const std::string &baseTypeName, Dali::TypeInfo::CreateFunction creator)
63   : mTypeName(name), mBaseTypeName(baseTypeName), mCreate(creator)
64 {
65   DALI_ASSERT_ALWAYS(!name.empty() && "Type info construction must have a name");
66   DALI_ASSERT_ALWAYS(!baseTypeName.empty() && "Type info construction must have a base type name");
67 }
68
69 TypeInfo::~TypeInfo()
70 {
71 }
72
73 BaseHandle TypeInfo::CreateInstance()
74 {
75   BaseHandle ret;
76
77   if(mCreate)
78   {
79     ret = mCreate();
80   }
81   return ret;
82 }
83
84 bool TypeInfo::DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties)
85 {
86   bool done = false;
87
88   ActionContainer::iterator iter = find_if(mActions.begin(), mActions.end(), PairNameListFinder<ActionPair>(actionName));
89
90   if( iter != mActions.end() )
91   {
92     done = (iter->second)(object, actionName, properties);
93   }
94   else
95   {
96     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", mTypeName.c_str(), actionName.c_str());
97   }
98
99   if(!done)
100   {
101     Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
102     while( base )
103     {
104       done = GetImplementation(base).DoActionTo(object, actionName, properties);
105       if( done )
106       {
107         break;
108       }
109       base =  Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
110     }
111   }
112
113   return done;
114 }
115
116 bool TypeInfo::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
117 {
118   bool connected( false );
119
120   ConnectorContainerV2::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
121                                                  PairNameListFinder<ConnectionPairV2>(signalName) );
122
123   if( iter != mSignalConnectors.end() )
124   {
125     connected = (iter->second)( object, connectionTracker, signalName, functor );
126   }
127
128   return connected;
129 }
130
131 const std::string& TypeInfo::GetName()
132 {
133   return mTypeName;
134 }
135
136 const std::string& TypeInfo::GetBaseName()
137 {
138   return mBaseTypeName;
139 }
140
141 Dali::TypeInfo::CreateFunction TypeInfo::GetCreator()
142 {
143   return mCreate;
144 }
145
146 Dali::TypeInfo::NameContainer TypeInfo::GetActions()
147 {
148   Dali::TypeInfo::NameContainer ret;
149
150   for(ActionContainer::iterator iter = mActions.begin(); iter != mActions.end(); ++iter)
151   {
152     ret.push_back(iter->first);
153   }
154
155   Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
156   while( base )
157   {
158     for(ActionContainer::iterator iter = GetImplementation(base).mActions.begin();
159         iter != GetImplementation(base).mActions.end(); ++iter)
160     {
161       ret.push_back(iter->first);
162     }
163
164     base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
165   }
166
167   return ret;
168 }
169
170 Dali::TypeInfo::NameContainer TypeInfo::GetSignals()
171 {
172   Dali::TypeInfo::NameContainer ret;
173
174   for(ConnectorContainerV2::iterator iter = mSignalConnectors.begin(); iter != mSignalConnectors.end(); ++iter)
175   {
176     ret.push_back(iter->first);
177   }
178
179   Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
180   while( base )
181   {
182     for(ConnectorContainerV2::iterator iter = GetImplementation(base).mSignalConnectors.begin();
183         iter != GetImplementation(base).mSignalConnectors.end(); ++iter)
184     {
185       ret.push_back(iter->first);
186     }
187
188     base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
189   }
190
191   return ret;
192 }
193
194 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
195 {
196   if( NULL == function)
197   {
198     DALI_LOG_WARNING("Action function is empty\n");
199   }
200   else
201   {
202     ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
203                                                   PairNameListFinder<ActionPair>(actionName));
204
205     if( iter == mActions.end() )
206     {
207       mActions.push_back( ActionPair( actionName, function ) );
208     }
209     else
210     {
211       DALI_LOG_WARNING("Action already exists in TypeRegistry Type", actionName.c_str());
212     }
213   }
214 }
215
216 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunctionV2 function )
217 {
218   if( NULL == function)
219   {
220     DALI_LOG_WARNING("Connector function is empty\n");
221   }
222   else
223   {
224     ConnectorContainerV2::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
225                                                    PairNameListFinder<ConnectionPairV2>(signalName) );
226
227     if( iter == mSignalConnectors.end() )
228     {
229       mSignalConnectors.push_back( ConnectionPairV2( signalName, function ) );
230     }
231     else
232     {
233       DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function", signalName.c_str());
234     }
235   }
236 }
237
238 } // namespace Internal
239
240 } // namespace Dali