Debian Codegraph @

lib/database/src/so/engines/SoOutputData.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 | SoEngineOutputData
47 |
48 | Author(s) : Paul S. Strauss, Nick Thompson, Gavin Bell
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/SbString.h>
55 #include <Inventor/SoInput.h>
56 #include <Inventor/SoOutput.h>
57 #include <Inventor/engines/SoOutputData.h>
58 #include <Inventor/engines/SoEngine.h>
59 #include <Inventor/errors/SoDebugError.h>
60 #include <cstddef>
61
62 // Syntax for reading/writing type information to files
63 #define OPEN_BRACE_CHAR '['
64 #define CLOSE_BRACE_CHAR ']'
65 #define VALUE_SEPARATOR_CHAR ','
66
67 //////////////////////////////////////////////////////////////////////////////
68 //
69 // This structure holds the name and offset of a output in an
70 // SoEngineOutputData. It is internal to Inventor.
71 //
72 //////////////////////////////////////////////////////////////////////////////
73
74 struct SoOutputEntry {
75 SbName name; // Name of output
76 std::ptrdiff_t offset; // Offset of output within object
77 SoType type; // Type of output
78 };
79
80 ////////////////////////////////////////////////////////////////////////
81 //
82 // Description:
83 // like a copy constructor, but takes a pointer
84 //
85 SoEngineOutputData::SoEngineOutputData(const SoEngineOutputData *from)
86 //
87 ////////////////////////////////////////////////////////////////////////
88 {
89 int i;
90 struct SoOutputEntry *fromOutput, *toOutput;
91
92 if (from == NULL)
93 return;
94
95 for (i = 0; i < from->outputs.getLength(); i++) {
96
97 fromOutput = (struct SoOutputEntry *) from->outputs[i];
98
99 toOutput = new struct SoOutputEntry;
100 *toOutput = *fromOutput;
101
102 outputs.append((void *) toOutput);
103 }
104 }
105
106 ////////////////////////////////////////////////////////////////////////
107 //
108 // Description:
109 // Destructor. Frees up entries.
110 //
111 SoEngineOutputData::~SoEngineOutputData()
112 //
113 ////////////////////////////////////////////////////////////////////////
114 {
115 for (int i = 0; i < outputs.getLength(); i++) {
116 delete (struct SoOutputEntry *)outputs[i];
117 }
118 }
119
120 ////////////////////////////////////////////////////////////////////////
121 //
122 // Description:
123 // Adds an output to current outputData. Called inside SoEngine
124 // subclass constructors, from SO_ENGINE_ADD_OUTPUT macro.
125 //
126 // Use: internal
127
128 void
129 SoEngineOutputData::addOutput(
130 const SoEngine *defEngine, // pointer to container
131 const char *outputName, // Name of output
132 const SoEngineOutput *output, // Pointer to output in container
133 SoType type) // Type of output
134 //
135 ////////////////////////////////////////////////////////////////////////
136 {
137 struct SoOutputEntry *newOutput = new struct SoOutputEntry;
138
139 newOutput->name = outputName;
140 newOutput->offset = (const char *) output - (const char *) defEngine;
141 newOutput->type = type;
142
143 outputs.append((void *) newOutput);
144 }
145
146 ////////////////////////////////////////////////////////////////////////
147 //
148 // Description:
149 // Returns name of output with given index in given object.
150 //
151 // Use: internal
152
153 const SbName &
154 SoEngineOutputData::getOutputName(int index) const
155 //
156 ////////////////////////////////////////////////////////////////////////
157 {
158 return ((SoOutputEntry *) outputs[index])->name;
159 }
160
161 ////////////////////////////////////////////////////////////////////////
162 //
163 // Description:
164 // Returns pointer to output with given index within given node instance.
165 //
166 // Use: internal
167
168 SoEngineOutput *
169 SoEngineOutputData::getOutput(const SoEngine *func, int index) const
170 //
171 ////////////////////////////////////////////////////////////////////////
172 {
173 // This generates a CC warning; there's not much we can do about it...
174 return (SoEngineOutput *) ((char *) func +
175 ((SoOutputEntry *) outputs[index])->offset);
176 }
177
178 ////////////////////////////////////////////////////////////////////////
179 //
180 // Description:
181 // Returns index to output, given output and engine
182 //
183 // Use: internal
184
185 int
186 SoEngineOutputData::getIndex(const SoEngine *func,
187 const SoEngineOutput *output) const
188 //
189 ////////////////////////////////////////////////////////////////////////
190 {
191 int offset = (const char *) output - (const char *) func;
192
193 // Loop through the list looking for the correct offset:
194 // (we'll assume this won't be very slow, since the list will
195 // typically be very short):
196 for (int i = 0; i < outputs.getLength(); i++) {
197 SoOutputEntry *entry = (SoOutputEntry *)outputs[i];
198 if (entry->offset == offset) return i;
199 }
200
201 // This should never happen.
202 SoDebugError::post("(internal) SoEngineOutputData::getIndex",
203 "Did not find engineOutput");
204 return 0;
205 }
206
207 ////////////////////////////////////////////////////////////////////////
208 //
209 // Description:
210 // Returns type of the output with given index
211 //
212 // Use: internal
213
214 const SoType &
215 SoEngineOutputData::getType(int index) const
216 //
217 ////////////////////////////////////////////////////////////////////////
218 {
219 #ifdef DEBUG
220 if (index >= outputs.getLength())
221 SoDebugError::post("SoEngineOutputData::getType",
222 "Trying to get type of output %d, but engine has "
223 "only %d outputs", index, outputs.getLength());
224 #endif /* DEBUG */
225 return ((SoOutputEntry *)(outputs[index]))->type;
226 }
227
228 ////////////////////////////////////////////////////////////////////////
229 //
230 // Description:
231 // Reads output type info
232 //
233 // Use: internal
234
235 SbBool
236 SoEngineOutputData::readDescriptions(SoInput *in, SoEngine *object) const
237
238 //
239 ////////////////////////////////////////////////////////////////////////
240 {
241 SbBool gotChar;
242 SbName fieldType, fieldName;
243 char c;
244
245 // TRUE if reading an Inventor file pre-Inventor 2.1:
246 SbBool oldFileFormat = (in->getIVVersion() < 2.1f);
247
248 // TRUE if reading binary file format:
249 SbBool isBinary = in->isBinary();
250
251 unsigned short numDescriptions = 1<<15; // Any huge number will work
252 if (isBinary) {
253 if (oldFileFormat) {
254 // Read and ignore "outputs" string:
255 SbString junk;
256 if (!in->read(junk) || junk != "outputs") return FALSE;
257
258 int num; // NumDescriptions written as integer in old
259 // file format
260 if (!in->read(num)) return FALSE;
261 numDescriptions = (short) num;
262 } else {
263 if (!in->read(numDescriptions))
264 return FALSE;
265 }
266 }
267
268 SbBool hadOutputsDefined = outputs.getLength() > 0;
269
270 if (!isBinary) {
271 if (! ((gotChar = in->read(c)) || c != OPEN_BRACE_CHAR))
272 return FALSE;
273 }
274
275 for (int i = 0; i < numDescriptions; i++) {
276
277 // Check for closing brace:
278 if (!isBinary) {
279 // Check for closing brace:
280 if (in->read(c) && c == CLOSE_BRACE_CHAR)
281 return TRUE;
282 else in->putBack(c);
283 }
284
285 SbName type, outputName;
286 if (!in->read(type, TRUE)) return FALSE;
287 if (!in->read(outputName, TRUE)) return FALSE;
288
289 SoType outputType = SoType::fromName(type);
290
291 if (!hadOutputsDefined) {
292 // Only create outputs if none defined yet.
293
294 if (outputType.isBad())
295 return FALSE;
296
297 // Create and initialize an instance of the output.
298 // Add it to the field data.
299 SoEngineOutput *output = new SoEngineOutput;
300 output->setContainer(object);
301
302 // Cast const away to add info:
303 SoEngineOutputData *This = (SoEngineOutputData *)this;
304 This->addOutput(object, outputName.getString(),
305 output, outputType);
306 }
307 #ifdef DEBUG
308 else {
309 // Check to make sure specification matches reality:
310 SoEngineOutput *o = object->getOutput(outputName);
311 if (o == NULL) {
312 SoDebugError::post("SoEngineOutputData::readDescriptions",
313 "%s does not have a field named %s",
314 object->getTypeId().getName().getString(),
315 outputName.getString());
316 }
317 else if (!outputType.isDerivedFrom(o->getConnectionType())) {
318 SoDebugError::post("SoFieldData::readFieldDescriptions",
319 "%s.%s is type %s, NOT type %s",
320 object->getTypeId().getName().getString(),
321 outputName.getString(),
322 o->getConnectionType().getName().getString(),
323 type.getString());
324 }
325 }
326 #endif
327 if (!isBinary) {
328 // Better get a ',' or a ']' at this point:
329 if (! in->read(c))
330 return FALSE;
331 if (c != VALUE_SEPARATOR_CHAR) {
332 if (c == CLOSE_BRACE_CHAR)
333 return TRUE;
334 else return FALSE;
335 }
336 // Got a ',', continue reading
337 }
338 }
339
340 return TRUE;
341 }
342
343 ////////////////////////////////////////////////////////////////////////
344 //
345 // Description:
346 // Reads output type info
347 //
348 // Use: internal
349
350 void
351 SoEngineOutputData::writeDescriptions(SoOutput *out, SoEngine *object) const
352
353 //
354 ////////////////////////////////////////////////////////////////////////
355 {
356 SbBool isBinary = out->isBinary();
357
358 if (!isBinary) {
359 out->indent();
360 out->write("outputs");
361 out->write(' ');
362 out->write(OPEN_BRACE_CHAR);
363 out->write(' ');
364 out->incrementIndent(2);
365 } else {
366 unsigned short numDescriptions = getNumOutputs();
367 out->write(numDescriptions);
368 }
369
370 // For each output, write out the output type name and output name.
371 int i = 0;
372 int numWritten = 0;
373 for (i = 0; i < getNumOutputs(); i++) {
374
375 SoEngineOutput *output = getOutput(object, i);
376
377 SoType type = output->getConnectionType();
378
379 out->write(type.getName().getString());
380 if (! isBinary)
381 out->write(' ');
382 out->write(getOutputName(i).getString());
383
384 // Format nice and pretty for ascii
385 if (! isBinary) {
386 if (i != getNumOutputs() - 1) {
387 out->write(VALUE_SEPARATOR_CHAR);
388 ++numWritten;
389 if ((numWritten%4) == 0) { // 4 pairs per line
390 out->write('\n');
391 out->indent();
392 } else out->write(' ');
393 }
394 }
395 }
396
397 // Write out the closing brace of the field type information.
398 if (! out->isBinary()) {
399 out->write(' ');
400 out->write(CLOSE_BRACE_CHAR);
401 out->write('\n');
402 out->decrementIndent(2);
403 }
404 }
405