Debian Codegraph @

lib/database/src/so/SoState.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoState
47 |
48 | Author(s) : Paul S. Strauss
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/elements/SoElement.h>
55 #include <Inventor/errors/SoDebugError.h>
56 #include <Inventor/misc/SoState.h>
57
58 ////////////////////////////////////////////////////////////////////////
59 //
60 // Description:
61 // SoState constructor. It is passed a pointer to the action
62 // instance the state is in, and a list of type-ids for all
63 // elements enabled in this instance.
64 //
65 // Use: public
66
67 SoState::SoState(SoAction *_action, const SoTypeList &enabledElements)
68 //
69 ////////////////////////////////////////////////////////////////////////
70 {
71 int i;
72 SoElement *elt;
73
74 action = _action;
75 depth = 0;
76
77 // Find out number of elements
78 numStacks = SoElement::getNumStackIndices();
79
80 // Allocate stack pointers
81 stack = new SoElement * [numStacks];
82
83 // Initialize all stacks to NULL
84 for (i = 0; i < numStacks; i++)
85 stack[i] = NULL;
86
87 // Allocate and initialize one instance of each enabled element.
88 // While doing this, set up threaded stack of elements
89 topElement = NULL;
90 for (i = 0; i < enabledElements.getLength(); i++) {
91 // Skip bad elements
92 if (enabledElements[i].isBad())
93 continue;
94
95 elt = (SoElement *) enabledElements[i].createInstance();
96 elt->setDepth(depth);
97 stack[elt->getStackIndex()] = elt;
98 elt->init(this);
99 elt->setNext(topElement);
100 elt->setNextInStack(NULL);
101 elt->setNextFree(NULL);
102 topElement = elt;
103 }
104
105 // Push state to avoid clobbering initial element instances
106 push();
107
108 // Assume there are no caches open yet
109 setCacheOpen(FALSE);
110 }
111
112 ////////////////////////////////////////////////////////////////////////
113 //
114 // Description:
115 // SoState destructor.
116 //
117 // Use: public
118
119 SoState::~SoState()
120 //
121 ////////////////////////////////////////////////////////////////////////
122 {
123 // Pop state; matches push done in constructor
124 pop();
125
126 #ifdef DEBUG
127 if (depth != 0) {
128 SoDebugError::post("SoState::~SoState",
129 "State destroyed with non-zero (%d) "
130 "depth", depth);
131 }
132 #endif
133
134 // Get rid of all the elements on all the stacks.
135 SoElement *elt, *nextElt;
136 int i;
137 for (i = 0; i < numStacks; i++) {
138 elt = stack[i];
139 while (elt != NULL) {
140 nextElt = elt->getNextFree();
141 delete elt;
142 elt = nextElt;
143 }
144 }
145
146 // Get rid of stack pointer array
147 delete[] stack;
148 }
149
150 ////////////////////////////////////////////////////////////////////////
151 //
152 // Description:
153 // Returns a writable instance of the element on the top of the
154 // stack with the given index.
155 //
156 // Use: public
157
158 SoElement *
159 SoState::getElement(int stackIndex)
160 //
161 ////////////////////////////////////////////////////////////////////////
162 {
163 #ifdef DEBUG
164 // Nasty bug: calling this routine while popping the state
165 // can case very bad things to happen (topElement->next chain will
166 // end up not being sorted by depth). We'll check and make sure
167 // that doesn't happen:
168 if (depth < topElement->getDepth()) {
169 SoDebugError::post("SoState::getElement",
170 "Elements must not be changed while the state is being "
171 "popped (element being changed: %s).",
172 SoElement::getIdFromStackIndex(stackIndex).getName().getString());
173 }
174 #endif
175
176 // Get top of element stack with given index
177 SoElement *elt = stack[stackIndex];
178
179 #ifdef DEBUG
180 if (elt == NULL) {
181 SoDebugError::post("SoState::getElement", "%s is not enabled",
182 SoElement::getIdFromStackIndex(stackIndex).getName().getString());
183 return NULL;
184 }
185 #endif /* DEBUG */
186
187 // If element is not at current depth, we have to push a new
188 // element on the stack
189 if (elt->getDepth() < depth) {
190 SoElement *newElt;
191
192 // Each element stack is a doubly-linked list. The
193 // nextInStack pointer points to the next lowest element, and
194 // the nextFree pointer points to the next hightest element.
195 // The top element's nextFree pointer points to a free element.
196 //
197 // With this scheme we only have to allocate elements for the
198 // stack once; pushing and popping during subsequent
199 // traversals just move the topElement pointer up and down the
200 // list.
201 if (elt->getNextFree()) {
202 newElt = elt->getNextFree();
203 } else {
204 newElt = (SoElement *)(elt->getTypeId().createInstance());
205 elt->setNextFree(newElt);
206 newElt->setNextInStack(elt);
207 newElt->setNextFree(NULL);
208 }
209
210 newElt->setDepth(depth);
211
212 // Add it to the all-element stack
213 newElt->setNext(topElement);
214
215 topElement = stack[stackIndex] = newElt;
216
217 // Call push on new element in case it has side effects
218 newElt->push(this);
219
220 // Return new element
221 elt = newElt;
222 }
223
224 return elt;
225 }
226
227
228 ////////////////////////////////////////////////////////////////////////
229 //
230 // Description:
231 // Pops the state, restoring the state to just before the last push().
232 //
233 // Use: public
234
235 void
236 SoState::pop()
237 //
238 ////////////////////////////////////////////////////////////////////////
239 {
240 SoElement *poppedElt, *nextInStack;
241
242 --depth;
243
244 // Do the popping in two passes. The first calls the pop() method
245 // for all popped elements. The second pass actually removes the
246 // elements from the stacks and frees them up. We do this in two
247 // passes because calling pop() may add elements to the current
248 // SoCacheElement, so we want to make sure this is done before
249 // that element is popped.
250
251 // Call pop() for all elements that were at previous depth
252 for (poppedElt = topElement;
253 poppedElt != NULL && poppedElt->getDepth() > depth;
254 poppedElt = poppedElt->getNext()) {
255
256 // Find next element in same stack as popped element. This
257 // element will become the new top of that stack.
258 nextInStack = poppedElt->getNextInStack();
259
260 // Give new top element in stack a chance to update things.
261 // Pass old element instance just in case.
262 poppedElt->getNextInStack()->pop(this, poppedElt);
263
264 }
265
266 // Actually pop all such elements
267 while (topElement != NULL && topElement->getDepth() > depth) {
268 poppedElt = topElement;
269
270 // Remove from main stack
271 topElement = topElement->getNext();
272
273 // Remove from element stack
274 stack[poppedElt->getStackIndex()] = poppedElt->getNextInStack();
275 }
276 }
277
278 ////////////////////////////////////////////////////////////////////////
279 //
280 // Description:
281 // Prints state to file for debugging.
282 //
283 // Use: public
284
285 void
286 SoState::print(FILE *fp)
287 //
288 ////////////////////////////////////////////////////////////////////////
289 {
290 const SoElement *elt;
291 int i;
292
293 fprintf(fp, "_________________________________________________________\n");
294 fprintf(fp, "SoState\n");
295 fprintf(fp, "_________________________________________________________\n");
296
297 for (i = 0; i < numStacks; i++) {
298
299 // Print only enabled element stacks
300 if (stack[i] != NULL) {
301
302 fprintf(fp, " stack[%02d]:\n", i);
303
304 for (elt = stack[i]; elt != NULL; elt = elt->getNextInStack()) {
305 fprintf(fp, " ");
306 elt->print(fp);
307 }
308 }
309 }
310
311 fprintf(fp, "_________________________________________________________\n");
312 }