[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / builder / tree-node.h
1 #ifndef __DALI_SCRIPT_TREE_NODE_H__
2 #define __DALI_SCRIPT_TREE_NODE_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
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
10 //
11 //     http://floralicense.org/license/
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 #include <utility> // pair
21 #include <iterator>
22 #include <vector>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26
27 namespace Dali DALI_IMPORT_API
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 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 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
110     /*
111      * pointer semantics
112      */
113     KeyNodePair operator->();
114   private:
115     TreeNode* mNode;
116   };
117
118   /*
119    * Iterate begin() over the nodes children
120    * @return a const interator
121    */
122   ConstIterator CBegin() const;
123
124   /*
125    * Iterate end()
126    * @return a const interator
127    */
128   ConstIterator CEnd() const;
129
130   /*
131    * Size (number of children)
132    * @return The number of children
133    */
134   size_t Size() const;
135
136   /*
137    * Count (the number of children of a sub child node)
138    * @param childName The name of the child to find
139    * @return the number of children in the found child
140    */
141   size_t Count(const std::string& childName) const;
142
143   /*
144    * Get the nodes name
145    * @return The nodes name
146    */
147   const char* GetName() const;
148
149   /*
150    * Gets the nodes type
151    * @return The nodes type
152    */
153   NodeType GetType() const;
154
155   /*
156    * Gets the nodes string value
157    * Only valid if the type == TreeNode::STRING
158    * @return The string value
159    */
160   const char* GetString() const;
161
162   /*
163    * Gets the nodes float value
164    * Only valid if the type == TreeNode::FLOAT
165    * @return The float value
166    */
167   float GetFloat() const;
168
169   /*
170    * Gets the nodes integer value
171    * Only valid if the type == TreeNode::INTEGER
172    * @return The integer value
173    */
174   int GetInteger() const;
175
176   /*
177    * Gets the nodes boolean value
178    * Only valid if the type == TreeNode::BOOLEAN
179    * @return The boolean value
180    */
181   bool GetBoolean() const;
182
183   /*
184    * Gets the substituion flag
185    * Only valid if the type == TreeNode::STRING
186    * @return The substitution flag
187    */
188   bool HasSubstitution() const;
189
190   /*
191    * Gets a child of the node
192    * @param name The name of the child
193    * @return The child if found, else NULL
194    */
195   const TreeNode* GetChild(const std::string& name) const;
196
197   /*
198    * Recursively search for a child of the node
199    * @param name The name of the child
200    * @return The child if found, else NULL
201    */
202   const TreeNode* Find(const std::string& name) const;
203
204 private:
205   friend class Internal::TreeNodeManipulator;
206
207   /*
208    * Constructor
209    */
210   TreeNode();
211
212   // non copyable or assignable
213   TreeNode(TreeNode &);
214   TreeNode& operator=(const TreeNode&);
215
216   const char* mName;                   ///< The nodes name (if any)
217
218   TreeNode* mParent;                   ///< The nodes parent
219   TreeNode* mNextSibling;              ///< The nodes next sibling
220   TreeNode* mFirstChild;               ///< The nodes first child
221   TreeNode* mLastChild;                ///< The nodes last child
222
223   union
224   {
225     const char* mStringValue;          ///< The node string value
226     int mIntValue;                     ///< The node integer value
227     float mFloatValue;                 ///< The node float value
228   };
229
230   NodeType mType;                      ///< The nodes type
231   bool mSubstituion;                   ///< String substitution flag
232
233 };
234
235 } // namespace Toolkit
236
237 } // namespace Dali
238
239 #endif // __DALI_SCRIPT_TREE_NODE_H__