doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Hierarchy.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __HIERARCHY_H__
30 #define __HIERARCHY_H__
31 
32 /*
33 ==============================================================================
34 
35  idHierarchy
36 
37 ==============================================================================
38 */
39 
40 template< class type >
41 class idHierarchy {
42 public:
43 
44  idHierarchy();
45  ~idHierarchy();
46 
47  void SetOwner( type *object );
48  type * Owner( void ) const;
49  void ParentTo( idHierarchy &node );
50  void MakeSiblingAfter( idHierarchy &node );
51  bool ParentedBy( const idHierarchy &node ) const;
52  void RemoveFromParent( void );
53  void RemoveFromHierarchy( void );
54 
55  type * GetParent( void ) const; // parent of this node
56  type * GetChild( void ) const; // first child of this node
57  type * GetSibling( void ) const; // next node with the same parent
58  type * GetPriorSibling( void ) const; // previous node with the same parent
59  type * GetNext( void ) const; // goes through all nodes of the hierarchy
60  type * GetNextLeaf( void ) const; // goes through all leaf nodes of the hierarchy
61 
62 private:
67 
68  idHierarchy<type> *GetPriorSiblingNode( void ) const; // previous node with the same parent
69 };
70 
71 /*
72 ================
73 idHierarchy<type>::idHierarchy
74 ================
75 */
76 template< class type >
78  owner = NULL;
79  parent = NULL;
80  sibling = NULL;
81  child = NULL;
82 }
83 
84 /*
85 ================
86 idHierarchy<type>::~idHierarchy
87 ================
88 */
89 template< class type >
91  RemoveFromHierarchy();
92 }
93 
94 /*
95 ================
96 idHierarchy<type>::Owner
97 
98 Gets the object that is associated with this node.
99 ================
100 */
101 template< class type >
103  return owner;
104 }
105 
106 /*
107 ================
108 idHierarchy<type>::SetOwner
109 
110 Sets the object that this node is associated with.
111 ================
112 */
113 template< class type >
115  owner = object;
116 }
117 
118 /*
119 ================
120 idHierarchy<type>::ParentedBy
121 ================
122 */
123 template< class type >
124 bool idHierarchy<type>::ParentedBy( const idHierarchy &node ) const {
125  if ( parent == &node ) {
126  return true;
127  } else if ( parent ) {
128  return parent->ParentedBy( node );
129  }
130  return false;
131 }
132 
133 /*
134 ================
135 idHierarchy<type>::ParentTo
136 
137 Makes the given node the parent.
138 ================
139 */
140 template< class type >
142  RemoveFromParent();
143 
144  parent = &node;
145  sibling = node.child;
146  node.child = this;
147 }
148 
149 /*
150 ================
151 idHierarchy<type>::MakeSiblingAfter
152 
153 Makes the given node a sibling after the passed in node.
154 ================
155 */
156 template< class type >
158  RemoveFromParent();
159  parent = node.parent;
160  sibling = node.sibling;
161  node.sibling = this;
162 }
163 
164 /*
165 ================
166 idHierarchy<type>::RemoveFromParent
167 ================
168 */
169 template< class type >
171  idHierarchy<type> *prev;
172 
173  if ( parent ) {
174  prev = GetPriorSiblingNode();
175  if ( prev ) {
176  prev->sibling = sibling;
177  } else {
178  parent->child = sibling;
179  }
180  }
181 
182  parent = NULL;
183  sibling = NULL;
184 }
185 
186 /*
187 ================
188 idHierarchy<type>::RemoveFromHierarchy
189 
190 Removes the node from the hierarchy and adds it's children to the parent.
191 ================
192 */
193 template< class type >
195  idHierarchy<type> *parentNode;
196  idHierarchy<type> *node;
197 
198  parentNode = parent;
199  RemoveFromParent();
200 
201  if ( parentNode ) {
202  while( child ) {
203  node = child;
204  node->RemoveFromParent();
205  node->ParentTo( *parentNode );
206  }
207  } else {
208  while( child ) {
209  child->RemoveFromParent();
210  }
211  }
212 }
213 
214 /*
215 ================
216 idHierarchy<type>::GetParent
217 ================
218 */
219 template< class type >
221  if ( parent ) {
222  return parent->owner;
223  }
224  return NULL;
225 }
226 
227 /*
228 ================
229 idHierarchy<type>::GetChild
230 ================
231 */
232 template< class type >
234  if ( child ) {
235  return child->owner;
236  }
237  return NULL;
238 }
239 
240 /*
241 ================
242 idHierarchy<type>::GetSibling
243 ================
244 */
245 template< class type >
247  if ( sibling ) {
248  return sibling->owner;
249  }
250  return NULL;
251 }
252 
253 /*
254 ================
255 idHierarchy<type>::GetPriorSiblingNode
256 
257 Returns NULL if no parent, or if it is the first child.
258 ================
259 */
260 template< class type >
262  if ( !parent || ( parent->child == this ) ) {
263  return NULL;
264  }
265 
266  idHierarchy<type> *prev;
267  idHierarchy<type> *node;
268 
269  node = parent->child;
270  prev = NULL;
271  while( ( node != this ) && ( node != NULL ) ) {
272  prev = node;
273  node = node->sibling;
274  }
275 
276  if ( node != this ) {
277  idLib::Error( "idHierarchy::GetPriorSibling: could not find node in parent's list of children" );
278  }
279 
280  return prev;
281 }
282 
283 /*
284 ================
285 idHierarchy<type>::GetPriorSibling
286 
287 Returns NULL if no parent, or if it is the first child.
288 ================
289 */
290 template< class type >
292  idHierarchy<type> *prior;
293 
294  prior = GetPriorSiblingNode();
295  if ( prior ) {
296  return prior->owner;
297  }
298 
299  return NULL;
300 }
301 
302 /*
303 ================
304 idHierarchy<type>::GetNext
305 
306 Goes through all nodes of the hierarchy.
307 ================
308 */
309 template< class type >
311  const idHierarchy<type> *node;
312 
313  if ( child ) {
314  return child->owner;
315  } else {
316  node = this;
317  while( node && node->sibling == NULL ) {
318  node = node->parent;
319  }
320  if ( node ) {
321  return node->sibling->owner;
322  } else {
323  return NULL;
324  }
325  }
326 }
327 
328 /*
329 ================
330 idHierarchy<type>::GetNextLeaf
331 
332 Goes through all leaf nodes of the hierarchy.
333 ================
334 */
335 template< class type >
337  const idHierarchy<type> *node;
338 
339  if ( child ) {
340  node = child;
341  while ( node->child ) {
342  node = node->child;
343  }
344  return node->owner;
345  } else {
346  node = this;
347  while( node && node->sibling == NULL ) {
348  node = node->parent;
349  }
350  if ( node ) {
351  node = node->sibling;
352  while ( node->child ) {
353  node = node->child;
354  }
355  return node->owner;
356  } else {
357  return NULL;
358  }
359  }
360 }
361 
362 #endif /* !__HIERARCHY_H__ */
type * GetPriorSibling(void) const
Definition: Hierarchy.h:291
void MakeSiblingAfter(idHierarchy &node)
Definition: Hierarchy.h:157
type * owner
Definition: Hierarchy.h:66
type * GetNextLeaf(void) const
Definition: Hierarchy.h:336
void SetOwner(type *object)
Definition: Hierarchy.h:114
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
void ParentTo(idHierarchy &node)
Definition: Hierarchy.h:141
bool ParentedBy(const idHierarchy &node) const
Definition: Hierarchy.h:124
idHierarchy< type > * GetPriorSiblingNode(void) const
Definition: Hierarchy.h:261
void RemoveFromHierarchy(void)
Definition: Hierarchy.h:194
~idHierarchy()
Definition: Hierarchy.h:90
#define NULL
Definition: Lib.h:88
idHierarchy * sibling
Definition: Hierarchy.h:64
type * GetNext(void) const
Definition: Hierarchy.h:310
idHierarchy * parent
Definition: Hierarchy.h:63
type * GetChild(void) const
Definition: Hierarchy.h:233
type * GetSibling(void) const
Definition: Hierarchy.h:246
type * GetParent(void) const
Definition: Hierarchy.h:220
static void Error(const char *fmt,...)
Definition: Lib.cpp:230
type * Owner(void) const
Definition: Hierarchy.h:102
idHierarchy * child
Definition: Hierarchy.h:65
void RemoveFromParent(void)
Definition: Hierarchy.h:170