File indexing completed on 2024-12-01 03:32:23
0001 /* 0002 SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef _TREENODE_H_ 0008 #define _TREENODE_H_ 0009 0010 #include <QList> 0011 0012 0013 #include "token.h" 0014 #include "value.h" 0015 0016 0017 0018 /** 0019 * @short A node in the pointer based node tree structure. 0020 * 0021 * The TreeNode object is a node in the node tree as created by the Parser. 0022 * It can be executed by the Executer. 0023 * 0024 * The list of pointers to the children of the TreeNode is only created 0025 * when it is needed. 0026 * Each TreeNode has a list of pointers to its children, a pointer to its 0027 * parent, a pointer to one Token, and can also have a pointer to a Value. 0028 * 0029 * @author Cies Breijs 0030 */ 0031 class TreeNode 0032 { 0033 public: 0034 /** 0035 * @short Constructor. 0036 * Initialses the TreeNode. 0037 * @param token pointer to Token that the TreeNode is associated with. 0038 */ 0039 explicit TreeNode(Token* token) { init(nullptr, token); } 0040 0041 /** 0042 * @short Destructor. 0043 * This deletes the Value and the Token associated with this TreeNode. 0044 * The childList auto-deletes all the children, which means that deleting 0045 * the root node deletes the whole tree. 0046 */ 0047 virtual ~TreeNode(); 0048 0049 0050 0051 /** @returns the pointer to the parent TreeNode. @see setParent() */ 0052 TreeNode* parent() { return _parent; } 0053 0054 /** @returns the pointer to associated Token. @see setToken() */ 0055 Token* token() { return _token; } 0056 0057 /** @returns the pointer to associated Value. @see setValue() @see setNullValue() */ 0058 Value* value() { if (_value == nullptr) _value = new Value(); return _value; } 0059 0060 0061 0062 /** Sets the pointer to the parent to @p parent. @see parent() */ 0063 void setParent(TreeNode* parent) { _parent = parent; } 0064 0065 /** Sets the pointer to the associated token to @p token. @see token() and @see TreeNode() */ 0066 void setToken(Token* token) { _token = token; } 0067 0068 /** Sets the pointer to the associated value to @p value. @see setNullValue() @see value() */ 0069 void setValue(Value value) { delete _value; _value = new Value(value); } 0070 0071 /** Sets the pointer to the associated value to zero. @see setValue() @see value() */ 0072 void setNullValue() { delete _value; _value = nullptr; } // appears Empty (see value()) 0073 0074 0075 0076 /** @returns TRUE is the TreeNode has an associated Value. @see value @see setValue */ 0077 bool hasValue() const { return _value != nullptr; } 0078 0079 /** @returns TRUE is the TreeNode has children. @see childCount @see appendChild */ 0080 bool hasChildren() const { if (childList == nullptr) return false; else return !childList->isEmpty(); } 0081 0082 /** @returns the amount of children. @see appendChild @see hasChildren */ 0083 uint childCount() const { if (childList == nullptr) return 0; else return childList->size(); } 0084 0085 /** 0086 * Appends the pointer to the TreeNode @p newChild to the childList and 0087 * sets the child's parent to this. @see childCount @see hasChildren 0088 */ 0089 void appendChild(TreeNode* newChild); 0090 0091 0092 0093 /** @returns the pointer to child number @p i (zero if the child does not exists). This does not change the current child. */ 0094 TreeNode* child(int i); 0095 0096 /** @returns the pointer to the first child (zero if the child does not exists), and set the current child to the first */ 0097 TreeNode* firstChild(); 0098 0099 /** @returns the pointer to the next child (zero if the child does not exists), and set the current child to the next */ 0100 TreeNode* nextChild(); 0101 0102 0103 /** @returns the pointer to the next sibling; the next child of the parent (zero if the next sibling does not exists) */ 0104 TreeNode* nextSibling(); 0105 0106 0107 /** @returns the node tree, starting from 'this' node, as a multi line string */ 0108 QString toString(); 0109 0110 0111 0112 private: 0113 /// Initializes a the TreeNode. Called by the constructor. 0114 void init(TreeNode* parent, Token* token); 0115 0116 /** Calls itself, @ref show(), and @ref showTree() for each of the children */ 0117 virtual void show(QString& str, int indent = 0) const; 0118 0119 /** Prints an indented string describing itself to de debug info */ 0120 void showTree(QString& str, int indent = 0); 0121 0122 int findChildIndex(TreeNode* child); 0123 0124 /// typedef for the ChildList. 0125 typedef QList<TreeNode*> ChildList; 0126 0127 /// The childList, contains pointers to the children of this node. 0128 ChildList *childList; 0129 0130 /// Keeps track of the index of the current child. Zero when no child list is available. 0131 int currentChildIndex; 0132 0133 0134 0135 /// The pointer to the parent of this TreeNode. 0136 TreeNode *_parent; 0137 0138 /// The pointer to the token associated with this TreeNode (cannot be zero). 0139 Token *_token; 0140 0141 /// The pointer to the value associated with this TreeNode (can be zero). 0142 Value *_value; 0143 }; 0144 0145 #endif // _TREENODE_H_