1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*!
@file
@author Pavel Turin
@date 08/2009
*/
#ifndef GENERIC_NODE_H_
#define GENERIC_NODE_H_
namespace MyGUI
{
template<class NODE, class OWNER>
class GenericNode
{
public:
typedef std::vector<NODE*> VectorGenericNodePtr;
GenericNode();
GenericNode(OWNER* pOwner);
GenericNode(const UString& strText, NODE* pParent = nullptr);
virtual ~GenericNode();
void add(NODE* pNode);
void remove(NODE* pNode, bool bDelete = true);
void removeAll(bool bDelete = true);
bool hasAncestor(const NODE* pNode) const;
bool hasDescendant(const NODE* pNode) const;
bool hasChildren() const;
const VectorGenericNodePtr& getChildren() const;
VectorGenericNodePtr& getChildren();
NODE* getParent() const;
const UString& getText() const;
void setText(const UString& strText);
OWNER* getOwner() const;
void setOwner(OWNER* pOwner);
void invalidate();
protected:
NODE* mpParent;
VectorGenericNodePtr mChildren;
UString mstrText;
OWNER* mpOwner;
};
template<class NODE, class OWNER>
GenericNode<NODE, OWNER>::GenericNode(OWNER* pOwner) :
mpParent(nullptr),
mstrText("[ROOT]"),
mpOwner(pOwner)
{
MYGUI_DEBUG_ASSERT(mpOwner, "GenericNode<NODE, OWNER>::GenericNode pOwner is nullptr");
}
template<class NODE, class OWNER>
GenericNode<NODE, OWNER>::GenericNode(const UString& strText, NODE* pParent) :
mpParent(pParent),
mstrText(strText)
{
if (pParent)
{
mpOwner = pParent->mpOwner;
pParent->mChildren.push_back(static_cast<NODE*>(this));
invalidate();
}
else
mpOwner = nullptr;
}
template<class NODE, class OWNER>
GenericNode<NODE, OWNER>::~GenericNode()
{
if (mpParent)
mpParent->remove(static_cast<NODE*>(this), false);
while (!mChildren.empty())
{
NODE* pChild = mChildren.back();
mChildren.pop_back();
pChild->mpParent = nullptr;
delete pChild;
}
invalidate();
}
template<class NODE, class OWNER>
void GenericNode<NODE, OWNER>::setText(const UString& strText)
{
mstrText = strText;
invalidate();
}
template<class NODE, class OWNER>
void GenericNode<NODE, OWNER>::setOwner(OWNER* pOwner)
{
mpOwner = pOwner;
for (typename VectorGenericNodePtr::iterator Iterator = mChildren.begin(); Iterator != mChildren.end(); ++Iterator)
(*Iterator)->setOwner(pOwner);
}
template<class NODE, class OWNER>
bool GenericNode<NODE, OWNER>::hasAncestor(const NODE* pNode) const
{
return (mpParent == pNode || (mpParent && mpParent->hasAncestor(pNode)));
}
template<class NODE, class OWNER>
bool GenericNode<NODE, OWNER>::hasDescendant(const NODE* pNode) const
{
for (typename VectorGenericNodePtr::const_iterator Iterator = mChildren.begin(); Iterator != mChildren.end(); ++Iterator)
{
if (*Iterator == pNode || (*Iterator)->hasDescendant(pNode))
return true;
}
return false;
}
template<class NODE, class OWNER>
void GenericNode<NODE, OWNER>::add(NODE* pNode)
{
MYGUI_DEBUG_ASSERT(pNode, "GenericNode<NODE, OWNER>::add pNode is nullptr");
if (pNode->mpParent)
pNode->mpParent->remove(pNode, false);
pNode->mpParent = static_cast<NODE*>(this);
pNode->setOwner(mpOwner);
mChildren.push_back(pNode);
invalidate();
}
template<class NODE, class OWNER>
void GenericNode<NODE, OWNER>::remove(NODE* pNode, bool bDelete)
{
MYGUI_DEBUG_ASSERT(pNode, "GenericNode<NODE, OWNER>::remove pNode is nullptr");
for (typename VectorGenericNodePtr::iterator Iterator = mChildren.begin(); Iterator != mChildren.end(); ++Iterator)
{
if (*Iterator != pNode)
continue;
mChildren.erase(Iterator);
pNode->mpParent = nullptr;
if (bDelete)
delete pNode;
invalidate();
return;
}
MYGUI_DEBUG_ASSERT(pNode, "GenericNode<NODE, OWNER>::remove pNode not child");
}
template<class NODE, class OWNER>
void GenericNode<NODE, OWNER>::removeAll(bool bDelete)
{
while (!mChildren.empty())
{
NODE* pChild = mChildren.back();
mChildren.pop_back();
pChild->mpParent = nullptr;
if (bDelete)
delete pChild;
}
invalidate();
}
template<class NODE, class OWNER>
void GenericNode<NODE, OWNER>::invalidate()
{
if (mpOwner)
mpOwner->invalidate();
}
template<class NODE, class OWNER>
inline GenericNode<NODE, OWNER>::GenericNode()
{
mpParent = nullptr;
mpOwner = nullptr;
}
template<class NODE, class OWNER>
inline const std::vector<NODE*>& GenericNode<NODE, OWNER>::getChildren() const
{
return mChildren;
}
template<class NODE, class OWNER>
inline std::vector<NODE*>& GenericNode<NODE, OWNER>::getChildren()
{
return mChildren;
}
template<class NODE, class OWNER>
inline bool GenericNode<NODE, OWNER>::hasChildren() const
{
return !mChildren.empty();
}
template<class NODE, class OWNER>
inline const UString& GenericNode<NODE, OWNER>::getText() const
{
return mstrText;
}
template<class NODE, class OWNER>
inline OWNER* GenericNode<NODE, OWNER>::getOwner() const
{
return mpOwner;
}
template<class NODE, class OWNER>
NODE* GenericNode<NODE, OWNER>::getParent() const
{
return mpParent;
}
}
#endif // GENERIC_NODE_H_
|