Add post processor
[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) 2021 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 inline OptionalVector4 IsVector4(const OptionalChild& node)
163 {
164   OptionalVector4 ret;
165
166   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4)
167   {
168     Dali::Vector4 v;
169     if(CopyNumbers((*node).CBegin(), 4, v))
170     {
171       ret = OptionalVector4(v);
172     }
173   }
174
175   return ret;
176 }
177
178 inline OptionalVector3 IsVector3(const OptionalChild& node)
179 {
180   OptionalVector3 ret;
181
182   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3)
183   {
184     Dali::Vector3 v;
185     if(CopyNumbers((*node).CBegin(), 3, v))
186     {
187       ret = OptionalVector3(v);
188     }
189   }
190
191   return ret;
192 }
193
194 inline OptionalVector2 IsVector2(const OptionalChild& node)
195 {
196   OptionalVector2 ret;
197
198   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2)
199   {
200     Dali::Vector2 v;
201     if(CopyNumbers((*node).CBegin(), 2, v))
202     {
203       ret = OptionalVector2(v);
204     }
205   }
206
207   return ret;
208 }
209
210 inline OptionalMatrix IsMatrix(const OptionalChild& node)
211 {
212   OptionalMatrix ret;
213
214   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16)
215   {
216     float v[16];
217     if(CopyNumbers((*node).CBegin(), 16, v))
218     {
219       ret = OptionalMatrix(Dali::Matrix(v));
220     }
221   }
222
223   return ret;
224 }
225
226 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
227 {
228   OptionalMatrix3 ret;
229
230   if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9)
231   {
232     float v[9];
233     if(CopyNumbers((*node).CBegin(), 9, v))
234     {
235       ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]));
236     }
237   }
238
239   return ret;
240 }
241
242 inline OptionalRect IsRect(const OptionalChild& node)
243 {
244   OptionalRect ret;
245   if(node && (*node).Size())
246   {
247     if((*node).Size() >= 4)
248     {
249       TreeNode::ConstIterator iter((*node).CBegin());
250       int                     v[4];
251       if(CopyNumbers((*node).CBegin(), 4, v))
252       {
253         ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
254       }
255     }
256   }
257   return ret;
258 }
259
260 inline OptionalExtents IsExtents(const OptionalChild& node)
261 {
262   OptionalExtents extents;
263   if(node && (*node).Size())
264   {
265     if((*node).Size() >= 4)
266     {
267       TreeNode::ConstIterator iter((*node).CBegin());
268       int                     v[4];
269       if(CopyNumbers((*node).CBegin(), 4, v))
270       {
271         extents = OptionalExtents(Dali::Extents(v[0], v[1], v[2], v[3]));
272       }
273     }
274   }
275   return extents;
276 }
277
278 //
279 //
280 //
281 inline OptionalString IsString(const TreeNode& parent, const std::string& childName)
282 {
283   return IsString(IsChild(&parent, childName));
284 }
285
286 inline OptionalFloat IsFloat(const TreeNode& parent, const std::string& childName)
287 {
288   return IsFloat(IsChild(&parent, childName));
289 }
290
291 inline OptionalInteger IsInteger(const TreeNode& parent, const std::string& childName)
292 {
293   return IsInteger(IsChild(&parent, childName));
294 }
295
296 inline OptionalBoolean IsBoolean(const TreeNode& parent, const std::string& childName)
297 {
298   return IsBoolean(IsChild(parent, childName));
299 }
300
301 inline OptionalVector4 IsVector4(const TreeNode& parent, const std::string& childName)
302 {
303   return IsVector4(IsChild(parent, childName));
304 }
305
306 inline OptionalVector3 IsVector3(const TreeNode& parent, const std::string& childName)
307 {
308   return IsVector3(IsChild(parent, childName));
309 }
310
311 inline OptionalVector2 IsVector2(const TreeNode& parent, const std::string& childName)
312 {
313   return IsVector2(IsChild(parent, childName));
314 }
315
316 inline OptionalMatrix IsMatrix(const TreeNode& parent, const std::string& childName)
317 {
318   return IsMatrix(IsChild(parent, childName));
319 }
320
321 inline OptionalMatrix3 IsMatrix3(const TreeNode& parent, const std::string& childName)
322 {
323   return IsMatrix3(IsChild(&parent, childName));
324 }
325
326 inline OptionalRect IsRect(const TreeNode& parent, const std::string& childName)
327 {
328   return IsRect(IsChild(&parent, childName));
329 }
330
331 inline OptionalExtents IsExtents(const TreeNode& parent, const std::string& childName)
332 {
333   return IsExtents(IsChild(&parent, childName));
334 }
335
336 //
337 //
338 //
339 inline OptionalString IsString(const TreeNode& node)
340 {
341   return IsString(OptionalChild(node));
342 }
343
344 inline OptionalFloat IsFloat(const TreeNode& node)
345 {
346   return IsFloat(OptionalChild(node));
347 }
348
349 inline OptionalInteger IsInteger(const TreeNode& node)
350 {
351   return IsInteger(OptionalChild(node));
352 }
353
354 inline OptionalBoolean IsBoolean(const TreeNode& node)
355 {
356   return IsBoolean(OptionalChild(node));
357 }
358
359 inline OptionalVector4 IsVector4(const TreeNode& node)
360 {
361   return IsVector4(OptionalChild(node));
362 }
363
364 inline OptionalVector3 IsVector3(const TreeNode& node)
365 {
366   return IsVector3(OptionalChild(node));
367 }
368
369 inline OptionalVector2 IsVector2(const TreeNode& node)
370 {
371   return IsVector2(OptionalChild(node));
372 }
373
374 inline OptionalMatrix IsMatrix(const TreeNode& node)
375 {
376   return IsMatrix(OptionalChild(node));
377 }
378
379 inline OptionalMatrix3 IsMatrix3(const TreeNode& node)
380 {
381   return IsMatrix3(OptionalChild(node));
382 }
383
384 inline OptionalRect IsRect(const TreeNode& node)
385 {
386   return IsRect(OptionalChild(node));
387 }
388
389 inline OptionalExtents IsExtents(const TreeNode& node)
390 {
391   return IsExtents(OptionalChild(node));
392 }
393
394 //
395 //
396 //
397 inline Dali::Vector4 GetVector4(const TreeNode& child)
398 {
399   OptionalVector4 v(IsVector4(OptionalChild(child)));
400   DALI_ASSERT_ALWAYS(v);
401   return *v;
402 }
403
404 inline Dali::Vector3 GetVector3(const TreeNode& child)
405 {
406   OptionalVector3 v(IsVector3(OptionalChild(child)));
407   DALI_ASSERT_ALWAYS(v);
408   return *v;
409 }
410
411 inline Dali::Vector2 GetVector2(const TreeNode& child)
412 {
413   OptionalVector2 v(IsVector2(OptionalChild(child)));
414   DALI_ASSERT_ALWAYS(v);
415   return *v;
416 }
417
418 inline float GetFloat(const TreeNode& child)
419 {
420   OptionalFloat v(IsFloat(OptionalChild(child)));
421   DALI_ASSERT_ALWAYS(v);
422   return *v;
423 }
424
425 inline bool GetBoolean(const TreeNode& child)
426 {
427   OptionalBoolean v(IsBoolean(OptionalChild(child)));
428   DALI_ASSERT_ALWAYS(v);
429   return *v;
430 }
431
432 inline int GetInteger(const TreeNode& child)
433 {
434   OptionalInteger v(IsInteger(OptionalChild(child)));
435   DALI_ASSERT_ALWAYS(v);
436   return *v;
437 }
438
439 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL