1 #ifndef __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
2 #define __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
5 * Copyright (c) 2016 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 template <typename T>
141 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
143 for(int i = 0; i < N; ++i)
145 if( (*iter).second.GetType() == TreeNode::FLOAT)
147 vector[i] = (*iter).second.GetFloat();
149 else if( (*iter).second.GetType() == TreeNode::INTEGER )
151 vector[i] = static_cast<float>((*iter).second.GetInteger());
163 inline OptionalVector4 IsVector4(const OptionalChild& node)
167 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
170 if( CopyNumbers((*node).CBegin(), 4, v) )
172 ret = OptionalVector4(v);
179 inline OptionalVector3 IsVector3(const OptionalChild& node)
183 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
186 if( CopyNumbers((*node).CBegin(), 3, v) )
188 ret = OptionalVector3(v);
195 inline OptionalVector2 IsVector2(const OptionalChild& node)
199 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
202 if( CopyNumbers((*node).CBegin(), 2, v) )
204 ret = OptionalVector2(v);
211 inline OptionalMatrix IsMatrix(const OptionalChild &node)
215 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
218 if( CopyNumbers((*node).CBegin(), 16, v) )
220 ret = OptionalMatrix(Dali::Matrix(v));
227 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
231 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
234 if( CopyNumbers((*node).CBegin(), 9, v) )
236 ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
245 inline OptionalRect IsRect(const OptionalChild& node)
248 if(node && (*node).Size())
250 if((*node).Size() >= 4)
252 TreeNode::ConstIterator iter((*node).CBegin());
254 if( CopyNumbers((*node).CBegin(), 4, v) )
256 ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
266 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
268 return IsString( IsChild(&parent, childName) );
271 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
273 return IsFloat( IsChild(&parent, childName) );
276 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
278 return IsInteger( IsChild(&parent, childName) );
281 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
283 return IsBoolean( IsChild(parent, childName) );
286 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
288 return IsVector4( IsChild(parent, childName) );
291 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
293 return IsVector3( IsChild(parent, childName) );
296 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
298 return IsVector2( IsChild(parent, childName) );
301 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
303 return IsMatrix( IsChild(parent, childName) );
306 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
308 return IsMatrix3( IsChild(&parent, childName) );
311 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
313 return IsRect( IsChild(&parent, childName) );
319 inline OptionalString IsString( const TreeNode& node )
321 return IsString( OptionalChild( node ) );
324 inline OptionalFloat IsFloat( const TreeNode& node )
326 return IsFloat( OptionalChild( node ) );
329 inline OptionalInteger IsInteger( const TreeNode& node )
331 return IsInteger( OptionalChild( node ) );
334 inline OptionalBoolean IsBoolean( const TreeNode& node )
336 return IsBoolean( OptionalChild( node ) );
339 inline OptionalVector4 IsVector4(const TreeNode &node )
341 return IsVector4( OptionalChild( node ) );
344 inline OptionalVector3 IsVector3(const TreeNode &node )
346 return IsVector3( OptionalChild( node ) );
349 inline OptionalVector2 IsVector2(const TreeNode &node )
351 return IsVector2( OptionalChild( node ) );
354 inline OptionalMatrix IsMatrix(const TreeNode &node )
356 return IsMatrix( OptionalChild( node ) );
359 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
361 return IsMatrix3( OptionalChild( node ) );
364 inline OptionalRect IsRect(const TreeNode &node )
366 return IsRect( OptionalChild( node ) );
372 inline Dali::Vector4 GetVector4(const TreeNode &child)
374 OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
375 DALI_ASSERT_ALWAYS(v);
379 inline Dali::Vector3 GetVector3(const TreeNode &child)
381 OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
382 DALI_ASSERT_ALWAYS(v);
386 inline Dali::Vector2 GetVector2(const TreeNode &child)
388 OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
389 DALI_ASSERT_ALWAYS(v);
393 inline float GetFloat(const TreeNode &child)
395 OptionalFloat v( IsFloat( OptionalChild( child ) ) );
396 DALI_ASSERT_ALWAYS(v);
400 inline bool GetBoolean(const TreeNode &child)
402 OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
403 DALI_ASSERT_ALWAYS(v);
407 inline int GetInteger(const TreeNode &child)
409 OptionalInteger v( IsInteger( OptionalChild( child ) ) );
410 DALI_ASSERT_ALWAYS(v);
416 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__