1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL
5 * Copyright (c) 2022 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());
163 template<typename T, int N>
164 inline bool CopyNumbers(TreeNode::ConstIterator iter, T (&vector)[N])
166 for(int i = 0; i < N; ++i)
168 if((*iter).second.GetType() == TreeNode::FLOAT)
170 vector[i] = (*iter).second.GetFloat();
172 else if((*iter).second.GetType() == TreeNode::INTEGER)
174 vector[i] = static_cast<float>((*iter).second.GetInteger());
186 inline OptionalVector4 IsVector4(const OptionalChild& node)
190 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4)
193 if(CopyNumbers((*node).CBegin(), 4, v))
195 ret = OptionalVector4(v);
202 inline OptionalVector3 IsVector3(const OptionalChild& node)
206 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3)
209 if(CopyNumbers((*node).CBegin(), 3, v))
211 ret = OptionalVector3(v);
218 inline OptionalVector2 IsVector2(const OptionalChild& node)
222 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2)
225 if(CopyNumbers((*node).CBegin(), 2, v))
227 ret = OptionalVector2(v);
234 inline OptionalMatrix IsMatrix(const OptionalChild& node)
238 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16)
241 if(CopyNumbers((*node).CBegin(), v))
243 ret = OptionalMatrix(Dali::Matrix(v));
250 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
254 if(node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9)
257 if(CopyNumbers((*node).CBegin(), v))
259 ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]));
266 inline OptionalRect IsRect(const OptionalChild& node)
269 if(node && (*node).Size())
271 if((*node).Size() >= 4)
273 TreeNode::ConstIterator iter((*node).CBegin());
275 if(CopyNumbers((*node).CBegin(), v))
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])));
284 inline OptionalExtents IsExtents(const OptionalChild& node)
286 OptionalExtents extents;
287 if(node && (*node).Size())
289 if((*node).Size() >= 4)
291 TreeNode::ConstIterator iter((*node).CBegin());
293 if(CopyNumbers((*node).CBegin(), v))
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])));
305 inline OptionalString IsString(const TreeNode& parent, const std::string& childName)
307 return IsString(IsChild(&parent, childName));
310 inline OptionalFloat IsFloat(const TreeNode& parent, const std::string& childName)
312 return IsFloat(IsChild(&parent, childName));
315 inline OptionalInteger IsInteger(const TreeNode& parent, const std::string& childName)
317 return IsInteger(IsChild(&parent, childName));
320 inline OptionalBoolean IsBoolean(const TreeNode& parent, const std::string& childName)
322 return IsBoolean(IsChild(parent, childName));
325 inline OptionalVector4 IsVector4(const TreeNode& parent, const std::string& childName)
327 return IsVector4(IsChild(parent, childName));
330 inline OptionalVector3 IsVector3(const TreeNode& parent, const std::string& childName)
332 return IsVector3(IsChild(parent, childName));
335 inline OptionalVector2 IsVector2(const TreeNode& parent, const std::string& childName)
337 return IsVector2(IsChild(parent, childName));
340 inline OptionalMatrix IsMatrix(const TreeNode& parent, const std::string& childName)
342 return IsMatrix(IsChild(parent, childName));
345 inline OptionalMatrix3 IsMatrix3(const TreeNode& parent, const std::string& childName)
347 return IsMatrix3(IsChild(&parent, childName));
350 inline OptionalRect IsRect(const TreeNode& parent, const std::string& childName)
352 return IsRect(IsChild(&parent, childName));
355 inline OptionalExtents IsExtents(const TreeNode& parent, const std::string& childName)
357 return IsExtents(IsChild(&parent, childName));
363 inline OptionalString IsString(const TreeNode& node)
365 return IsString(OptionalChild(node));
368 inline OptionalFloat IsFloat(const TreeNode& node)
370 return IsFloat(OptionalChild(node));
373 inline OptionalInteger IsInteger(const TreeNode& node)
375 return IsInteger(OptionalChild(node));
378 inline OptionalBoolean IsBoolean(const TreeNode& node)
380 return IsBoolean(OptionalChild(node));
383 inline OptionalVector4 IsVector4(const TreeNode& node)
385 return IsVector4(OptionalChild(node));
388 inline OptionalVector3 IsVector3(const TreeNode& node)
390 return IsVector3(OptionalChild(node));
393 inline OptionalVector2 IsVector2(const TreeNode& node)
395 return IsVector2(OptionalChild(node));
398 inline OptionalMatrix IsMatrix(const TreeNode& node)
400 return IsMatrix(OptionalChild(node));
403 inline OptionalMatrix3 IsMatrix3(const TreeNode& node)
405 return IsMatrix3(OptionalChild(node));
408 inline OptionalRect IsRect(const TreeNode& node)
410 return IsRect(OptionalChild(node));
413 inline OptionalExtents IsExtents(const TreeNode& node)
415 return IsExtents(OptionalChild(node));
421 inline Dali::Vector4 GetVector4(const TreeNode& child)
423 OptionalVector4 v(IsVector4(OptionalChild(child)));
424 DALI_ASSERT_ALWAYS(v);
428 inline Dali::Vector3 GetVector3(const TreeNode& child)
430 OptionalVector3 v(IsVector3(OptionalChild(child)));
431 DALI_ASSERT_ALWAYS(v);
435 inline Dali::Vector2 GetVector2(const TreeNode& child)
437 OptionalVector2 v(IsVector2(OptionalChild(child)));
438 DALI_ASSERT_ALWAYS(v);
442 inline float GetFloat(const TreeNode& child)
444 OptionalFloat v(IsFloat(OptionalChild(child)));
445 DALI_ASSERT_ALWAYS(v);
449 inline bool GetBoolean(const TreeNode& child)
451 OptionalBoolean v(IsBoolean(OptionalChild(child)));
452 DALI_ASSERT_ALWAYS(v);
456 inline int GetInteger(const TreeNode& child)
458 OptionalInteger v(IsInteger(OptionalChild(child)));
459 DALI_ASSERT_ALWAYS(v);
463 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL