Merge "TextController - Update the text model." into devel/master
[platform/core/uifw/dali-toolkit.git] / 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
61 inline OptionalFloat IsFloat(const OptionalChild& node)
62 {
63   OptionalFloat ret;
64
65   if( node )
66   {
67     if( (*node).GetType() == TreeNode::FLOAT )
68     {
69       ret = (*node).GetFloat();
70     }
71     else if( (*node).GetType() == TreeNode::INTEGER )
72     {
73       // JSON has number not float/int but JsonParser discriminates.
74       // Here we don't care so we allow coercion
75       ret = static_cast<float>( (*node).GetInteger() );
76     }
77   }
78
79   return ret;
80 }
81
82 inline OptionalInteger IsInteger(const OptionalChild &node)
83 {
84   OptionalInteger ret;
85
86   if( node )
87   {
88     if( (*node).GetType() == TreeNode::INTEGER )
89     {
90       ret = (*node).GetInteger();
91     }
92     else if( (*node).GetType() == TreeNode::FLOAT )
93     {
94       ret = static_cast<int>(  (*node).GetFloat() );
95     }
96   }
97
98   return ret;
99 }
100
101 inline OptionalBoolean IsBoolean(const OptionalChild& node)
102 {
103   if( node && (*node).GetType() == TreeNode::BOOLEAN )
104   {
105     return OptionalBoolean(1 == (*node).GetInteger());
106   }
107   else
108   {
109     return OptionalBoolean();
110   }
111 }
112
113
114 // copy N Numbers
115 template <typename T>
116 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
117 {
118   for(int i = 0; i < N; ++i)
119   {
120     if( (*iter).second.GetType() == TreeNode::FLOAT)
121     {
122       vector[i] = (*iter).second.GetFloat();
123     }
124     else if( (*iter).second.GetType() == TreeNode::INTEGER )
125     {
126       vector[i] = static_cast<float>((*iter).second.GetInteger());
127     }
128     else
129     {
130       return false;
131     }
132     iter++;
133   }
134
135   return true;
136 }
137
138 inline OptionalVector4 IsVector4(const OptionalChild& node)
139 {
140   OptionalVector4 ret;
141
142   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
143   {
144     Dali::Vector4 v;
145     if( CopyNumbers((*node).CBegin(), 4, v) )
146     {
147       ret = OptionalVector4(v);
148     }
149   }
150
151   return ret;
152 }
153
154 inline OptionalVector3 IsVector3(const OptionalChild& node)
155 {
156   OptionalVector3 ret;
157
158   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
159   {
160     Dali::Vector3 v;
161     if( CopyNumbers((*node).CBegin(), 3, v) )
162     {
163       ret = OptionalVector3(v);
164     }
165   }
166
167   return ret;
168 }
169
170 inline OptionalVector2 IsVector2(const OptionalChild& node)
171 {
172   OptionalVector2 ret;
173
174   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
175   {
176     Dali::Vector2 v;
177     if( CopyNumbers((*node).CBegin(), 2, v) )
178     {
179       ret = OptionalVector2(v);
180     }
181   }
182
183   return ret;
184 }
185
186 inline OptionalMatrix IsMatrix(const OptionalChild &node)
187 {
188   OptionalMatrix ret;
189
190   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
191   {
192     float v[16];
193     if( CopyNumbers((*node).CBegin(), 16, v) )
194     {
195       ret = OptionalMatrix(Dali::Matrix(v));
196     }
197   }
198
199   return ret;
200 }
201
202 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
203 {
204   OptionalMatrix3 ret;
205
206   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
207   {
208     float v[9];
209     if( CopyNumbers((*node).CBegin(), 9, v) )
210     {
211       ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
212                                           v[3], v[4], v[5],
213                                           v[6], v[7], v[8] ));
214     }
215   }
216
217   return ret;
218 }
219
220 inline OptionalRect IsRect(const OptionalChild& node)
221 {
222   OptionalRect ret;
223   if(node && (*node).Size())
224   {
225     if((*node).Size() >= 4)
226     {
227       TreeNode::ConstIterator iter((*node).CBegin());
228       int v[4];
229       if( CopyNumbers((*node).CBegin(), 4, v) )
230       {
231         ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
232       }
233     }
234   }
235   return ret;
236 }
237
238 //
239 //
240 //
241 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
242 {
243   return IsString( IsChild(&parent, childName) );
244 }
245
246 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
247 {
248   return IsFloat( IsChild(&parent, childName) );
249 }
250
251 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
252 {
253   return IsInteger( IsChild(&parent, childName) );
254 }
255
256 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
257 {
258   return IsBoolean( IsChild(parent, childName) );
259 }
260
261 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
262 {
263   return IsVector4( IsChild(parent, childName) );
264 }
265
266 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
267 {
268   return IsVector3( IsChild(parent, childName) );
269 }
270
271 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
272 {
273   return IsVector2( IsChild(parent, childName) );
274 }
275
276 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
277 {
278   return IsMatrix( IsChild(parent, childName) );
279 }
280
281 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
282 {
283   return IsMatrix3( IsChild(&parent, childName) );
284 }
285
286 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
287 {
288   return IsRect( IsChild(&parent, childName) );
289 }
290
291 //
292 //
293 //
294 inline OptionalString IsString( const TreeNode& node )
295 {
296   return IsString( OptionalChild( node ) );
297 }
298
299 inline OptionalFloat IsFloat( const TreeNode& node )
300 {
301   return IsFloat( OptionalChild( node ) );
302 }
303
304 inline OptionalInteger IsInteger( const TreeNode& node )
305 {
306   return IsInteger( OptionalChild( node ) );
307 }
308
309 inline OptionalBoolean IsBoolean( const TreeNode& node )
310 {
311   return IsBoolean( OptionalChild( node ) );
312 }
313
314 inline OptionalVector4 IsVector4(const TreeNode &node )
315 {
316   return IsVector4( OptionalChild( node ) );
317 }
318
319 inline OptionalVector3 IsVector3(const TreeNode &node )
320 {
321   return IsVector3( OptionalChild( node ) );
322 }
323
324 inline OptionalVector2 IsVector2(const TreeNode &node )
325 {
326   return IsVector2( OptionalChild( node ) );
327 }
328
329 inline OptionalMatrix IsMatrix(const TreeNode &node )
330 {
331   return IsMatrix( OptionalChild( node ) );
332 }
333
334 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
335 {
336   return IsMatrix3( OptionalChild( node ) );
337 }
338
339 inline OptionalRect IsRect(const TreeNode &node )
340 {
341   return IsRect( OptionalChild( node ) );
342 }
343
344 //
345 //
346 //
347 inline Dali::Vector4 GetVector4(const TreeNode &child)
348 {
349   OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
350   DALI_ASSERT_ALWAYS(v);
351   return *v;
352 }
353
354 inline Dali::Vector3 GetVector3(const TreeNode &child)
355 {
356   OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
357   DALI_ASSERT_ALWAYS(v);
358   return *v;
359 }
360
361 inline Dali::Vector2 GetVector2(const TreeNode &child)
362 {
363   OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
364   DALI_ASSERT_ALWAYS(v);
365   return *v;
366 }
367
368 inline float GetFloat(const TreeNode &child)
369 {
370   OptionalFloat v( IsFloat( OptionalChild( child ) ) );
371   DALI_ASSERT_ALWAYS(v);
372   return *v;
373 }
374
375 inline bool GetBoolean(const TreeNode &child)
376 {
377   OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
378   DALI_ASSERT_ALWAYS(v);
379   return *v;
380 }
381
382 inline int GetInteger(const TreeNode &child)
383 {
384   OptionalInteger v( IsInteger( OptionalChild( child ) ) );
385   DALI_ASSERT_ALWAYS(v);
386   return *v;
387 }
388
389
390
391 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__