Merge "Dont register accessibility and keyboard focus properties as scene graph prope...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / tree-node-manipulator.h
1 #ifndef __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__
2 #define __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__
3
4 /*
5  * Copyright (c) 2014 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 <cstring>
25
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/builder/tree-node.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40 typedef std::vector<char> VectorChar;
41 typedef VectorChar::iterator VectorCharIter;
42
43 /*
44  * TreeNodeManipulator performs modification operations on a TreeNode which are
45  * otherwise prohibited on the TreeNode public interface.
46  */
47 class TreeNodeManipulator
48 {
49 public:
50   /*
51    * Constructor
52    * @param node The TreeNode to modify
53    */
54   explicit TreeNodeManipulator(TreeNode* node);
55
56   /*
57    * Create a new TreeNode instance
58    * @return new TreeNode
59    */
60   static TreeNode* NewTreeNode();
61
62   /*
63    * Shallow copy node data
64    * Shallow copy the data but doesnt parent or copy children
65    * @param from Node to copy from
66    * @param to Node to copy to
67    */
68   static void ShallowCopy(const TreeNode* from, TreeNode* to);
69
70   /*
71    * Moves all string data to a new buffer. There must be enough space for all string data.
72    * @param start The buffer start
73    * @param sentinel The end of the buffer
74    */
75   void MoveStrings(VectorCharIter& start, const VectorCharIter& sentinel);
76
77   /*
78    * Remove all children from the node
79    */
80   void RemoveChildren();
81
82   /*
83    * Make a deep copy of the tree.
84    * @param tree The tree to copy
85    * @param numberOfNodes The number of nodes that were copied
86    * @param numberOfChars The size of string data.
87    */
88   static TreeNode* Copy(const TreeNode& tree, int& numberOfNodes, int& numberOfChars);
89
90   /*
91    * Add child to the node
92    * @param child The child to add
93    * @return the added child
94    */
95   TreeNode *AddChild(TreeNode *child);
96
97   /*
98    * Change the type of the Node
99    * NB: If the type changes from a type with children to a value type without children then
100    *     the children are removed
101    * @param type The new type
102    */
103   void SetType( TreeNode::NodeType type);
104
105   /*
106    * Set the name of the node
107    * @param name The name to set
108    */
109   void SetName( const char* name );
110
111   /*
112    * Set the substituion flag
113    * The substitution flag indicates this nodes string value contains a reference to another node
114    * in the tree.
115    * @param on The state
116    */
117   void SetSubstitution( bool on );
118
119   /*
120    * Get the nodes type
121    * @return The nodes type
122    */
123   TreeNode::NodeType GetType() const;
124
125   /*
126    * Get the number of children of the node
127    * @return The number of children
128    */
129   size_t Size() const;
130
131   /*
132    * Set the node as a string value
133    * @param string The string value
134    */
135   void SetString( const char* string );
136
137   /*
138    * Set the node as an integer value
139    * @param i The integer
140    */
141   void SetInteger( int i );
142
143   /*
144    * Set the node as an float value
145    * @param f The float
146    */
147   void SetFloat( float f );
148
149   /*
150    * Set the node as an boolean value
151    * @param b The boolean
152    */
153   void SetBoolean( bool b );
154
155   /*
156    * Get the nodes parent
157    * @return The nodes parent
158    */
159   TreeNode* GetParent() const;
160
161   /*
162    * Get the nodes child by name
163    * @param name The childs name
164    * @return The nodes if found, else NULL
165    */
166   const TreeNode* GetChild(const std::string& name) const;
167
168   /*
169    * @copydoc Dali::Scripting::JsonParser::Write()
170    */
171   void Write(std::ostream& output, int indent) const;
172
173 private:
174   TreeNode *mNode;
175
176   /*
177    * Move the nodes strings to the buffer
178    */
179   void MoveNodeStrings(VectorCharIter& start, const VectorCharIter& sentinel);
180
181   /*
182    * Recursively move child strings to the buffer
183    */
184   void RecurseMoveChildStrings(VectorCharIter& start, const VectorCharIter& sentinel);
185
186   /*
187    * Recursively copy children
188    */
189   static void CopyChildren(const TreeNode* from, TreeNode* to, int& numberNodes, int& numberChars);
190
191   /*
192    * Do write to string stream
193    */
194   void DoWrite(const TreeNode *value, std::ostream& output, int ident) const;
195
196 };
197
198 /*
199  * Collect nodes
200  */
201 struct CollectNodes : public std::unary_function<TreeNode*, void>
202 {
203   CollectNodes() {};
204
205   /*
206    * Call operator to add nodes to the list
207    */
208   result_type operator()(argument_type& n)
209   {
210     DALI_ASSERT_DEBUG(n && "Operation on NULL JSON node");
211     nodes.push_back(n);
212   }
213
214   typedef std::vector<const TreeNode*> VectorNodes;
215   typedef VectorNodes::iterator iterator;
216
217   VectorNodes nodes; ///< List of collected nodes
218 };
219
220 /*
221  * Depth first walk of nodes applying given operation (unary_function)
222  */
223 template <typename Operation>
224 void DepthFirst( TreeNode* node, Operation& operation)
225 {
226   DALI_ASSERT_DEBUG(node && "Operation on NULL JSON node");
227
228   for(TreeNode::ConstIterator iter = node->CBegin(); iter != node->CEnd(); ++iter)
229   {
230     // iterator access is const for external api but were modifying
231     DepthFirst( const_cast<TreeNode*>(&((*iter).second)), operation);
232   }
233
234   operation(node);
235
236 }
237
238 /*
239  * Recursive search on the tree for the child with the given name
240  * @param childName The name to find
241  * @param tree The tree to search
242  * @return the TreeNode if found, else NULL
243  */
244 const TreeNode* FindIt(const std::string& childName, const TreeNode* tree);
245
246 /*
247  * Copy string to a buffer
248  * Raises if there is not enough space in the buffer
249  * @param fromString The string
250  * @param iter The start of the buffer
251  * @param sentinel The buffer sentinel
252  * @return The start of the given buffer
253  */
254 char *CopyString( const char *fromString, VectorCharIter& iter, const VectorCharIter& sentinel);
255
256
257 } // namespace Internal
258
259 } // namespace Toolkit
260
261 } // namespace Dali
262
263 #endif // __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__