1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali-toolkit/internal/builder/builder-declarations.h>
24 inline OptionalChild IsChild(const TreeNode* node, const std::string& childName)
28 const TreeNode* c = node->GetChild(childName);
31 return OptionalChild(*c);
35 return OptionalChild();
40 return OptionalChild();
44 inline OptionalChild IsChildIgnoreCase(const TreeNode* node, const std::string& childName)
48 const TreeNode* c = node->GetChildIgnoreCase(childName);
51 return OptionalChild(*c);
55 return OptionalChild();
60 return OptionalChild();
64 inline OptionalChild IsChild(const TreeNode& node, const std::string& childName)
66 return IsChild(&node, childName);
69 inline OptionalChild IsChildIgnoreCase(const TreeNode& node, const std::string& childName)
71 return IsChildIgnoreCase(&node, childName);
74 inline OptionalString IsString(const OptionalChild& node)
76 if(node && (*node).GetType() == TreeNode::STRING)
78 return OptionalString((*node).GetString());
82 return OptionalString();
86 inline OptionalFloat IsFloat(const OptionalChild& node)
92 if((*node).GetType() == TreeNode::FLOAT)
94 ret = (*node).GetFloat();
96 else if((*node).GetType() == TreeNode::INTEGER)
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());
107 inline OptionalInteger IsInteger(const OptionalChild& node)
113 if((*node).GetType() == TreeNode::INTEGER)
115 ret = (*node).GetInteger();
117 else if((*node).GetType() == TreeNode::FLOAT)
119 ret = static_cast<int>((*node).GetFloat());
126 inline OptionalBoolean IsBoolean(const OptionalChild& node)
128 if(node && (*node).GetType() == TreeNode::BOOLEAN)
130 return OptionalBoolean(1 == (*node).GetInteger());
134 return OptionalBoolean();
140 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
142 for(int i = 0; i < N; ++i)
144 if((*iter).second.GetType() == TreeNode::FLOAT)
146 vector[i] = (*iter).second.GetFloat();
148 else if((*iter).second.GetType() == TreeNode::INTEGER)
150 vector[i] = static_cast<float>((*iter).second.GetInteger());
162 inline OptionalVector4 IsVector4(const OptionalChild& node)
166 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4)
169 if(CopyNumbers((*node).CBegin(), 4, v))
171 ret = OptionalVector4(v);
178 inline OptionalVector3 IsVector3(const OptionalChild& node)
182 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3)
185 if(CopyNumbers((*node).CBegin(), 3, v))
187 ret = OptionalVector3(v);
194 inline OptionalVector2 IsVector2(const OptionalChild& node)
198 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2)
201 if(CopyNumbers((*node).CBegin(), 2, v))
203 ret = OptionalVector2(v);
210 inline OptionalMatrix IsMatrix(const OptionalChild& node)
214 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16)
217 if(CopyNumbers((*node).CBegin(), 16, v))
219 ret = OptionalMatrix(Dali::Matrix(v));
226 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
230 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9)
233 if(CopyNumbers((*node).CBegin(), 9, v))
235 ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]));
242 inline OptionalRect IsRect(const OptionalChild& node)
245 if(node && (*node).Size())
247 if((*node).Size() >= 4)
249 TreeNode::ConstIterator iter((*node).CBegin());
251 if(CopyNumbers((*node).CBegin(), 4, v))
253 ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
260 inline OptionalExtents IsExtents(const OptionalChild& node)
262 OptionalExtents extents;
263 if(node && (*node).Size())
265 if((*node).Size() >= 4)
267 TreeNode::ConstIterator iter((*node).CBegin());
269 if(CopyNumbers((*node).CBegin(), 4, v))
271 extents = OptionalExtents(Dali::Extents(v[0], v[1], v[2], v[3]));
281 inline OptionalString IsString(const TreeNode& parent, const std::string& childName)
283 return IsString(IsChild(&parent, childName));
286 inline OptionalFloat IsFloat(const TreeNode& parent, const std::string& childName)
288 return IsFloat(IsChild(&parent, childName));
291 inline OptionalInteger IsInteger(const TreeNode& parent, const std::string& childName)
293 return IsInteger(IsChild(&parent, childName));
296 inline OptionalBoolean IsBoolean(const TreeNode& parent, const std::string& childName)
298 return IsBoolean(IsChild(parent, childName));
301 inline OptionalVector4 IsVector4(const TreeNode& parent, const std::string& childName)
303 return IsVector4(IsChild(parent, childName));
306 inline OptionalVector3 IsVector3(const TreeNode& parent, const std::string& childName)
308 return IsVector3(IsChild(parent, childName));
311 inline OptionalVector2 IsVector2(const TreeNode& parent, const std::string& childName)
313 return IsVector2(IsChild(parent, childName));
316 inline OptionalMatrix IsMatrix(const TreeNode& parent, const std::string& childName)
318 return IsMatrix(IsChild(parent, childName));
321 inline OptionalMatrix3 IsMatrix3(const TreeNode& parent, const std::string& childName)
323 return IsMatrix3(IsChild(&parent, childName));
326 inline OptionalRect IsRect(const TreeNode& parent, const std::string& childName)
328 return IsRect(IsChild(&parent, childName));
331 inline OptionalExtents IsExtents(const TreeNode& parent, const std::string& childName)
333 return IsExtents(IsChild(&parent, childName));
339 inline OptionalString IsString(const TreeNode& node)
341 return IsString(OptionalChild(node));
344 inline OptionalFloat IsFloat(const TreeNode& node)
346 return IsFloat(OptionalChild(node));
349 inline OptionalInteger IsInteger(const TreeNode& node)
351 return IsInteger(OptionalChild(node));
354 inline OptionalBoolean IsBoolean(const TreeNode& node)
356 return IsBoolean(OptionalChild(node));
359 inline OptionalVector4 IsVector4(const TreeNode& node)
361 return IsVector4(OptionalChild(node));
364 inline OptionalVector3 IsVector3(const TreeNode& node)
366 return IsVector3(OptionalChild(node));
369 inline OptionalVector2 IsVector2(const TreeNode& node)
371 return IsVector2(OptionalChild(node));
374 inline OptionalMatrix IsMatrix(const TreeNode& node)
376 return IsMatrix(OptionalChild(node));
379 inline OptionalMatrix3 IsMatrix3(const TreeNode& node)
381 return IsMatrix3(OptionalChild(node));
384 inline OptionalRect IsRect(const TreeNode& node)
386 return IsRect(OptionalChild(node));
389 inline OptionalExtents IsExtents(const TreeNode& node)
391 return IsExtents(OptionalChild(node));
397 inline Dali::Vector4 GetVector4(const TreeNode& child)
399 OptionalVector4 v(IsVector4(OptionalChild(child)));
400 DALI_ASSERT_ALWAYS(v);
404 inline Dali::Vector3 GetVector3(const TreeNode& child)
406 OptionalVector3 v(IsVector3(OptionalChild(child)));
407 DALI_ASSERT_ALWAYS(v);
411 inline Dali::Vector2 GetVector2(const TreeNode& child)
413 OptionalVector2 v(IsVector2(OptionalChild(child)));
414 DALI_ASSERT_ALWAYS(v);
418 inline float GetFloat(const TreeNode& child)
420 OptionalFloat v(IsFloat(OptionalChild(child)));
421 DALI_ASSERT_ALWAYS(v);
425 inline bool GetBoolean(const TreeNode& child)
427 OptionalBoolean v(IsBoolean(OptionalChild(child)));
428 DALI_ASSERT_ALWAYS(v);
432 inline int GetInteger(const TreeNode& child)
434 OptionalInteger v(IsInteger(OptionalChild(child)));
435 DALI_ASSERT_ALWAYS(v);
439 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL