mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-11 22:54:34 +00:00
destructor work, set/get child by ID work
This commit is contained in:
parent
c72d8a1f9b
commit
6d708af9ba
1 changed files with 79 additions and 8 deletions
|
|
@ -20,17 +20,29 @@
|
||||||
// IN THE SOFTWARE.
|
// IN THE SOFTWARE.
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef _TREEOBJECT_H_
|
||||||
|
#define _TREEOBJECT_H_
|
||||||
#include "core/util/tVector.h"
|
#include "core/util/tVector.h"
|
||||||
#include "console/simObject.h"
|
#include "console/simObject.h"
|
||||||
#include "core/util/tDictionary.h"
|
#include "core/util/tDictionary.h"
|
||||||
#include "console/consoleTypes.h"
|
#include "console/consoleTypes.h"
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class TreeNode : public Vector<TreeNode<T>*> {
|
class TreeNode : public Vector<TreeNode<T>*> {
|
||||||
public:
|
public:
|
||||||
T data;
|
T data;
|
||||||
TreeNode<T>* parent;
|
TreeNode<T>* parent;
|
||||||
TreeNode(const T& val = T(), TreeNode<T>* p = NULL) : data(val), parent(p){}
|
TreeNode(const T& val = T(), TreeNode<T>* p = NULL) : data(val), parent(p){}
|
||||||
virtual ~TreeNode() { clear();}
|
virtual ~TreeNode()
|
||||||
|
{
|
||||||
|
deleteChildren();
|
||||||
|
if (parent)
|
||||||
|
{
|
||||||
|
parent->remove(this);
|
||||||
|
parent = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//description logic
|
//description logic
|
||||||
inline bool isRoot() const { return parent == NULL; }
|
inline bool isRoot() const { return parent == NULL; }
|
||||||
inline bool isLeaf() const { return size() == 0; }
|
inline bool isLeaf() const { return size() == 0; }
|
||||||
|
|
@ -45,29 +57,74 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
//children logic
|
//children logic
|
||||||
inline TreeNode<T>* addChild(const T& val) {
|
inline TreeNode<T>* addChild(const T& val)
|
||||||
|
{
|
||||||
TreeNode<T>* child = new TreeNode<T>(val, this);
|
TreeNode<T>* child = new TreeNode<T>(val, this);
|
||||||
push_back(child);
|
push_back(child);
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
inline void addChild(TreeNode<T>* child) {
|
inline void addChild(TreeNode<T>* child)
|
||||||
|
{
|
||||||
if (child) {
|
if (child) {
|
||||||
child->parent = this;
|
child->parent = this;
|
||||||
push_back(child);
|
push_back(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inline void addChildren(const Vector<T>* children) {
|
inline void setChild(U32 i, TreeNode<T>* child)
|
||||||
|
{
|
||||||
|
if (i >= size())
|
||||||
|
{
|
||||||
|
U32 oldSize = size();
|
||||||
|
increment((i + 1) - oldSize);
|
||||||
|
for (U32 j = oldSize; j < size(); j++)
|
||||||
|
(*this)[j] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (child == NULL)
|
||||||
|
{
|
||||||
|
removeChild(i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
(*this)[i] = child;
|
||||||
|
child->parent = this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TreeNode<T>* getChild(U32 i) const
|
||||||
|
{
|
||||||
|
if (!this)
|
||||||
|
return NULL;
|
||||||
|
if (i < size())
|
||||||
|
return (*this)[i];
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void addChildren(const Vector<T>* children)
|
||||||
|
{
|
||||||
if (!children) return;
|
if (!children) return;
|
||||||
for (U32 i = 0; i < children->size(); i++) {
|
for (U32 i = 0; i < children->size(); i++) {
|
||||||
TreeNode<T>* child = new TreeNode<T>((*children)[i], this);
|
TreeNode<T>* child = new TreeNode<T>((*children)[i], this);
|
||||||
push_back(child);
|
push_back(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void removeChild(U32 i)
|
||||||
|
{
|
||||||
|
if (i < size())
|
||||||
|
{
|
||||||
|
TreeNode<T>* child = (*this)[i];
|
||||||
|
if (child)
|
||||||
|
child->parent = NULL;
|
||||||
|
erase(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
void operator =(Vector<T>* other) { clear(); addChildren(other); }
|
void operator =(Vector<T>* other) { clear(); addChildren(other); }
|
||||||
|
|
||||||
inline U32 getNumChildren() const { return size(); }
|
inline U32 getNumChildren() const { return size(); }
|
||||||
inline bool hasChildren() const { return size() > 0; }
|
inline bool hasChildren() const { return size() > 0; }
|
||||||
inline Vector<TreeNode<T>*> getChildren() const {
|
inline Vector<TreeNode<T>*> getChildren() const
|
||||||
|
{
|
||||||
Vector<TreeNode<T>*> children;
|
Vector<TreeNode<T>*> children;
|
||||||
children.reserve(size());
|
children.reserve(size());
|
||||||
for (U32 i = 0; i < size(); i++)
|
for (U32 i = 0; i < size(); i++)
|
||||||
|
|
@ -76,13 +133,25 @@ public:
|
||||||
}
|
}
|
||||||
inline void deleteChildren()
|
inline void deleteChildren()
|
||||||
{
|
{
|
||||||
for (U32 i = 0; i < size(); i++)
|
Vector<TreeNode<T>*> children;
|
||||||
delete (*this)[i];
|
for (U32 i = 0; i < size(); i++) {
|
||||||
|
children.push_back((*this)[i]);
|
||||||
|
}
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
|
for (U32 i = 0; i < children.size(); i++)
|
||||||
|
{
|
||||||
|
if (children[i])
|
||||||
|
{
|
||||||
|
children[i]->parent = NULL;
|
||||||
|
delete children[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//sibling logic
|
//sibling logic
|
||||||
inline Vector<TreeNode<T>*> getSiblings() const {
|
inline Vector<TreeNode<T>*> getSiblings() const
|
||||||
|
{
|
||||||
Vector<TreeNode<T>*> siblings;
|
Vector<TreeNode<T>*> siblings;
|
||||||
if (!parent) return siblings;
|
if (!parent) return siblings;
|
||||||
|
|
||||||
|
|
@ -99,6 +168,7 @@ public:
|
||||||
inline T getData() const { return data; }
|
inline T getData() const { return data; }
|
||||||
inline void setData(const T& val) { data = val; }
|
inline void setData(const T& val) { data = val; }
|
||||||
inline void operator =(const T& val) { data = val; }
|
inline void operator =(const T& val) { data = val; }
|
||||||
|
inline T* getDataPtr() { return &data; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class TreeObject : public SimObject {
|
class TreeObject : public SimObject {
|
||||||
|
|
@ -157,3 +227,4 @@ public:
|
||||||
|
|
||||||
DECLARE_CONOBJECT(TreeObject);
|
DECLARE_CONOBJECT(TreeObject);
|
||||||
};
|
};
|
||||||
|
#endif //_TREEOBJECT_H_
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue