1 #ifndef __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__
2 #define __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <utility> // pair
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/vector-wrapper.h>
30 #include <dali-toolkit/devel-api/builder/tree-node.h>
40 typedef std::vector<char> VectorChar;
41 typedef VectorChar::iterator VectorCharIter;
44 * TreeNodeManipulator performs modification operations on a TreeNode which are
45 * otherwise prohibited on the TreeNode public interface.
47 class TreeNodeManipulator
52 * @param node The TreeNode to modify
54 explicit TreeNodeManipulator(TreeNode* node);
57 * Create a new TreeNode instance
58 * @return new TreeNode
60 static TreeNode* NewTreeNode();
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
68 static void ShallowCopy(const TreeNode* from, TreeNode* to);
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
75 void MoveStrings(VectorCharIter& start, const VectorCharIter& sentinel);
78 * Remove all children from the node
80 void RemoveChildren();
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.
88 static TreeNode* Copy(const TreeNode& tree, int& numberOfNodes, int& numberOfChars);
91 * Add child to the node
92 * @param child The child to add
93 * @return the added child
95 TreeNode *AddChild(TreeNode *child);
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
103 void SetType( TreeNode::NodeType type);
106 * Set the name of the node
107 * @param name The name to set
109 void SetName( const char* name );
112 * Set the substituion flag
113 * The substitution flag indicates this nodes string value contains a reference to another node
115 * @param on The state
117 void SetSubstitution( bool on );
121 * @return The nodes type
123 TreeNode::NodeType GetType() const;
126 * Get the number of children of the node
127 * @return The number of children
132 * Set the node as a string value
133 * @param string The string value
135 void SetString( const char* string );
138 * Set the node as an integer value
139 * @param i The integer
141 void SetInteger( int i );
144 * Set the node as an float value
147 void SetFloat( float f );
150 * Set the node as an boolean value
151 * @param b The boolean
153 void SetBoolean( bool b );
156 * Get the nodes parent
157 * @return The nodes parent
159 TreeNode* GetParent() const;
162 * Get the nodes child by name
163 * @param name The childs name
164 * @return The nodes if found, else NULL
166 const TreeNode* GetChild(const std::string& name) const;
169 * @copydoc Dali::Scripting::JsonParser::Write()
171 void Write(std::ostream& output, int indent) const;
177 * Move the nodes strings to the buffer
179 void MoveNodeStrings(VectorCharIter& start, const VectorCharIter& sentinel);
182 * Recursively move child strings to the buffer
184 void RecurseMoveChildStrings(VectorCharIter& start, const VectorCharIter& sentinel);
187 * Recursively copy children
189 static void CopyChildren(const TreeNode* from, TreeNode* to, int& numberNodes, int& numberChars);
192 * Do write to string stream
194 void DoWrite(const TreeNode *value, std::ostream& output, int level, int ident, bool groupChildren) const;
201 struct CollectNodes : public std::unary_function<TreeNode*, void>
206 * Call operator to add nodes to the list
208 result_type operator()(argument_type& n)
210 DALI_ASSERT_DEBUG(n && "Operation on NULL JSON node");
214 typedef std::vector<const TreeNode*> VectorNodes;
215 typedef VectorNodes::iterator iterator;
217 VectorNodes nodes; ///< List of collected nodes
221 * Depth first walk of nodes applying given operation (unary_function)
223 template <typename Operation>
224 void DepthFirst( TreeNode* node, Operation& operation)
226 DALI_ASSERT_DEBUG(node && "Operation on NULL JSON node");
228 for(TreeNode::ConstIterator iter = node->CBegin(); iter != node->CEnd(); ++iter)
230 // iterator access is const for external api but were modifying
231 DepthFirst( const_cast<TreeNode*>(&((*iter).second)), operation);
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
244 const TreeNode* FindIt(const std::string& childName, const TreeNode* tree);
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
254 char *CopyString( const char *fromString, VectorCharIter& iter, const VectorCharIter& sentinel);
257 } // namespace Internal
259 } // namespace Toolkit
263 #endif // __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__