Debian Codegraph @

lib/database/src/so/fields/SoMFNode.c++

raw symbols SCIP
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 | SoMFNode
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/SoDB.h>
55 #include <Inventor/nodes/SoNode.h>
56 #include <Inventor/actions/SoWriteAction.h>
57 #include <Inventor/fields/SoMFNode.h>
58
59 //////////////////////////////////////////////////////////////////////////////
60 //
61 // SoMFNode class - contains pointers to SoNodes. This has to
62 // implement some extra node bookkeeping.
63 //
64 //////////////////////////////////////////////////////////////////////////////
65
66 // Use standard definitions of required methods, but we have to
67 // redefine the rest to keep track of references and auditors.
68
69 SO_MFIELD_REQUIRED_SOURCE(SoMFNode);
70
71 ////////////////////////////////////////////////////////////////////////
72 //
73 // Description:
74 // Constructor (standard stuff)
75 //
76 // Use: public
77
78 SoMFNode::SoMFNode()
79 //
80 ////////////////////////////////////////////////////////////////////////
81 {
82 values = NULL;
83 }
84
85 ////////////////////////////////////////////////////////////////////////
86 //
87 // Description:
88 // Destructor
89 //
90 // Use: public
91
92 SoMFNode::~SoMFNode()
93 //
94 ////////////////////////////////////////////////////////////////////////
95 {
96 deleteAllValues();
97 }
98
99 ////////////////////////////////////////////////////////////////////////
100 //
101 // These all override the definitions in SO_MFIELD_VALUE_SOURCE() to
102 // keep track of references and auditors.
103 //
104 ////////////////////////////////////////////////////////////////////////
105
106 int
107 SoMFNode::find(SoNode *targetValue, SbBool addIfNotFound)
108 {
109 int i, num = getNum();
110
111 for (i = 0; i < num; i++)
112 if (values[i] == targetValue)
113 return i;
114
115 if (addIfNotFound)
116 set1Value(num, targetValue);
117
118 return -1;
119 }
120
121 void
122 SoMFNode::setValues(int start, int num, const SoNode **newValues)
123 {
124 int newNum = start + num, i;
125
126 if (newNum > getNum())
127 makeRoom(newNum);
128
129 for (i = 0; i < num; i++)
130 setVal(start + i, (SoNode *) newValues[i]);
131
132 valueChanged();
133 }
134
135 void
136 SoMFNode::set1Value(int index, SoNode *newValue)
137 {
138 if (index >= getNum())
139 makeRoom(index + 1);
140 setVal(index, newValue);
141 valueChanged();
142 }
143
144 void
145 SoMFNode::setValue(SoNode *newValue)
146 {
147 makeRoom(1);
148 setVal(0, newValue);
149 valueChanged();
150 }
151
152 int
153 SoMFNode::operator ==(const SoMFNode &f) const
154 {
155 int i, num = getNum();
156 const SoNode **myVals, **itsVals;
157
158 if (num != f.getNum())
159 return FALSE;
160
161 myVals = getValues(0);
162 itsVals = f.getValues(0);
163
164 for (i = 0; i < num; i++)
165 if (! (myVals[i] == itsVals[i]))
166 return FALSE;
167
168 return TRUE;
169 }
170
171 void
172 SoMFNode::deleteAllValues()
173 {
174 allocValues(0);
175 }
176
177 void
178 SoMFNode::copyValue(int to, int from)
179 {
180 if (to != from)
181 setVal(to, values[from]);
182 }
183
184 ////////////////////////////////////////////////////////////////////////
185 //
186 // This overrides the definition in SO_MFIELD_ALLOC_SOURCE() to
187 // keep track of references and auditors.
188 //
189 ////////////////////////////////////////////////////////////////////////
190
191 void
192 SoMFNode::allocValues(int newNum)
193 {
194 int i;
195
196 if (values == NULL) {
197 if (newNum > 0) {
198 values = new SoNode * [newNum];
199
200 // Make sure all pointers are initialized to NULL
201 for (i = 0; i < newNum; i++)
202 values[i] = NULL;
203 }
204 }
205 else {
206 SoNode **oldValues = values;
207
208 if (newNum > 0) {
209 values = new SoNode * [newNum];
210 for (i = 0; i < num && i < newNum; i++)
211 values[i] = oldValues[i];
212
213 // Initialize unused pointers to NULL
214 for (i = num; i < newNum; i++)
215 values[i] = NULL;
216 }
217 else
218 values = NULL;
219
220 // Free up any old stuff
221 if (oldValues != NULL) {
222
223 // Remove auditors and references on unused values
224 for (i = newNum; i < num; i++) {
225 if (oldValues[i] != NULL) {
226 oldValues[i]->removeAuditor(this, SoNotRec::FIELD);
227 oldValues[i]->unref();
228 }
229 }
230
231 delete [] oldValues;
232 }
233 }
234
235 num = maxNum = newNum;
236 }
237
238 ////////////////////////////////////////////////////////////////////////
239 //
240 // Description:
241 // Reads one (indexed) value from file. Returns FALSE on error.
242 //
243 // Use: private
244
245 SbBool
246 SoMFNode::read1Value(SoInput *in, int index)
247 //
248 ////////////////////////////////////////////////////////////////////////
249 {
250 SbName name;
251 SoBase *base;
252
253 // See if it's a null pointer
254 if (in->read(name)) {
255 if (name == "NULL") {
256 setVal(index, NULL);
257 return TRUE;
258 }
259 else
260 in->putBack(name.getString());
261 }
262
263 // Read node
264 if (! SoBase::read(in, base, SoNode::getClassTypeId())) {
265 setVal(index, NULL);
266 return FALSE;
267 }
268
269 // Set value (adds a reference to node)
270 setVal(index, (SoNode *) base);
271
272 return TRUE;
273 }
274
275 ////////////////////////////////////////////////////////////////////////
276 //
277 // Description:
278 // Overrides SoField::countWriteRefs() to maintain write-references
279 // in nodes.
280 //
281 // Use: private
282
283 void
284 SoMFNode::countWriteRefs(SoOutput *out) const
285 //
286 ////////////////////////////////////////////////////////////////////////
287 {
288 // Do standard counting for field
289 SoField::countWriteRefs(out);
290
291 // Count nodes
292 for (int i = 0; i < num; i++) {
293 SoNode *node = values[i];
294
295 if (node != NULL)
296 node->writeInstance(out);
297 }
298 }
299
300 ////////////////////////////////////////////////////////////////////////
301 //
302 // Description:
303 // Writes value of field to file.
304 //
305 // Use: private
306
307 void
308 SoMFNode::write1Value(SoOutput *out, int index) const
309 //
310 ////////////////////////////////////////////////////////////////////////
311 {
312 if (values[index] != NULL)
313 values[index]->writeInstance(out);
314 else
315 out->write("NULL");
316 }
317
318 ////////////////////////////////////////////////////////////////////////
319 //
320 // Description:
321 // Changes value in field without doing other notification stuff.
322 // Keeps track of references and auditors.
323 //
324 // Use: private
325
326 void
327 SoMFNode::setVal(int index, SoNode *newValue)
328 //
329 ////////////////////////////////////////////////////////////////////////
330 {
331 SoNode *value = values[index];
332
333 // Play it safe if we are replacing one pointer with the same pointer
334 if (newValue != NULL)
335 newValue->ref();
336
337 // Get rid of old node, if any
338 if (value != NULL) {
339 value->removeAuditor(this, SoNotRec::FIELD);
340 value->unref();
341 }
342
343 value = values[index] = newValue;
344
345 if (value != NULL) {
346 value->ref();
347 value->addAuditor(this, SoNotRec::FIELD);
348 }
349
350 if (newValue != NULL)
351 newValue->unref();
352 }
353
354 ////////////////////////////////////////////////////////////////////////
355 //
356 // Description:
357 // Update a copied field to use the copy of each node if there is
358 // one.
359 //
360 // Use: internal
361
362 void
363 SoMFNode::fixCopy(SbBool copyConnections)
364 //
365 ////////////////////////////////////////////////////////////////////////
366 {
367 for (int i = 0; i < num; i++) {
368 if (values[i] != NULL) {
369 SoNode *nodeCopy = (SoNode *)
370 SoFieldContainer::findCopy(values[i], copyConnections);
371 if (nodeCopy != NULL)
372 setVal(i, nodeCopy);
373 }
374 }
375 }
376
377 ////////////////////////////////////////////////////////////////////////
378 //
379 // Description:
380 // Override this to also check the stored nodes.
381 //
382 // Use: internal, virtual
383
384 SbBool
385 SoMFNode::referencesCopy() const
386 //
387 ////////////////////////////////////////////////////////////////////////
388 {
389 // Do the normal test, and also see if the stored nodes are copies
390 if (SoMField::referencesCopy())
391 return TRUE;
392
393 for (int i = 0; i < num; i++)
394 if (values[i] != NULL &&
395 SoFieldContainer::checkCopy(values[i]) != NULL)
396 return TRUE;
397
398 return FALSE;
399 }