Fix assetion error in FocusManager
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / builder / builder-get-is.inl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
2 #define __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/builder/builder-declarations.h>
23
24 inline OptionalChild IsChild(const TreeNode* node, const std::string& childName)
25 {
26   if( node )
27   {
28     const TreeNode* c = node->GetChild(childName);
29     if( NULL != c )
30     {
31       return OptionalChild( *c );
32     }
33     else
34     {
35       return OptionalChild();
36     }
37   }
38   else
39   {
40     return OptionalChild();
41   }
42 }
43
44 inline OptionalChild IsChild(const TreeNode& node, const std::string& childName)
45 {
46   return IsChild(&node, childName);
47 }
48
49 inline OptionalString IsString(const OptionalChild& node)
50 {
51   if( node && (*node).GetType() == TreeNode::STRING )
52   {
53     return OptionalString((*node).GetString());
54   }
55   else
56   {
57     return OptionalString();
58   }
59 }
60 inline OptionalFloat IsFloat(const OptionalChild& node)
61 {
62   OptionalFloat ret;
63
64   if( node )
65   {
66     if( (*node).GetType() == TreeNode::FLOAT )
67     {
68       ret = (*node).GetFloat();
69     }
70     else if( (*node).GetType() == TreeNode::INTEGER )
71     {
72       // JSON has number not float/int but JsonParser discriminates.
73       // Here we don't care so we allow coercion
74       ret = static_cast<float>( (*node).GetInteger() );
75     }
76   }
77
78   return ret;
79 }
80
81 inline OptionalInteger IsInteger(const OptionalChild &node)
82 {
83   OptionalInteger ret;
84
85   if( node )
86   {
87     if( (*node).GetType() == TreeNode::INTEGER )
88     {
89       ret = (*node).GetInteger();
90     }
91     else if( (*node).GetType() == TreeNode::FLOAT )
92     {
93       ret = static_cast<int>(  (*node).GetFloat() );
94     }
95   }
96
97   return ret;
98 }
99
100 inline OptionalBoolean IsBoolean(const OptionalChild& node)
101 {
102   if( node && (*node).GetType() == TreeNode::BOOLEAN )
103   {
104     return OptionalBoolean(1 == (*node).GetInteger());
105   }
106   else
107   {
108     return OptionalBoolean();
109   }
110 }
111
112 // copy N Numbers
113 template <typename T>
114 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
115 {
116   for(int i = 0; i < N; ++i)
117   {
118     if( (*iter).second.GetType() == TreeNode::FLOAT)
119     {
120       vector[i] = (*iter).second.GetFloat();
121     }
122     else if( (*iter).second.GetType() == TreeNode::INTEGER )
123     {
124       vector[i] = static_cast<float>((*iter).second.GetInteger());
125     }
126     else
127     {
128       return false;
129     }
130     iter++;
131   }
132
133   return true;
134 }
135
136 inline OptionalVector4 IsVector4(const OptionalChild& node)
137 {
138   OptionalVector4 ret;
139
140   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
141   {
142     Dali::Vector4 v;
143     if( CopyNumbers((*node).CBegin(), 4, v) )
144     {
145       ret = OptionalVector4(v);
146     }
147   }
148
149   return ret;
150 }
151
152 inline OptionalVector3 IsVector3(const OptionalChild& node)
153 {
154   OptionalVector3 ret;
155
156   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
157   {
158     Dali::Vector3 v;
159     if( CopyNumbers((*node).CBegin(), 3, v) )
160     {
161       ret = OptionalVector3(v);
162     }
163   }
164
165   return ret;
166 }
167
168 inline OptionalVector2 IsVector2(const OptionalChild& node)
169 {
170   OptionalVector2 ret;
171
172   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
173   {
174     Dali::Vector2 v;
175     if( CopyNumbers((*node).CBegin(), 2, v) )
176     {
177       ret = OptionalVector2(v);
178     }
179   }
180
181   return ret;
182 }
183
184 inline OptionalMatrix IsMatrix(const OptionalChild &node)
185 {
186   OptionalMatrix ret;
187
188   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
189   {
190     float v[16];
191     if( CopyNumbers((*node).CBegin(), 16, v) )
192     {
193       ret = OptionalMatrix(Dali::Matrix(v));
194     }
195   }
196
197   return ret;
198 }
199
200 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
201 {
202   OptionalMatrix3 ret;
203
204   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
205   {
206     float v[9];
207     if( CopyNumbers((*node).CBegin(), 9, v) )
208     {
209       ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
210                                           v[3], v[4], v[5],
211                                           v[6], v[7], v[8] ));
212     }
213   }
214
215   return ret;
216 }
217
218 inline OptionalRect IsRect(const OptionalChild& node)
219 {
220   OptionalRect ret;
221   if(node && (*node).Size())
222   {
223     if((*node).Size() >= 4)
224     {
225       TreeNode::ConstIterator iter((*node).CBegin());
226       int v[4];
227       if( CopyNumbers((*node).CBegin(), 4, v) )
228       {
229         ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
230       }
231     }
232   }
233   return ret;
234 }
235
236 //
237 //
238 //
239 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
240 {
241   return IsString( IsChild(&parent, childName) );
242 }
243
244 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
245 {
246   return IsFloat( IsChild(&parent, childName) );
247 }
248
249 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
250 {
251   return IsInteger( IsChild(&parent, childName) );
252 }
253
254 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
255 {
256   return IsBoolean( IsChild(parent, childName) );
257 }
258
259 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
260 {
261   return IsVector4( IsChild(parent, childName) );
262 }
263
264 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
265 {
266   return IsVector3( IsChild(parent, childName) );
267 }
268
269 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
270 {
271   return IsVector2( IsChild(parent, childName) );
272 }
273
274 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
275 {
276   return IsMatrix( IsChild(parent, childName) );
277 }
278
279 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
280 {
281   return IsMatrix3( IsChild(&parent, childName) );
282 }
283
284 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
285 {
286   return IsRect( IsChild(&parent, childName) );
287 }
288
289 //
290 //
291 //
292 inline OptionalString IsString( const TreeNode& node )
293 {
294   return IsString( OptionalChild( node ) );
295 }
296
297 inline OptionalFloat IsFloat( const TreeNode& node )
298 {
299   return IsFloat( OptionalChild( node ) );
300 }
301
302 inline OptionalInteger IsInteger( const TreeNode& node )
303 {
304   return IsInteger( OptionalChild( node ) );
305 }
306
307 inline OptionalBoolean IsBoolean( const TreeNode& node )
308 {
309   return IsBoolean( OptionalChild( node ) );
310 }
311
312 inline OptionalVector4 IsVector4(const TreeNode &node )
313 {
314   return IsVector4( OptionalChild( node ) );
315 }
316
317 inline OptionalVector3 IsVector3(const TreeNode &node )
318 {
319   return IsVector3( OptionalChild( node ) );
320 }
321
322 inline OptionalVector2 IsVector2(const TreeNode &node )
323 {
324   return IsVector2( OptionalChild( node ) );
325 }
326
327 inline OptionalMatrix IsMatrix(const TreeNode &node )
328 {
329   return IsMatrix( OptionalChild( node ) );
330 }
331
332 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
333 {
334   return IsMatrix3( OptionalChild( node ) );
335 }
336
337 inline OptionalRect IsRect(const TreeNode &node )
338 {
339   return IsRect( OptionalChild( node ) );
340 }
341
342 //
343 //
344 //
345 inline Dali::Vector4 GetVector4(const TreeNode &child)
346 {
347   OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
348   DALI_ASSERT_ALWAYS(v);
349   return *v;
350 }
351
352 inline Dali::Vector3 GetVector3(const TreeNode &child)
353 {
354   OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
355   DALI_ASSERT_ALWAYS(v);
356   return *v;
357 }
358
359 inline Dali::Vector2 GetVector2(const TreeNode &child)
360 {
361   OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
362   DALI_ASSERT_ALWAYS(v);
363   return *v;
364 }
365
366 inline float GetFloat(const TreeNode &child)
367 {
368   OptionalFloat v( IsFloat( OptionalChild( child ) ) );
369   DALI_ASSERT_ALWAYS(v);
370   return *v;
371 }
372
373 inline bool GetBoolean(const TreeNode &child)
374 {
375   OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
376   DALI_ASSERT_ALWAYS(v);
377   return *v;
378 }
379
380 inline int GetInteger(const TreeNode &child)
381 {
382   OptionalInteger v( IsInteger( OptionalChild( child ) ) );
383   DALI_ASSERT_ALWAYS(v);
384   return *v;
385 }
386
387
388
389 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__