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();
61 inline OptionalFloat IsFloat(const OptionalChild& node)
67 if( (*node).GetType() == TreeNode::FLOAT )
69 ret = (*node).GetFloat();
71 else if( (*node).GetType() == TreeNode::INTEGER )
73 // JSON has number not float/int but JsonParser discriminates.
74 // Here we don't care so we allow coercion
75 ret = static_cast<float>( (*node).GetInteger() );
82 inline OptionalInteger IsInteger(const OptionalChild &node)
88 if( (*node).GetType() == TreeNode::INTEGER )
90 ret = (*node).GetInteger();
92 else if( (*node).GetType() == TreeNode::FLOAT )
94 ret = static_cast<int>( (*node).GetFloat() );
101 inline OptionalBoolean IsBoolean(const OptionalChild& node)
103 if( node && (*node).GetType() == TreeNode::BOOLEAN )
105 return OptionalBoolean(1 == (*node).GetInteger());
109 return OptionalBoolean();
115 template <typename T>
116 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
118 for(int i = 0; i < N; ++i)
120 if( (*iter).second.GetType() == TreeNode::FLOAT)
122 vector[i] = (*iter).second.GetFloat();
124 else if( (*iter).second.GetType() == TreeNode::INTEGER )
126 vector[i] = static_cast<float>((*iter).second.GetInteger());
138 inline OptionalVector4 IsVector4(const OptionalChild& node)
142 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
145 if( CopyNumbers((*node).CBegin(), 4, v) )
147 ret = OptionalVector4(v);
154 inline OptionalVector3 IsVector3(const OptionalChild& node)
158 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
161 if( CopyNumbers((*node).CBegin(), 3, v) )
163 ret = OptionalVector3(v);
170 inline OptionalVector2 IsVector2(const OptionalChild& node)
174 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
177 if( CopyNumbers((*node).CBegin(), 2, v) )
179 ret = OptionalVector2(v);
186 inline OptionalMatrix IsMatrix(const OptionalChild &node)
190 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
193 if( CopyNumbers((*node).CBegin(), 16, v) )
195 ret = OptionalMatrix(Dali::Matrix(v));
202 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
206 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
209 if( CopyNumbers((*node).CBegin(), 9, v) )
211 ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
220 inline OptionalRect IsRect(const OptionalChild& node)
223 if(node && (*node).Size())
225 if((*node).Size() >= 4)
227 TreeNode::ConstIterator iter((*node).CBegin());
229 if( CopyNumbers((*node).CBegin(), 4, v) )
231 ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
241 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
243 return IsString( IsChild(&parent, childName) );
246 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
248 return IsFloat( IsChild(&parent, childName) );
251 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
253 return IsInteger( IsChild(&parent, childName) );
256 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
258 return IsBoolean( IsChild(parent, childName) );
261 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
263 return IsVector4( IsChild(parent, childName) );
266 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
268 return IsVector3( IsChild(parent, childName) );
271 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
273 return IsVector2( IsChild(parent, childName) );
276 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
278 return IsMatrix( IsChild(parent, childName) );
281 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
283 return IsMatrix3( IsChild(&parent, childName) );
286 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
288 return IsRect( IsChild(&parent, childName) );
294 inline OptionalString IsString( const TreeNode& node )
296 return IsString( OptionalChild( node ) );
299 inline OptionalFloat IsFloat( const TreeNode& node )
301 return IsFloat( OptionalChild( node ) );
304 inline OptionalInteger IsInteger( const TreeNode& node )
306 return IsInteger( OptionalChild( node ) );
309 inline OptionalBoolean IsBoolean( const TreeNode& node )
311 return IsBoolean( OptionalChild( node ) );
314 inline OptionalVector4 IsVector4(const TreeNode &node )
316 return IsVector4( OptionalChild( node ) );
319 inline OptionalVector3 IsVector3(const TreeNode &node )
321 return IsVector3( OptionalChild( node ) );
324 inline OptionalVector2 IsVector2(const TreeNode &node )
326 return IsVector2( OptionalChild( node ) );
329 inline OptionalMatrix IsMatrix(const TreeNode &node )
331 return IsMatrix( OptionalChild( node ) );
334 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
336 return IsMatrix3( OptionalChild( node ) );
339 inline OptionalRect IsRect(const TreeNode &node )
341 return IsRect( OptionalChild( node ) );
347 inline Dali::Vector4 GetVector4(const TreeNode &child)
349 OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
350 DALI_ASSERT_ALWAYS(v);
354 inline Dali::Vector3 GetVector3(const TreeNode &child)
356 OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
357 DALI_ASSERT_ALWAYS(v);
361 inline Dali::Vector2 GetVector2(const TreeNode &child)
363 OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
364 DALI_ASSERT_ALWAYS(v);
368 inline float GetFloat(const TreeNode &child)
370 OptionalFloat v( IsFloat( OptionalChild( child ) ) );
371 DALI_ASSERT_ALWAYS(v);
375 inline bool GetBoolean(const TreeNode &child)
377 OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
378 DALI_ASSERT_ALWAYS(v);
382 inline int GetInteger(const TreeNode &child)
384 OptionalInteger v( IsInteger( OptionalChild( child ) ) );
385 DALI_ASSERT_ALWAYS(v);
391 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__