ScummVM API documentation
dgNode.h
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __dgNode__
23 #define __dgNode__
24 
25 #include "dgStdafx.h"
26 #include "dgCRC.h"
27 #include "dgRef.h"
28 
29 #include "common/util.h"
30 
31 
32 class dgFile;
33 //enum dgSaveType;
34 
35 
36 class dgBaseNode: public dgRef {
37 public:
38  dgBaseNode *GetChild() const;
39  dgBaseNode *GetParent() const;
40  dgBaseNode *GetSibling() const;
41 
42  void Detach();
43  void Attach(dgBaseNode *parent, bool addFirst = false);
44 
45  const dgBaseNode *GetRoot() const;
46  const dgBaseNode *GetFirst() const;
47  const dgBaseNode *GetLast() const;
48  const dgBaseNode *GetNext() const;
49  const dgBaseNode *GetPrev() const;
50  dgBaseNode *GetRoot();
51  dgBaseNode *GetFirst();
52  dgBaseNode *GetLast();
53  dgBaseNode *GetNext();
54  dgBaseNode *GetPrev();
55 
56  const dgBaseNode *Find(dgUnsigned32 nameCRC) const;
57  dgBaseNode *Find(dgUnsigned32 nameCRC);
58  const dgBaseNode *Find(const char *name) const;
59  dgBaseNode *Find(const char *name);
60  void DebugPrint(const char *fileName);
61  bool SanityCheck();
62 
63 
64 protected:
65  dgBaseNode();
66  dgBaseNode(const char *name);
67  dgBaseNode(const dgBaseNode &clone);
68  ~dgBaseNode();
69 
70 // virtual void Save (dgFile &file, dgSaveType saveType, void* const context) const;
71  virtual void CloneFixUp(const dgBaseNode &clone);
72  virtual void PrintHierarchy(dgFile &file, char *indentation) const;
73 
74 private:
75  inline void Clear();
76 
77  dgBaseNode *parent;
78  dgBaseNode *child;
79  dgBaseNode *sibling;
80 
81 };
82 
83 template<class T>
84 class dgNode: public dgBaseNode {
85 public:
86  dgNode();
87  dgNode(const char *name);
88  void Attach(T *parentNode, bool addFirst = false);
89  void Detach();
90  T *GetChild() const;
91  T *GetSibling() const;
92  T *GetParent() const;
93  T *GetRoot() const;
94  T *GetRoot();
95  T *GetFirst() const;
96  T *GetLast() const;
97  T *GetNext() const;
98  T *GetPrev() const;
99  T *Find(dgUnsigned32 nameCRC) const;
100  T *Find(const char *name) const;
101 
102 protected:
103  dgNode(const T &clone);
104  virtual ~dgNode();
105  dgRef *CreateClone() const;
106 
107 };
108 
109 
110 
111 
112 
113 inline dgBaseNode::dgBaseNode()
114  : dgRef() {
115  Clear();
116 }
117 
118 inline dgBaseNode::dgBaseNode(const char *name)
119  : dgRef(name) {
120  Clear();
121 }
122 
123 
124 inline void dgBaseNode::Clear() {
125  child = NULL;
126  parent = NULL;
127  sibling = NULL;
128 }
129 
130 
131 inline dgBaseNode *dgBaseNode::GetChild() const {
132  return child;
133 }
134 
135 inline dgBaseNode *dgBaseNode::GetSibling() const {
136  return sibling;
137 }
138 
139 inline dgBaseNode *dgBaseNode::GetParent() const {
140  return parent;
141 }
142 
143 
144 inline const dgBaseNode *dgBaseNode::Find(const char *name) const {
145  return Find(dgCRC(name));
146 }
147 
148 inline dgBaseNode *dgBaseNode::Find(const char *name) {
149  return Find(dgCRC(name));
150 }
151 
152 
153 
154 template<class T>
156  : dgBaseNode() {
157 }
158 
159 template<class T>
160 dgNode<T>::dgNode(const T &clone)
161  : dgBaseNode(clone) {
162 }
163 
164 template<class T>
165 dgNode<T>::dgNode(const char *name)
166  : dgBaseNode(name) {
167 }
168 
169 template<class T>
171 }
172 
173 
174 template<class T>
175 dgRef *dgNode<T>::CreateClone() const {
176  return new T(*const_cast<T *>((const T *)this));
177 }
178 
179 template<class T>
180 void dgNode<T>::Attach(T *parentNode, bool addFirst) {
181  dgBaseNode::Attach(parentNode, addFirst);
182 }
183 
184 template<class T>
185 void dgNode<T>::Detach() {
186  dgBaseNode::Detach();
187 }
188 
189 template<class T>
190 T *dgNode<T>::GetChild() const {
191  return (T *) dgBaseNode::GetChild();
192 }
193 
194 template<class T>
195 T *dgNode<T>::GetSibling() const {
196  return (T *) dgBaseNode::GetSibling();
197 }
198 
199 template<class T>
200 T *dgNode<T>::GetParent() const {
201  return (T *) dgBaseNode::GetParent();
202 }
203 
204 
205 template<class T>
206 T *dgNode<T>::GetRoot() const {
207  return (T *) dgBaseNode::GetRoot();
208 }
209 
210 
211 template<class T>
212 T *dgNode<T>::GetFirst() const {
213  return (T *) dgBaseNode::GetFirst();
214 }
215 
216 template<class T>
217 T *dgNode<T>::GetLast() const {
218  return (T *) dgBaseNode::GetLast();
219 }
220 
221 
222 template<class T>
223 T *dgNode<T>::GetNext() const {
224  return (T *) dgBaseNode::GetNext();
225 }
226 
227 template<class T>
228 T *dgNode<T>::GetPrev() const {
229  return (T *) dgBaseNode::GetPrev();
230 }
231 
232 
233 template<class T>
234 T *dgNode<T>::Find(dgUnsigned32 nameCRC) const {
235  return (T *) dgBaseNode::Find(nameCRC);
236 }
237 
238 template<class T>
239 T *dgNode<T>::Find(const char *name) const {
240  return (T *) dgBaseNode::Find(name);
241 }
242 
243 
244 
245 
246 #endif
Definition: dgNode.h:84
Definition: dgNode.h:36
Definition: dgRef.h:48