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 Flora License, Version 1.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://floralicense.org/license/
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.
21 #include <dali-toolkit/internal/builder/builder-declarations.h>
23 inline OptionalChild IsChild(const TreeNode* node, const std::string& childName)
27 const TreeNode* c = node->GetChild(childName);
30 return OptionalChild( *c );
34 return OptionalChild();
39 return OptionalChild();
43 inline OptionalChild IsChild(const TreeNode& node, const std::string& childName)
45 return IsChild(&node, childName);
48 inline OptionalString IsString(const OptionalChild& node)
50 if( node && (*node).GetType() == TreeNode::STRING )
52 return OptionalString((*node).GetString());
56 return OptionalString();
59 inline OptionalFloat IsFloat(const OptionalChild& node)
65 if( (*node).GetType() == TreeNode::FLOAT )
67 ret = (*node).GetFloat();
69 else if( (*node).GetType() == TreeNode::INTEGER )
71 // JSON has number not float/int but JsonParser discriminates.
72 // Here we don't care so we allow coercion
73 ret = static_cast<float>( (*node).GetInteger() );
80 inline OptionalInteger IsInteger(const OptionalChild &node)
86 if( (*node).GetType() == TreeNode::INTEGER )
88 ret = (*node).GetInteger();
90 else if( (*node).GetType() == TreeNode::FLOAT )
92 ret = static_cast<int>( (*node).GetFloat() );
99 inline OptionalBoolean IsBoolean(const OptionalChild& node)
101 if( node && (*node).GetType() == TreeNode::BOOLEAN )
103 return OptionalBoolean(1 == (*node).GetInteger());
107 return OptionalBoolean();
112 template <typename T>
113 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
115 for(int i = 0; i < N; ++i)
117 if( (*iter).second.GetType() == TreeNode::FLOAT)
119 vector[i] = (*iter).second.GetFloat();
121 else if( (*iter).second.GetType() == TreeNode::INTEGER )
123 vector[i] = static_cast<float>((*iter).second.GetInteger());
135 inline OptionalVector4 IsVector4(const OptionalChild& node)
139 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
142 if( CopyNumbers((*node).CBegin(), 4, v) )
144 ret = OptionalVector4(v);
151 inline OptionalVector3 IsVector3(const OptionalChild& node)
155 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
158 if( CopyNumbers((*node).CBegin(), 3, v) )
160 ret = OptionalVector3(v);
167 inline OptionalVector2 IsVector2(const OptionalChild& node)
171 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
174 if( CopyNumbers((*node).CBegin(), 2, v) )
176 ret = OptionalVector2(v);
183 inline OptionalMatrix IsMatrix(const OptionalChild &node)
187 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
190 if( CopyNumbers((*node).CBegin(), 16, v) )
192 ret = OptionalMatrix(Dali::Matrix(v));
199 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
203 if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
206 if( CopyNumbers((*node).CBegin(), 9, v) )
208 ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
217 inline OptionalRect IsRect(const OptionalChild& node)
220 if(node && (*node).Size())
222 if((*node).Size() >= 4)
224 TreeNode::ConstIterator iter((*node).CBegin());
226 if( CopyNumbers((*node).CBegin(), 4, v) )
228 ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
238 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
240 return IsString( IsChild(&parent, childName) );
243 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
245 return IsFloat( IsChild(&parent, childName) );
248 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
250 return IsInteger( IsChild(&parent, childName) );
253 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
255 return IsBoolean( IsChild(parent, childName) );
258 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
260 return IsVector4( IsChild(parent, childName) );
263 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
265 return IsVector3( IsChild(parent, childName) );
268 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
270 return IsVector2( IsChild(parent, childName) );
273 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
275 return IsMatrix( IsChild(parent, childName) );
278 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
280 return IsMatrix3( IsChild(&parent, childName) );
283 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
285 return IsRect( IsChild(&parent, childName) );
291 inline OptionalString IsString( const TreeNode& node )
293 return IsString( OptionalChild( node ) );
296 inline OptionalFloat IsFloat( const TreeNode& node )
298 return IsFloat( OptionalChild( node ) );
301 inline OptionalInteger IsInteger( const TreeNode& node )
303 return IsInteger( OptionalChild( node ) );
306 inline OptionalBoolean IsBoolean( const TreeNode& node )
308 return IsBoolean( OptionalChild( node ) );
311 inline OptionalVector4 IsVector4(const TreeNode &node )
313 return IsVector4( OptionalChild( node ) );
316 inline OptionalVector3 IsVector3(const TreeNode &node )
318 return IsVector3( OptionalChild( node ) );
321 inline OptionalVector2 IsVector2(const TreeNode &node )
323 return IsVector2( OptionalChild( node ) );
326 inline OptionalMatrix IsMatrix(const TreeNode &node )
328 return IsMatrix( OptionalChild( node ) );
331 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
333 return IsMatrix3( OptionalChild( node ) );
336 inline OptionalRect IsRect(const TreeNode &node )
338 return IsRect( OptionalChild( node ) );
344 inline Dali::Vector4 GetVector4(const TreeNode &child)
346 OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
347 DALI_ASSERT_ALWAYS(v);
351 inline Dali::Vector3 GetVector3(const TreeNode &child)
353 OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
354 DALI_ASSERT_ALWAYS(v);
358 inline Dali::Vector2 GetVector2(const TreeNode &child)
360 OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
361 DALI_ASSERT_ALWAYS(v);
365 inline float GetFloat(const TreeNode &child)
367 OptionalFloat v( IsFloat( OptionalChild( child ) ) );
368 DALI_ASSERT_ALWAYS(v);
372 inline bool GetBoolean(const TreeNode &child)
374 OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
375 DALI_ASSERT_ALWAYS(v);
379 inline int GetInteger(const TreeNode &child)
381 OptionalInteger v( IsInteger( OptionalChild( child ) ) );
382 DALI_ASSERT_ALWAYS(v);
388 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__