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