e7e511e7c29f995df8f71fd693a6e2d6818a3c7e
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / tree-node.h
1 #ifndef DALI_SCRIPT_TREE_NODE_H
2 #define DALI_SCRIPT_TREE_NODE_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <iterator>
24 #include <string>
25 #include <utility> // pair
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Internal DALI_INTERNAL
32 {
33 class TreeNodeManipulator;
34
35 } // namespace DALI_INTERNAL
36
37 /*
38  * TreeNode describes a tree of nodes.
39  * TreeNode does not own its string data which is held by a container eg JsonParser
40  * Modification operations should be done through a container.
41  */
42 class DALI_TOOLKIT_API TreeNode
43 {
44 public:
45   /*
46    * enum typedef describing the node type
47    */
48   enum NodeType
49   {
50     IS_NULL,
51     OBJECT,
52     ARRAY,
53     STRING,
54     INTEGER,
55     FLOAT,
56     BOOLEAN
57   };
58
59   /*
60    * Non virtual destructor; this class should not be inherited from.
61    */
62   ~TreeNode();
63
64   typedef std::pair<const char*, const TreeNode&> KeyNodePair;
65
66   /*
67    * Iterator to iterate through children
68    */
69   class DALI_TOOLKIT_API ConstIterator
70   {
71   public:
72     typedef KeyNodePair               value_type;
73     typedef KeyNodePair*              pointer;
74     typedef const KeyNodePair*        const_pointer;
75     typedef KeyNodePair&              reference;
76     typedef const KeyNodePair&        const_reference;
77     typedef size_t                    size_type;
78     typedef std::ptrdiff_t            difference_type;
79     typedef std::forward_iterator_tag iterator_category;
80
81     /*
82      * constructor
83      */
84     explicit ConstIterator(TreeNode* v);
85
86     /*
87      * pre increment
88      */
89     ConstIterator& operator++();
90
91     /*
92      * post increment
93      */
94     ConstIterator operator++(int);
95
96     /*
97      * != test
98      */
99     bool operator!=(const ConstIterator& rhs) const;
100
101     /*
102      * pointer semantics
103      */
104     KeyNodePair operator*();
105
106   private:
107     TreeNode* mNode;
108   };
109
110   /*
111    * Iterate begin() over the nodes children
112    * @return a const interator
113    */
114   ConstIterator CBegin() const;
115
116   /*
117    * Iterate end()
118    * @return a const interator
119    */
120   ConstIterator CEnd() const;
121
122   /*
123    * Size (number of children)
124    * @return The number of children
125    */
126   size_t Size() const;
127
128   /*
129    * Count (the number of children of a sub child node)
130    * @param childName The name of the child to find
131    * @return the number of children in the found child
132    */
133   size_t Count(const std::string& childName) const;
134
135   /*
136    * Get the nodes name
137    * @return The nodes name
138    */
139   const char* GetName() const;
140
141   /*
142    * Gets the nodes type
143    * @return The nodes type
144    */
145   NodeType GetType() const;
146
147   /*
148    * Gets the nodes string value
149    * Only valid if the type == TreeNode::STRING
150    * @return The string value
151    */
152   const char* GetString() const;
153
154   /*
155    * Gets the nodes float value
156    * Only valid if the type == TreeNode::FLOAT
157    * @return The float value
158    */
159   float GetFloat() const;
160
161   /*
162    * Gets the nodes integer value
163    * Only valid if the type == TreeNode::INTEGER
164    * @return The integer value
165    */
166   int GetInteger() const;
167
168   /*
169    * Gets the nodes boolean value
170    * Only valid if the type == TreeNode::BOOLEAN
171    * @return The boolean value
172    */
173   bool GetBoolean() const;
174
175   /*
176    * Gets the substituion flag
177    * Only valid if the type == TreeNode::STRING
178    * @return The substitution flag
179    */
180   bool HasSubstitution() const;
181
182   /*
183    * Gets a child of the node (using case sensitive matching)
184    * @param name The name of the child.
185    * @return The child if found, else NULL
186    */
187   const TreeNode* GetChild(const std::string& name) const;
188
189   /*
190    * Gets a child of the node (using case insensitive matching)
191    * @param name The name of the child in lower case
192    * @return The child if found, else NULL
193    */
194   const TreeNode* GetChildIgnoreCase(const std::string& name) const;
195
196   /*
197    * Recursively search for a child of the node
198    * @param name The name of the child
199    * @return The child if found, else NULL
200    */
201   const TreeNode* Find(const std::string& name) const;
202
203 private:
204   friend class Internal::TreeNodeManipulator;
205
206   /*
207    * Constructor
208    */
209   DALI_INTERNAL TreeNode();
210
211   // non copyable or assignable
212   DALI_INTERNAL TreeNode(TreeNode&);
213   DALI_INTERNAL TreeNode& operator=(const TreeNode&);
214
215   const char* mName; ///< The nodes name (if any)
216
217   TreeNode* mParent;      ///< The nodes parent
218   TreeNode* mNextSibling; ///< The nodes next sibling
219   TreeNode* mFirstChild;  ///< The nodes first child
220   TreeNode* mLastChild;   ///< The nodes last child
221
222   union
223   {
224     const char* mStringValue; ///< The node string value
225     int         mIntValue;    ///< The node integer value
226     float       mFloatValue;  ///< The node float value
227   };
228
229   NodeType mType;        ///< The nodes type
230   bool     mSubstituion; ///< String substitution flag
231 };
232
233 } // namespace Toolkit
234
235 } // namespace Dali
236
237 #endif // DALI_SCRIPT_TREE_NODE_H