[dali_2.3.20] Merge branch '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) 2022 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 IsChildIgnoreCase(const TreeNode* node, const std::string& childName)
45 {
46   if(node)
47   {
48     const TreeNode* c = node->GetChildIgnoreCase(childName);
49     if(NULL != c)
50     {
51       return OptionalChild(*c);
52     }
53     else
54     {
55       return OptionalChild();
56     }
57   }
58   else
59   {
60     return OptionalChild();
61   }
62 }
63
64 inline OptionalChild IsChild(const TreeNode& node, const std::string& childName)
65 {
66   return IsChild(&node, childName);
67 }
68
69 inline OptionalChild IsChildIgnoreCase(const TreeNode& node, const std::string& childName)
70 {
71   return IsChildIgnoreCase(&node, childName);
72 }
73
74 inline OptionalString IsString(const OptionalChild& node)
75 {
76   if(node && (*node).GetType() == TreeNode::STRING)
77   {
78     return OptionalString((*node).GetString());
79   }
80   else
81   {
82     return OptionalString();
83   }
84 }
85
86 inline OptionalFloat IsFloat(const OptionalChild& node)
87 {
88   OptionalFloat ret;
89
90   if(node)
91   {
92     if((*node).GetType() == TreeNode::FLOAT)
93     {
94       ret = (*node).GetFloat();
95     }
96     else if((*node).GetType() == TreeNode::INTEGER)
97     {
98       // JSON has number not float/int but JsonParser discriminates.
99       // Here we don't care so we allow coercion
100       ret = static_cast<float>((*node).GetInteger());
101     }
102   }
103
104   return ret;
105 }
106
107 inline OptionalInteger IsInteger(const OptionalChild& node)
108 {
109   OptionalInteger ret;
110
111   if(node)
112   {
113     if((*node).GetType() == TreeNode::INTEGER)
114     {
115       ret = (*node).GetInteger();
116     }
117     else if((*node).GetType() == TreeNode::FLOAT)
118     {
119       ret = static_cast<int>((*node).GetFloat());
120     }
121   }
122
123   return ret;
124 }
125
126 inline OptionalBoolean IsBoolean(const OptionalChild& node)
127 {
128   if(node && (*node).GetType() == TreeNode::BOOLEAN)
129   {
130     return OptionalBoolean(1 == (*node).GetInteger());
131   }
132   else
133   {
134     return OptionalBoolean();
135   }
136 }
137
138 // copy N Numbers
139 template<typename T>
140 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
141 {
142   for(int i = 0; i < N; ++i)
143   {
144     if((*iter).second.GetType() == TreeNode::FLOAT)
145     {
146       vector[i] = (*iter).second.GetFloat();
147     }
148     else if((*iter).second.GetType() == TreeNode::INTEGER)
149     {
150       vector[i] = static_cast<float>((*iter).second.GetInteger());
151     }
152     else
153     {
154       return false;
155     }
156     iter++;
157   }
158
159   return true;
160 }
161
162 // copy N Numbers
163 template<typename T, int N>
164 inline bool CopyNumbers(TreeNode::ConstIterator iter, T (&vector)[N])
165 {
166   for(int i = 0; i < N; ++i)
167   {
168     if((*iter).second.GetType() == TreeNode::FLOAT)
169     {
170       vector[i] = (*iter).second.GetFloat();
171     }
172     else if((*iter).second.GetType() == TreeNode::INTEGER)
173     {
174       vector[i] = static_cast<float>((*iter).second.GetInteger());
175     }
176     else
177     {
178       return false;
179     }
180     iter++;
181   }
182
183   return true;
184 }
185
186 inline OptionalVector4 IsVector4(const OptionalChild& node)
187 {
188   OptionalVector4 ret;
189
190   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4)
191   {
192     Dali::Vector4 v;
193     if(CopyNumbers((*node).CBegin(), 4, v))
194     {
195       ret = OptionalVector4(v);
196     }
197   }
198
199   return ret;
200 }
201
202 inline OptionalVector3 IsVector3(const OptionalChild& node)
203 {
204   OptionalVector3 ret;
205
206   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3)
207   {
208     Dali::Vector3 v;
209     if(CopyNumbers((*node).CBegin(), 3, v))
210     {
211       ret = OptionalVector3(v);
212     }
213   }
214
215   return ret;
216 }
217
218 inline OptionalVector2 IsVector2(const OptionalChild& node)
219 {
220   OptionalVector2 ret;
221
222   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2)
223   {
224     Dali::Vector2 v;
225     if(CopyNumbers((*node).CBegin(), 2, v))
226     {
227       ret = OptionalVector2(v);
228     }
229   }
230
231   return ret;
232 }
233
234 inline OptionalMatrix IsMatrix(const OptionalChild& node)
235 {
236   OptionalMatrix ret;
237
238   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16)
239   {
240     float v[16];
241     if(CopyNumbers((*node).CBegin(), v))
242     {
243       ret = OptionalMatrix(Dali::Matrix(v));
244     }
245   }
246
247   return ret;
248 }
249
250 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
251 {
252   OptionalMatrix3 ret;
253
254   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9)
255   {
256     float v[9];
257     if(CopyNumbers((*node).CBegin(), v))
258     {
259       ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]));
260     }
261   }
262
263   return ret;
264 }
265
266 inline OptionalRect IsRect(const OptionalChild& node)
267 {
268   OptionalRect ret;
269   if(node && (*node).Size())
270   {
271     if((*node).Size() >= 4)
272     {
273       TreeNode::ConstIterator iter((*node).CBegin());
274       float                   v[4];
275       if(CopyNumbers((*node).CBegin(), v))
276       {
277         ret = OptionalRect(Dali::Rect<int>(static_cast<int>(v[0]), static_cast<int>(v[1]), static_cast<int>(v[2]), static_cast<int>(v[3])));
278       }
279     }
280   }
281   return ret;
282 }
283
284 inline OptionalExtents IsExtents(const OptionalChild& node)
285 {
286   OptionalExtents extents;
287   if(node && (*node).Size())
288   {
289     if((*node).Size() >= 4)
290     {
291       TreeNode::ConstIterator iter((*node).CBegin());
292       float                   v[4];
293       if(CopyNumbers((*node).CBegin(), v))
294       {
295         extents = OptionalExtents(Dali::Extents(static_cast<uint16_t>(v[0]), static_cast<uint16_t>(v[1]), static_cast<uint16_t>(v[2]), static_cast<uint16_t>(v[3])));
296       }
297     }
298   }
299   return extents;
300 }
301
302 //
303 //
304 //
305 inline OptionalString IsString(const TreeNode& parent, const std::string& childName)
306 {
307   return IsString(IsChild(&parent, childName));
308 }
309
310 inline OptionalFloat IsFloat(const TreeNode& parent, const std::string& childName)
311 {
312   return IsFloat(IsChild(&parent, childName));
313 }
314
315 inline OptionalInteger IsInteger(const TreeNode& parent, const std::string& childName)
316 {
317   return IsInteger(IsChild(&parent, childName));
318 }
319
320 inline OptionalBoolean IsBoolean(const TreeNode& parent, const std::string& childName)
321 {
322   return IsBoolean(IsChild(parent, childName));
323 }
324
325 inline OptionalVector4 IsVector4(const TreeNode& parent, const std::string& childName)
326 {
327   return IsVector4(IsChild(parent, childName));
328 }
329
330 inline OptionalVector3 IsVector3(const TreeNode& parent, const std::string& childName)
331 {
332   return IsVector3(IsChild(parent, childName));
333 }
334
335 inline OptionalVector2 IsVector2(const TreeNode& parent, const std::string& childName)
336 {
337   return IsVector2(IsChild(parent, childName));
338 }
339
340 inline OptionalMatrix IsMatrix(const TreeNode& parent, const std::string& childName)
341 {
342   return IsMatrix(IsChild(parent, childName));
343 }
344
345 inline OptionalMatrix3 IsMatrix3(const TreeNode& parent, const std::string& childName)
346 {
347   return IsMatrix3(IsChild(&parent, childName));
348 }
349
350 inline OptionalRect IsRect(const TreeNode& parent, const std::string& childName)
351 {
352   return IsRect(IsChild(&parent, childName));
353 }
354
355 inline OptionalExtents IsExtents(const TreeNode& parent, const std::string& childName)
356 {
357   return IsExtents(IsChild(&parent, childName));
358 }
359
360 //
361 //
362 //
363 inline OptionalString IsString(const TreeNode& node)
364 {
365   return IsString(OptionalChild(node));
366 }
367
368 inline OptionalFloat IsFloat(const TreeNode& node)
369 {
370   return IsFloat(OptionalChild(node));
371 }
372
373 inline OptionalInteger IsInteger(const TreeNode& node)
374 {
375   return IsInteger(OptionalChild(node));
376 }
377
378 inline OptionalBoolean IsBoolean(const TreeNode& node)
379 {
380   return IsBoolean(OptionalChild(node));
381 }
382
383 inline OptionalVector4 IsVector4(const TreeNode& node)
384 {
385   return IsVector4(OptionalChild(node));
386 }
387
388 inline OptionalVector3 IsVector3(const TreeNode& node)
389 {
390   return IsVector3(OptionalChild(node));
391 }
392
393 inline OptionalVector2 IsVector2(const TreeNode& node)
394 {
395   return IsVector2(OptionalChild(node));
396 }
397
398 inline OptionalMatrix IsMatrix(const TreeNode& node)
399 {
400   return IsMatrix(OptionalChild(node));
401 }
402
403 inline OptionalMatrix3 IsMatrix3(const TreeNode& node)
404 {
405   return IsMatrix3(OptionalChild(node));
406 }
407
408 inline OptionalRect IsRect(const TreeNode& node)
409 {
410   return IsRect(OptionalChild(node));
411 }
412
413 inline OptionalExtents IsExtents(const TreeNode& node)
414 {
415   return IsExtents(OptionalChild(node));
416 }
417
418 //
419 //
420 //
421 inline Dali::Vector4 GetVector4(const TreeNode& child)
422 {
423   OptionalVector4 v(IsVector4(OptionalChild(child)));
424   DALI_ASSERT_ALWAYS(v);
425   return *v;
426 }
427
428 inline Dali::Vector3 GetVector3(const TreeNode& child)
429 {
430   OptionalVector3 v(IsVector3(OptionalChild(child)));
431   DALI_ASSERT_ALWAYS(v);
432   return *v;
433 }
434
435 inline Dali::Vector2 GetVector2(const TreeNode& child)
436 {
437   OptionalVector2 v(IsVector2(OptionalChild(child)));
438   DALI_ASSERT_ALWAYS(v);
439   return *v;
440 }
441
442 inline float GetFloat(const TreeNode& child)
443 {
444   OptionalFloat v(IsFloat(OptionalChild(child)));
445   DALI_ASSERT_ALWAYS(v);
446   return *v;
447 }
448
449 inline bool GetBoolean(const TreeNode& child)
450 {
451   OptionalBoolean v(IsBoolean(OptionalChild(child)));
452   DALI_ASSERT_ALWAYS(v);
453   return *v;
454 }
455
456 inline int GetInteger(const TreeNode& child)
457 {
458   OptionalInteger v(IsInteger(OptionalChild(child)));
459   DALI_ASSERT_ALWAYS(v);
460   return *v;
461 }
462
463 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL