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]));
263 inline OptionalExtents IsExtents(const OptionalChild& node)
265 OptionalExtents extents;
266 if(node && (*node).Size())
268 if((*node).Size() >= 4)
270 TreeNode::ConstIterator iter((*node).CBegin());
272 if( CopyNumbers((*node).CBegin(), 4, v) )
274 extents = OptionalExtents(Dali::Extents(v[0], v[1], v[2], v[3]));
284 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
286 return IsString( IsChild(&parent, childName) );
289 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
291 return IsFloat( IsChild(&parent, childName) );
294 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
296 return IsInteger( IsChild(&parent, childName) );
299 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
301 return IsBoolean( IsChild(parent, childName) );
304 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
306 return IsVector4( IsChild(parent, childName) );
309 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
311 return IsVector3( IsChild(parent, childName) );
314 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
316 return IsVector2( IsChild(parent, childName) );
319 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
321 return IsMatrix( IsChild(parent, childName) );
324 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
326 return IsMatrix3( IsChild(&parent, childName) );
329 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
331 return IsRect( IsChild(&parent, childName) );
334 inline OptionalExtents IsExtents(const TreeNode &parent, const std::string& childName)
336 return IsExtents( IsChild(&parent, childName) );
342 inline OptionalString IsString( const TreeNode& node )
344 return IsString( OptionalChild( node ) );
347 inline OptionalFloat IsFloat( const TreeNode& node )
349 return IsFloat( OptionalChild( node ) );
352 inline OptionalInteger IsInteger( const TreeNode& node )
354 return IsInteger( OptionalChild( node ) );
357 inline OptionalBoolean IsBoolean( const TreeNode& node )
359 return IsBoolean( OptionalChild( node ) );
362 inline OptionalVector4 IsVector4(const TreeNode &node )
364 return IsVector4( OptionalChild( node ) );
367 inline OptionalVector3 IsVector3(const TreeNode &node )
369 return IsVector3( OptionalChild( node ) );
372 inline OptionalVector2 IsVector2(const TreeNode &node )
374 return IsVector2( OptionalChild( node ) );
377 inline OptionalMatrix IsMatrix(const TreeNode &node )
379 return IsMatrix( OptionalChild( node ) );
382 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
384 return IsMatrix3( OptionalChild( node ) );
387 inline OptionalRect IsRect(const TreeNode &node )
389 return IsRect( OptionalChild( node ) );
392 inline OptionalExtents IsExtents(const TreeNode &node )
394 return IsExtents( OptionalChild( node ) );
400 inline Dali::Vector4 GetVector4(const TreeNode &child)
402 OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
403 DALI_ASSERT_ALWAYS(v);
407 inline Dali::Vector3 GetVector3(const TreeNode &child)
409 OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
410 DALI_ASSERT_ALWAYS(v);
414 inline Dali::Vector2 GetVector2(const TreeNode &child)
416 OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
417 DALI_ASSERT_ALWAYS(v);
421 inline float GetFloat(const TreeNode &child)
423 OptionalFloat v( IsFloat( OptionalChild( child ) ) );
424 DALI_ASSERT_ALWAYS(v);
428 inline bool GetBoolean(const TreeNode &child)
430 OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
431 DALI_ASSERT_ALWAYS(v);
435 inline int GetInteger(const TreeNode &child)
437 OptionalInteger v( IsInteger( OptionalChild( child ) ) );
438 DALI_ASSERT_ALWAYS(v);
444 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__