1 #ifndef DALI_SCRIPT_TREE_NODE_MANIPULATOR_H
2 #define DALI_SCRIPT_TREE_NODE_MANIPULATOR_H
5 * Copyright (c) 2021 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.
24 #include <utility> // pair
26 #include <dali-toolkit/public-api/dali-toolkit-common.h>
27 #include <dali/public-api/common/vector-wrapper.h>
30 #include <dali-toolkit/devel-api/builder/tree-node.h>
38 typedef std::vector<char> VectorChar;
39 typedef VectorChar::iterator VectorCharIter;
42 * TreeNodeManipulator performs modification operations on a TreeNode which are
43 * otherwise prohibited on the TreeNode public interface.
45 class TreeNodeManipulator
50 * @param node The TreeNode to modify
52 explicit TreeNodeManipulator(TreeNode* node);
55 * Create a new TreeNode instance
56 * @return new TreeNode
58 static TreeNode* NewTreeNode();
61 * Shallow copy node data
62 * Shallow copy the data but doesnt parent or copy children
63 * @param from Node to copy from
64 * @param to Node to copy to
66 static void ShallowCopy(const TreeNode* from, TreeNode* to);
69 * Moves all string data to a new buffer. There must be enough space for all string data.
70 * @param start The buffer start
71 * @param sentinel The end of the buffer
73 void MoveStrings(VectorCharIter& start, const VectorCharIter& sentinel);
76 * Remove all children from the node
78 void RemoveChildren();
81 * Make a deep copy of the tree.
82 * @param tree The tree to copy
83 * @param numberOfNodes The number of nodes that were copied
84 * @param numberOfChars The size of string data.
86 static TreeNode* Copy(const TreeNode& tree, int& numberOfNodes, int& numberOfChars);
89 * Add child to the node
90 * @param child The child to add
91 * @return the added child
93 TreeNode* AddChild(TreeNode* child);
96 * Change the type of the Node
97 * NB: If the type changes from a type with children to a value type without children then
98 * the children are removed
99 * @param type The new type
101 void SetType(TreeNode::NodeType type);
104 * Set the name of the node
105 * @param name The name to set
107 void SetName(const char* name);
110 * Set the substituion flag
111 * The substitution flag indicates this nodes string value contains a reference to another node
113 * @param on The state
115 void SetSubstitution(bool on);
119 * @return The nodes type
121 TreeNode::NodeType GetType() const;
124 * Get the number of children of the node
125 * @return The number of children
130 * Set the node as a string value
131 * @param string The string value
133 void SetString(const char* string);
136 * Set the node as an integer value
137 * @param i The integer
139 void SetInteger(int i);
142 * Set the node as an float value
145 void SetFloat(float f);
148 * Set the node as an boolean value
149 * @param b The boolean
151 void SetBoolean(bool b);
154 * Get the nodes parent
155 * @return The nodes parent
157 TreeNode* GetParent() const;
160 * Get the nodes child by name
161 * @param name The childs name
162 * @return The nodes if found, else NULL
164 const TreeNode* GetChild(const std::string& name) const;
167 * @copydoc Dali::Scripting::JsonParser::Write()
169 void Write(std::ostream& output, int indent) const;
175 * Move the nodes strings to the buffer
177 void MoveNodeStrings(VectorCharIter& start, const VectorCharIter& sentinel);
180 * Recursively move child strings to the buffer
182 void RecurseMoveChildStrings(VectorCharIter& start, const VectorCharIter& sentinel);
185 * Recursively copy children
187 static void CopyChildren(const TreeNode* from, TreeNode* to, int& numberNodes, int& numberChars);
190 * Do write to string stream
192 void DoWrite(const TreeNode* value, std::ostream& output, int level, int ident, bool groupChildren) const;
203 * Call operator to add nodes to the list
205 void operator()(TreeNode*& 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);
235 * Recursive search on the tree for the child with the given name
236 * @param childName The name to find
237 * @param tree The tree to search
238 * @return the TreeNode if found, else NULL
240 const TreeNode* FindIt(std::string_view childName, const TreeNode* tree);
243 * Copy string to a buffer
244 * Raises if there is not enough space in the buffer
245 * @param fromString The string
246 * @param iter The start of the buffer
247 * @param sentinel The buffer sentinel
248 * @return The start of the given buffer
250 char* CopyString(const char* fromString, VectorCharIter& iter, const VectorCharIter& sentinel);
252 } // namespace Internal
254 } // namespace Toolkit
258 #endif // DALI_SCRIPT_TREE_NODE_MANIPULATOR_H