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 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
11 // http://floralicense.org/license/
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.
20 #include <utility> // pair
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali-toolkit/public-api/builder/tree-node.h>
29 namespace Dali DALI_IMPORT_API
35 namespace Internal DALI_INTERNAL
37 typedef std::vector<char> VectorChar;
38 typedef VectorChar::iterator VectorCharIter;
41 * TreeNodeManipulator performs modification operations on a TreeNode which are
42 * otherwise prohibited on the TreeNode public interface.
44 class TreeNodeManipulator
49 * @param node The TreeNode to modify
51 explicit TreeNodeManipulator(TreeNode* node);
54 * Create a new TreeNode instance
55 * @return new TreeNode
57 static TreeNode* NewTreeNode();
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
65 static void ShallowCopy(const TreeNode* from, TreeNode* to);
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
72 void MoveStrings(VectorCharIter& start, const VectorCharIter& sentinel);
75 * Remove all children from the node
77 void RemoveChildren();
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.
85 static TreeNode* Copy(const TreeNode& tree, int& numberOfNodes, int& numberOfChars);
88 * Add child to the node
89 * @param child The child to add
90 * @return the added child
92 TreeNode *AddChild(TreeNode *child);
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
100 void SetType( TreeNode::NodeType type);
103 * Set the name of the node
104 * @param name The name to set
106 void SetName( const char* name );
109 * Set the substituion flag
110 * The substitution flag indicates this nodes string value contains a reference to another node
112 * @param on The state
114 void SetSubstitution( bool on );
118 * @return The nodes type
120 TreeNode::NodeType GetType() const;
123 * Get the number of children of the node
124 * @return The number of children
129 * Set the node as a string value
130 * @param string The string value
132 void SetString( const char* string );
135 * Set the node as an integer value
136 * @param i The integer
138 void SetInteger( int i );
141 * Set the node as an float value
144 void SetFloat( float f );
147 * Set the node as an boolean value
148 * @param b The boolean
150 void SetBoolean( bool b );
153 * Get the nodes parent
154 * @return The nodes parent
156 TreeNode* GetParent() const;
159 * Get the nodes child by name
160 * @param name The childs name
161 * @return The nodes if found, else NULL
163 const TreeNode* GetChild(const std::string& name) const;
166 * @copydoc Dali::Scripting::JsonParser::Write()
168 void Write(std::ostream& output, int indent) const;
174 * Move the nodes strings to the buffer
176 void MoveNodeStrings(VectorCharIter& start, const VectorCharIter& sentinel);
179 * Recursively move child strings to the buffer
181 void RecurseMoveChildStrings(VectorCharIter& start, const VectorCharIter& sentinel);
184 * Recursively copy children
186 static void CopyChildren(const TreeNode* from, TreeNode* to, int& numberNodes, int& numberChars);
189 * Do write to string stream
191 void DoWrite(const TreeNode *value, std::ostream& output, int ident) const;
198 struct CollectNodes : public std::unary_function<TreeNode*, void>
203 * Call operator to add nodes to the list
205 result_type operator()(argument_type& n)
207 DALI_ASSERT_DEBUG(n && "Operation on NULL JSON node");
211 typedef std::vector<const TreeNode*> VectorNodes;
212 typedef VectorNodes::iterator iterator;
214 VectorNodes nodes; ///< List of collected nodes
218 * Depth first walk of nodes applying given operation (unary_function)
220 template <typename Operation>
221 void DepthFirst( TreeNode* node, Operation& operation)
223 DALI_ASSERT_DEBUG(node && "Operation on NULL JSON node");
225 for(TreeNode::ConstIterator iter = node->CBegin(); iter != node->CEnd(); ++iter)
227 // iterator access is const for external api but were modifying
228 DepthFirst( const_cast<TreeNode*>(&((*iter).second)), operation);
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
241 const TreeNode* FindIt(const std::string& childName, const TreeNode* tree);
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
251 char *CopyString( const char *fromString, VectorCharIter& iter, const VectorCharIter& sentinel);
254 } // namespace Internal
256 } // namespace Toolkit
260 #endif // __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__