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