[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / tree-node.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <algorithm>
20 #include <cctype>
21 #include <string>
22 #include <string_view>
23
24 // INTERNAL INCLUDES
25 #include "dali-toolkit/devel-api/builder/tree-node.h"
26 #include "dali-toolkit/internal/builder/tree-node-manipulator.h"
27
28 namespace Dali
29 {
30 /**
31  * return true if the lower cased ASCII strings are equal.
32  */
33 bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b)
34 {
35   return (a.length() == b.length()) && std::equal(a.begin(), a.end(), b.begin(), [](auto x, auto y) {
36            return std::tolower(x) == std::tolower(y);
37          });
38 }
39
40 namespace Toolkit
41 {
42 TreeNode::TreeNode()
43 : mName(NULL),
44   mParent(NULL),
45   mNextSibling(NULL),
46   mFirstChild(NULL),
47   mLastChild(NULL),
48   mStringValue(NULL),
49   mType(TreeNode::IS_NULL),
50   mSubstituion(false)
51 {
52 }
53
54 TreeNode::~TreeNode()
55 {
56 }
57
58 const char* TreeNode::GetName() const
59 {
60   return mName;
61 }
62
63 TreeNode::NodeType TreeNode::GetType() const
64 {
65   return mType;
66 }
67
68 const char* TreeNode::GetString() const
69 {
70   return mStringValue;
71 }
72
73 bool TreeNode::HasSubstitution() const
74 {
75   return mSubstituion;
76 }
77
78 float TreeNode::GetFloat() const
79 {
80   return mFloatValue;
81 }
82
83 int TreeNode::GetInteger() const
84 {
85   return mIntValue;
86 }
87
88 bool TreeNode::GetBoolean() const
89 {
90   return mIntValue == 1 ? true : false;
91 }
92
93 size_t TreeNode::Size() const
94 {
95   size_t    c = 0;
96   TreeNode* p = mFirstChild;
97   while(p)
98   {
99     c++;
100     p = p->mNextSibling;
101   }
102   return c;
103 }
104
105 size_t TreeNode::Count(std::string_view childName) const
106 {
107   const TreeNode* c = GetChild(childName);
108   if(c)
109   {
110     return c->Size();
111   }
112   else
113   {
114     return 0;
115   }
116 }
117
118 const TreeNode* TreeNode::GetChild(std::string_view childName) const
119 {
120   const TreeNode* p = mFirstChild;
121   while(p)
122   {
123     if(p->mName && (std::string_view(p->mName) == childName))
124     {
125       return p;
126     }
127     p = p->mNextSibling;
128   }
129   return NULL;
130 }
131
132 const TreeNode* TreeNode::GetChildIgnoreCase(std::string_view childName) const
133 {
134   const TreeNode* p = mFirstChild;
135   while(p)
136   {
137     if(p->mName && CaseInsensitiveStringCompare(p->mName, childName))
138     {
139       return p;
140     }
141     p = p->mNextSibling;
142   }
143   return NULL;
144 }
145
146 const TreeNode* TreeNode::Find(std::string_view childName) const
147 {
148   if(mName && std::string_view(mName) == childName)
149   {
150     return this;
151   }
152   else
153   {
154     return Internal::FindIt(childName, this);
155   }
156 }
157
158 TreeNode::ConstIterator TreeNode::CBegin() const
159 {
160   return ConstIterator(mFirstChild);
161 }
162
163 TreeNode::ConstIterator TreeNode::CEnd() const
164 {
165   return ConstIterator(NULL);
166 }
167
168 TreeNode::ConstIterator::ConstIterator(TreeNode* v)
169 : mNode(v)
170 {
171 }
172
173 TreeNode::ConstIterator& TreeNode::ConstIterator::operator++()
174 {
175   if(mNode)
176   {
177     mNode = mNode->mNextSibling;
178   }
179   else
180   {
181     mNode = NULL;
182   }
183   return *this;
184 }
185
186 TreeNode::ConstIterator TreeNode::ConstIterator::operator++(int)
187 {
188   TreeNode::ConstIterator ret(mNode);
189
190   if(mNode)
191   {
192     mNode = mNode->mNextSibling;
193   }
194   else
195   {
196     mNode = NULL;
197   }
198   return ret;
199 }
200
201 TreeNode::KeyNodePair TreeNode::ConstIterator::operator*()
202 {
203   return KeyNodePair(mNode->mName, *mNode);
204 }
205
206 bool TreeNode::ConstIterator::operator!=(const TreeNode::ConstIterator& rhs) const
207 {
208   return mNode != rhs.mNode;
209 }
210
211 } // namespace Toolkit
212
213 } // namespace Dali