1 #ifndef __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
2 #define __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
5 * Copyright (c) 2014 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 IsChild(const TreeNode& node, const std::string& childName)
46 return IsChild(&node, childName);
49 inline OptionalString IsString(const OptionalChild& node)
51 if( node && (*node).GetType() == TreeNode::STRING )
53 return OptionalString((*node).GetString());
57 return OptionalString();
60 inline OptionalFloat IsFloat(const OptionalChild& node)
66 if( (*node).GetType() == TreeNode::FLOAT )
68 ret = (*node).GetFloat();
70 else if( (*node).GetType() == TreeNode::INTEGER )
72 // JSON has number not float/int but JsonParser discriminates.
73 // Here we don't care so we allow coercion
74 ret = static_cast<float>( (*node).GetInteger() );
81 inline OptionalInteger IsInteger(const OptionalChild &node)
87 if( (*node).GetType() == TreeNode::INTEGER )
89 ret = (*node).GetInteger();
91 else if( (*node).GetType() == TreeNode::FLOAT )
93 ret = static_cast<int>( (*node).GetFloat() );
100 inline OptionalBoolean IsBoolean(const OptionalChild& node)
102 if( node && (*node).GetType() == TreeNode::BOOLEAN )
104 return OptionalBoolean(1 == (*node).GetInteger());
108 return OptionalBoolean();
113 template <typename T>
114 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
116 for(int i = 0; i < N; ++i)
118 if( (*iter).second.GetType() == TreeNode::FLOAT)
120 vector[i] = (*iter).second.GetFloat();
122 else if( (*iter).second.GetType() == TreeNode::INTEGER )
124 vector[i] = static_cast<float>((*iter).second.GetInteger());
136 inline OptionalVector4 IsVector4(const OptionalChild& node)
140 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
143 if( CopyNumbers((*node).CBegin(), 4, v) )
145 ret = OptionalVector4(v);
152 inline OptionalVector3 IsVector3(const OptionalChild& node)
156 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
159 if( CopyNumbers((*node).CBegin(), 3, v) )
161 ret = OptionalVector3(v);
168 inline OptionalVector2 IsVector2(const OptionalChild& node)
172 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
175 if( CopyNumbers((*node).CBegin(), 2, v) )
177 ret = OptionalVector2(v);
184 inline OptionalMatrix IsMatrix(const OptionalChild &node)
188 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
191 if( CopyNumbers((*node).CBegin(), 16, v) )
193 ret = OptionalMatrix(Dali::Matrix(v));
200 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
204 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
207 if( CopyNumbers((*node).CBegin(), 9, v) )
209 ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
218 inline OptionalRect IsRect(const OptionalChild& node)
221 if(node && (*node).Size())
223 if((*node).Size() >= 4)
225 TreeNode::ConstIterator iter((*node).CBegin());
227 if( CopyNumbers((*node).CBegin(), 4, v) )
229 ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
239 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
241 return IsString( IsChild(&parent, childName) );
244 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
246 return IsFloat( IsChild(&parent, childName) );
249 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
251 return IsInteger( IsChild(&parent, childName) );
254 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
256 return IsBoolean( IsChild(parent, childName) );
259 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
261 return IsVector4( IsChild(parent, childName) );
264 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
266 return IsVector3( IsChild(parent, childName) );
269 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
271 return IsVector2( IsChild(parent, childName) );
274 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
276 return IsMatrix( IsChild(parent, childName) );
279 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
281 return IsMatrix3( IsChild(&parent, childName) );
284 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
286 return IsRect( IsChild(&parent, childName) );
292 inline OptionalString IsString( const TreeNode& node )
294 return IsString( OptionalChild( node ) );
297 inline OptionalFloat IsFloat( const TreeNode& node )
299 return IsFloat( OptionalChild( node ) );
302 inline OptionalInteger IsInteger( const TreeNode& node )
304 return IsInteger( OptionalChild( node ) );
307 inline OptionalBoolean IsBoolean( const TreeNode& node )
309 return IsBoolean( OptionalChild( node ) );
312 inline OptionalVector4 IsVector4(const TreeNode &node )
314 return IsVector4( OptionalChild( node ) );
317 inline OptionalVector3 IsVector3(const TreeNode &node )
319 return IsVector3( OptionalChild( node ) );
322 inline OptionalVector2 IsVector2(const TreeNode &node )
324 return IsVector2( OptionalChild( node ) );
327 inline OptionalMatrix IsMatrix(const TreeNode &node )
329 return IsMatrix( OptionalChild( node ) );
332 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
334 return IsMatrix3( OptionalChild( node ) );
337 inline OptionalRect IsRect(const TreeNode &node )
339 return IsRect( OptionalChild( node ) );
345 inline Dali::Vector4 GetVector4(const TreeNode &child)
347 OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
348 DALI_ASSERT_ALWAYS(v);
352 inline Dali::Vector3 GetVector3(const TreeNode &child)
354 OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
355 DALI_ASSERT_ALWAYS(v);
359 inline Dali::Vector2 GetVector2(const TreeNode &child)
361 OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
362 DALI_ASSERT_ALWAYS(v);
366 inline float GetFloat(const TreeNode &child)
368 OptionalFloat v( IsFloat( OptionalChild( child ) ) );
369 DALI_ASSERT_ALWAYS(v);
373 inline bool GetBoolean(const TreeNode &child)
375 OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
376 DALI_ASSERT_ALWAYS(v);
380 inline int GetInteger(const TreeNode &child)
382 OptionalInteger v( IsInteger( OptionalChild( child ) ) );
383 DALI_ASSERT_ALWAYS(v);
389 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__