Debian Codegraph @

lib/database/src/so/engines/SoEngine.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,92 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 | SoEngine, SoEngineOutput
47 |
48 | Author(s) : 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/SoDB.h>
55 #include <Inventor/SoOutput.h>
56 #include <Inventor/SoInput.h>
57 #include <Inventor/engines/SoEngine.h>
58 #include <Inventor/engines/SoFieldConverter.h>
59 #include <Inventor/engines/SoOutputData.h>
60 #include <Inventor/errors/SoDebugError.h>
61 #include <Inventor/fields/SoField.h>
62 #include <Inventor/fields/SoFieldData.h>
63 #include <Inventor/misc/SoNotification.h>
64
65 SoType SoEngine::classTypeId; // Type identifier
66
67 // Syntax for reading/writing type information to files
68 #define OPEN_BRACE_CHAR '['
69 #define CLOSE_BRACE_CHAR ']'
70 #define VALUE_SEPARATOR_CHAR ','
71
72 //////////////////////////////////////////////////////////////////////
73 //
74 // SoEngine class
75 //
76 //////////////////////////////////////////////////////////////////////
77
78 ////////////////////////////////////////////////////////////////////////
79 //
80 // Description:
81 // Constructor. Engines are assumed to need evaluation when
82 // first created (this is probably being paranoid, since
83 // needsEvaluation is set when a connection is made to a engine
84 // output, but hi-ho...).
85 //
86 // Use: protected
87
88 SoEngine::SoEngine()
89 //
90 ////////////////////////////////////////////////////////////////////////
91 {
92 needsEvaluation = TRUE;
93 notifying = FALSE;
94 #ifdef DEBUG
95 if (! SoDB::isInitialized())
96 SoDebugError::post("SoEngine::SoEngine",
97 "Cannot construct engines before "
98 "calling SoDB::init()");
99 #endif /* DEBUG */
100 }
101
102 ////////////////////////////////////////////////////////////////////////
103 //
104 // Description:
105 // Destructor. Called when its ref count goes to zero.
106 //
107 // Use: protected
108
109 SoEngine::~SoEngine()
110 //
111 ////////////////////////////////////////////////////////////////////////
112 {
113 // Note: the SoEngine destructor doesn't have to worry about
114 // unhooking connections to its inputs or outputs for the
115 // following reasons:
116 // -- a engine will only be deleted when it's outputs are
117 // disconnected from everything, since making a connection to an
118 // output increments the engines reference count. So outputs
119 // are OK.
120 // -- The inputs are taken care of by their destructors (which
121 // call disconnect()) and by SoBase::~SoBase, which disconnects
122 // any forward connections to the engine's input fields.
123
124 // In the debug case, our output's destructors will make sure they
125 // aren't connected to anything, and will complain if they are.
126 }
127
128 ////////////////////////////////////////////////////////////////////////
129 //
130 // Description:
131 // Returns a list of outputs in this engine.
132 //
133 // Use: public
134
135 int
136 SoEngine::getOutputs(SoEngineOutputList &list) const
137 //
138 ////////////////////////////////////////////////////////////////////////
139 {
140 int i;
141 const SoEngineOutputData *od = getOutputData();
142
143 if (od == NULL)
144 return 0;
145
146 for (i = 0; i < od->getNumOutputs(); i++) {
147 list.append(od->getOutput(this, i));
148 }
149 return od->getNumOutputs();
150 }
151
152 ////////////////////////////////////////////////////////////////////////
153 //
154 // Description:
155 // Returns a pointer to the output with the given name. If no such
156 // output exists, NULL is returned.
157 //
158 // Use: public
159
160 SoEngineOutput *
161 SoEngine::getOutput(const SbName &outputName) const
162 //
163 ////////////////////////////////////////////////////////////////////////
164 {
165 int i;
166 const SoEngineOutputData *od = getOutputData();
167
168 if (od == NULL)
169 return NULL;
170
171 // Search outputs for one with given name
172 for (i = 0; i < od->getNumOutputs(); i++)
173 if (od->getOutputName(i) == outputName)
174 return od->getOutput(this, i);
175
176 // Not found...
177 return NULL;
178 }
179
180 ////////////////////////////////////////////////////////////////////////
181 //
182 // Description:
183 // Returns (in outputName) the name of the output pointed to.
184 // Returns FALSE if the output is not contained within the engine
185 // instance.
186 //
187 // Use: public
188
189 SbBool
190 SoEngine::getOutputName(const SoEngineOutput *output,
191 SbName &outputName) const
192 //
193 ////////////////////////////////////////////////////////////////////////
194 {
195 int i;
196 const SoEngineOutputData *od = getOutputData();
197
198 if (od == NULL)
199 return FALSE;
200
201 // Search outputs for one with given pointer
202 for (i = 0; i < od->getNumOutputs(); i++) {
203 if (od->getOutput(this, i) == output) {
204 outputName = od->getOutputName(i);
205 return TRUE;
206 }
207 }
208
209 // Not found...
210 return FALSE;
211 }
212
213 ////////////////////////////////////////////////////////////////////////
214 //
215 // Description:
216 // Creates and returns a copy of the engine. All connections to
217 // inputs are copied as is (without copying what's at the other end).
218 //
219 // Use: public
220
221 SoEngine *
222 SoEngine::copy() const
223 //
224 ////////////////////////////////////////////////////////////////////////
225 {
226 // Set up an empty copy dictionary. The dictionary has to exist
227 // for copying to work, even though this operation will never
228 // store anything in it.
229 initCopyDict();
230
231 // Create a copy of this engine
232 SoEngine *newEngine = (SoEngine *) getTypeId().createInstance();
233 newEngine->ref();
234
235 // Copy the contents
236 newEngine->copyContents(this, TRUE);
237
238 // Clean up
239 copyDone();
240
241 // Return the copy
242 newEngine->unrefNoDelete();
243 return newEngine;
244 }
245
246 ////////////////////////////////////////////////////////////////////////
247 //
248 // Description:
249 // Returns the last engine given the name 'name'. Returns NULL if
250 // there is no engine with the given name.
251 //
252 // Use: public
253
254 SoEngine *
255 SoEngine::getByName(const SbName &name)
256 //
257 ////////////////////////////////////////////////////////////////////////
258 {
259 return (SoEngine *)getNamedBase(name, SoEngine::getClassTypeId());
260 }
261
262 ////////////////////////////////////////////////////////////////////////
263 //
264 // Description:
265 // Adds all engines named 'name' to the list. Returns the number of
266 // engines found.
267 //
268 // Use: public
269
270 int
271 SoEngine::getByName(const SbName &name, SoEngineList &list)
272 //
273 ////////////////////////////////////////////////////////////////////////
274 {
275 return getNamedBases(name, list, SoEngine::getClassTypeId());
276 }
277
278 ////////////////////////////////////////////////////////////////////////
279 //
280 // Description:
281 // This engine is a wrapper around the user's evaluate() routine.
282 // It takes care of some bookkeeping tasks common to all engines,
283 // and is called by SoField::getValue() routines.
284 //
285 // Use: internal
286
287 void
288 SoEngine::evaluateWrapper()
289 //
290 ////////////////////////////////////////////////////////////////////////
291 {
292 if (!needsEvaluation) return;
293
294 // Break cycles:
295 needsEvaluation = FALSE;
296
297 int i;
298 const SoEngineOutputData *od = getOutputData();
299 for (i = 0; i < od->getNumOutputs(); i++) {
300 SoEngineOutput *out = od->getOutput(this, i);
301 out->prepareToWrite();
302 }
303 // Evaluate all our inputs:
304 // (this works around some problems with engines that
305 // don't always get their inputs during evaluation):
306 const SoFieldData *fieldData = getFieldData();
307 for (i = 0; i < fieldData->getNumFields(); i++) {
308 SoField *inputField = fieldData->getField(this, i);
309 inputField->evaluate();
310 }
311
312 evaluate();
313
314 for (i = 0; i < od->getNumOutputs(); i++) {
315 SoEngineOutput *out = od->getOutput(this, i);
316 out->doneWriting();
317 }
318 }
319
320 ////////////////////////////////////////////////////////////////////////
321 //
322 // Description:
323 // Propagates notification through the engine.
324 //
325 // Use: internal
326
327 void
328 SoEngine::notify(SoNotList *list)
329 //
330 ////////////////////////////////////////////////////////////////////////
331 {
332 if (notifying) return; // Avoid potential loops
333
334 // There's an annoying bug with engines that enable and disable
335 // their outputs in their inputChanged method that requires this
336 // flag:
337 notifying = TRUE;
338
339 needsEvaluation = TRUE;
340
341 SoNotRec *lastRec = list->getLastRec();
342 SbBool notifiedFromContainer =
343 (lastRec && lastRec->getType() == SoNotRec::CONTAINER);
344
345 // If we are being notified by a change to one of our fields
346 // (notification type is CONTAINER), pass that info on to any
347 // subclass that cares.
348 if (notifiedFromContainer) {
349 inputChanged(list->getLastField());
350 }
351
352 // We may have auditors (fields that point to this engine), so
353 // notify them before we add to the list
354 SoFieldContainer::notify(list);
355
356 // Append a record of type ENGINE with the base set to this
357 SoNotRec rec(this);
358 rec.setType(SoNotRec::ENGINE);
359 list->append(&rec);
360
361 // Now notify our outputs' connected fields
362 SoNotList workingList(list);
363 SbBool firstConnection = TRUE;
364
365 const SoEngineOutputData *od = getOutputData();
366 for (int i = 0; i < od->getNumOutputs(); i++) {
367 SoEngineOutput *out = od->getOutput(this, i);
368 if (out->isEnabled()) {
369 for (int j = 0; j < out->getNumConnections(); j++) {
370
371 // Make sure we save the original list for use each
372 // time through the loop
373 if (firstConnection)
374 firstConnection = FALSE;
375 else
376 workingList = *list;
377
378 (*out)[j]->notify(&workingList);
379 }
380 }
381 }
382 notifying = FALSE;
383 }
384
385 ////////////////////////////////////////////////////////////////////////
386 //
387 // Description:
388 // This is called whenever the value of an input is changed. The
389 // default method does nothing. Subclasses can override this to
390 // detect when a specific field is changed.
391 //
392 // Use: protected
393
394 void
395 SoEngine::inputChanged(SoField *)
396 //
397 ////////////////////////////////////////////////////////////////////////
398 {
399 }
400
401 ////////////////////////////////////////////////////////////////////////
402 //
403 // Description:
404 // Writes instance to SoOutput.
405 //
406 // Use: internal
407
408 void
409 SoEngine::writeInstance(SoOutput *out)
410 //
411 ////////////////////////////////////////////////////////////////////////
412 {
413 if (! writeHeader(out, FALSE, TRUE)) {
414
415 const SoFieldData *fieldData = getFieldData();
416
417 fieldData->write(out, this);
418 if (! isBuiltIn) getOutputData()->writeDescriptions(out, this);
419
420 writeFooter(out);
421 }
422 }
423
424 ////////////////////////////////////////////////////////////////////////
425 //
426 // Description:
427 // Reads stuff into instance of subclass of SoEngine.
428 // Returns FALSE on error.
429 //
430 // Use: protected
431
432 SbBool
433 SoEngine::readInstance(SoInput *in, unsigned short /* flags not used */)
434 //
435 ////////////////////////////////////////////////////////////////////////
436 {
437 if (in->isBinary()) {
438 SbBool notBuiltIn;
439 SbBool readOK = getFieldData()->read(in, this, TRUE, notBuiltIn);
440
441 if (readOK && notBuiltIn)
442 readOK = getOutputData()->readDescriptions(in, this);
443
444 return readOK;
445 } else {
446 SbBool notBuiltIn; // Not used
447 SbBool readOK = getFieldData()->read(in, this, FALSE, notBuiltIn);
448 if (!readOK) return readOK;
449
450 // See if "outputs" keyword is there:
451 SbName outputs;
452 if (!in->read(outputs, TRUE) || outputs != "outputs") {
453 return readOK;
454 }
455 // If it is, read outputs:
456 return getOutputData()->readDescriptions(in, this);
457 }
458 }
459
460 ////////////////////////////////////////////////////////////////////////
461 //
462 // Description:
463 // Writes output type information to SoOutput.
464 //
465 // Use: internal, protected
466
467 void
468 SoEngine::writeOutputTypes(SoOutput *out)
469 //
470 ////////////////////////////////////////////////////////////////////////
471 {
472 SbName outName;
473 SoEngineOutputList outList;
474
475 (void)getOutputs( outList );
476
477 // Write the output keyword
478 if (!out->isBinary())
479 out->indent();
480 out->write( "outputs" );
481
482 if (out->isBinary())
483 out->write(outList.getLength());
484 else {
485 out->write( ' ' );
486 out->write( OPEN_BRACE_CHAR );
487 out->write( ' ' );
488 out->incrementIndent(2);
489 }
490
491 // For each output, write out the output type and name
492 for( int i=0; i<outList.getLength(); i++ ) {
493
494 const SoType outType = outList[i]->getConnectionType();
495 getOutputName( outList[i], outName );
496
497 out->write( outType.getName().getString() );
498 if (!out->isBinary())
499 out->write( ' ' );
500 out->write( outName.getString() );
501
502 if (!out->isBinary()) {
503 if( i != outList.getLength()-1 ) {
504 out->write( VALUE_SEPARATOR_CHAR );
505 out->write( '\n' );
506 out->indent();
507 }
508 }
509 }
510 if (!out->isBinary()) {
511 out->write( ' ' );
512 out->write( CLOSE_BRACE_CHAR );
513 out->write( '\n' );
514 out->decrementIndent(2);
515 }
516 }
517
518 ////////////////////////////////////////////////////////////////////////
519 //
520 // Description:
521 // During a copy operation, this copies an instance that is
522 // encountered through a field connection.
523 //
524 // Use: internal, virtual
525
526 SoFieldContainer *
527 SoEngine::copyThroughConnection() const
528 //
529 ////////////////////////////////////////////////////////////////////////
530 {
531 // If there is already a copy of this engine, use it
532 SoFieldContainer *copy = findCopy(this, TRUE);
533 if (copy != NULL)
534 return copy;
535
536 // Otherwise, we need to figure out whether to create a copy of
537 // this engine or to just return "this". We should return a copy
538 // if this engine is "inside" (see SoNode::copy() for details). To
539 // determine this, we need to check if we are connected from any
540 // inside node or engine. (We know that if we get here, we must be
541 // connected TO at least one inside node or engine, so we don't
542 // have to worry about that direction.)
543
544 if (shouldCopy()) {
545
546 // Create and add a new instance to the dictionary
547 SoEngine *newEngine = (SoEngine *) getTypeId().createInstance();
548 newEngine->ref();
549 addCopy(this, newEngine); // Adds a ref()
550 newEngine->unrefNoDelete();
551
552 // Find the copy and return it; this has the side effect of
553 // copying the contents and letting the dictionary know it has
554 // been copied once.
555 return findCopy(this, TRUE);
556 }
557
558 // Otherwise, just return this
559 return (SoFieldContainer *) this;
560 }
561
562 ////////////////////////////////////////////////////////////////////////
563 //
564 // Description:
565 // Recursive procedure that determines if this engine should be
566 // copied during a copy operation, or just referenced as is.
567 //
568 // Use: internal
569
570 SbBool
571 SoEngine::shouldCopy() const
572 //
573 ////////////////////////////////////////////////////////////////////////
574 {
575 // We need to determine if we are connected from any "inside" node
576 // or engine. Look through each connected input to determine this.
577
578 const SoFieldData *fieldData = getFieldData();
579 if (fieldData != NULL) {
580 int numFields = fieldData->getNumFields();
581 for (int i = 0; i < numFields; i++) {
582 SoField *inputField = fieldData->getField(this, i);
583 if (inputField->referencesCopy())
584 return TRUE;
585 }
586 }
587
588 // If we get here, we didn't find any inside connections
589 return FALSE;
590 }
591
592 //////////////////////////////////////////////////////////////////////
593 //
594 // SoEngineOutput class
595 //
596 //////////////////////////////////////////////////////////////////////
597
598 ////////////////////////////////////////////////////////////////////////
599 //
600 // Description:
601 // Constructor. EngineOutputs may only be constructed inside
602 // SoEngine instances.
603 //
604 // Use: internal
605
606 SoEngineOutput::SoEngineOutput()
607 //
608 ////////////////////////////////////////////////////////////////////////
609 {
610 enabled = TRUE;
611 container = NULL;
612 }
613
614 ////////////////////////////////////////////////////////////////////////
615 //
616 // Description:
617 // Destructor. Called automatically when SoEngine destructor is
618 // called.
619 //
620 // Use: internal
621
622 SoEngineOutput::~SoEngineOutput()
623 //
624 ////////////////////////////////////////////////////////////////////////
625 {
626 // A engine should only be deleted once it has nothing connected
627 // to its outputs. So, in the DEBUG case, we'll make sure there
628 // are no more connections, and complain if there are.
629 #ifdef DEBUG
630 if (getNumConnections() != 0)
631 SoDebugError::post("SoEngineOutput::~SoEngineOutput",
632 "Engine with output connections deleted.\n"
633 "Did you unref() an engine that you didn't ref()?");
634 #endif /* DEBUG */
635 }
636
637 ////////////////////////////////////////////////////////////////////////
638 //
639 // Description:
640 // Return the type of SoField this output can write to.
641 //
642 // Use: public
643
644 SoType
645 SoEngineOutput::getConnectionType() const
646 //
647 ////////////////////////////////////////////////////////////////////////
648 {
649 // The connection type is stored in our container's
650 // static EngineOutputData member (because it is the same for
651 // all engine instances).
652 #ifdef DEBUG
653 if (container == NULL) {
654 SoDebugError::post("SoEngineOutput::getConnectionType",
655 "container is NULL!");
656 return SoType::badType();
657 }
658 #endif /* DEBUG */
659
660 const SoEngineOutputData *od = container->getOutputData();
661
662 #ifdef DEBUG
663 if (od == NULL) {
664 SoDebugError::post("SoEngineOutput::getConnectionType",
665 "container has no output data!");
666 return SoType::badType();
667 }
668 #endif /* DEBUG */
669
670 return od->getType(od->getIndex(container, this));
671 }
672
673 ////////////////////////////////////////////////////////////////////////
674 //
675 // Description:
676 // Returns the number of fields this output is writing to, and
677 // adds pointers to those fields to the given list:
678 //
679 // Use: public
680
681 int
682 SoEngineOutput::getForwardConnections(SoFieldList &list) const
683 //
684 ////////////////////////////////////////////////////////////////////////
685 {
686 int numConnections = 0;
687
688 for (int i = 0; i < connections.getLength(); i++) {
689 SoField *field = connections[i];
690
691 // Skip over converter, if any
692 SoFieldContainer *container = field->getContainer();
693 if (container->isOfType(SoFieldConverter::getClassTypeId()))
694 numConnections += ((SoFieldConverter *) container)->
695 getForwardConnections(list);
696
697 else {
698 list.append(field);
699 numConnections++;
700 }
701 }
702
703 return numConnections;
704 }
705
706 ////////////////////////////////////////////////////////////////////////
707 //
708 // Description:
709 // Enables or disables all connections from this
710 // output. The enabled flag is checked at SO_ENGINE_OUTPUT time and
711 // at notify() to prevent notification/evaluation through disabled
712 // outputs.
713 //
714 // Use: public
715
716 void
717 SoEngineOutput::enable(SbBool flag)
718 //
719 ////////////////////////////////////////////////////////////////////////
720 {
721 if (enabled != flag) {
722 enabled = flag;
723
724 // Notify if re-enabling connections
725 if (flag) {
726
727 // A very annoying double notification occurs with engines
728 // that enable their outputs during inputChanged that we
729 // prevent by not bothering to start notification if we're
730 // already in the middle of notification:
731 SoEngine *e = getContainer();
732 if (e && e->isNotifying()) return;
733
734 for (int j = 0; j < getNumConnections(); j++) {
735 SoField *f = (*this)[j];
736
737 if (!f->flags.isEngineModifying) {
738 f->startNotify();
739 }
740 }
741 }
742 }
743 }
744
745 ////////////////////////////////////////////////////////////////////////
746 //
747 // Description:
748 // Called by SoField connection code to add a connection. Can pass
749 // NULL to just mark engine as needing evaluation.
750 //
751 // Use: internal
752
753 void
754 SoEngineOutput::addConnection(SoField *field)
755 //
756 ////////////////////////////////////////////////////////////////////////
757 {
758 if (field) {
759 // Add to lists of connections
760 connections.append(field);
761
762 // Increment containing engine's reference count
763 container->ref();
764 }
765
766 // This forces the engine to write to the new connection.
767 container->needsEvaluation = TRUE;
768 }
769
770 ////////////////////////////////////////////////////////////////////////
771 //
772 // Description:
773 // Called by SoField::disconnect() to remove a connection.
774 //
775 // Use: internal
776
777 void
778 SoEngineOutput::removeConnection(SoField *field)
779 //
780 ////////////////////////////////////////////////////////////////////////
781 {
782 int index = connections.find(field);
783 #ifdef DEBUG
784 if (index == -1)
785 SoDebugError::post("SoEngineOutput::removeConnection",
786 "Field is not connected!");
787 #endif /* DEBUG */
788
789 connections.remove(index);
790
791 // Decrement reference count of containing engine
792 container->unref();
793 }
794
795 ////////////////////////////////////////////////////////////////////////
796 //
797 // Description:
798 // Prepare for writing into fields.
799 //
800 // Use: internal
801
802 void
803 SoEngineOutput::prepareToWrite() const
804 //
805 ////////////////////////////////////////////////////////////////////////
806 {
807 for (int i = connections.getLength()-1; i >= 0; i--) {
808 SoField *f = connections.get(i);
809 #ifdef DEBUG
810 if (f->flags.isEngineModifying) {
811 SoDebugError::post("SoEngineOutput::prepareToWrite",
812 "Internal field flags are wrong; "
813 "did you call engine->evaluate() "
814 "instead of engine->evaluateWrapper?");
815 }
816 #endif
817 f->flags.isEngineModifying = TRUE;
818 }
819 }
820
821 ////////////////////////////////////////////////////////////////////////
822 //
823 // Description:
824 // Done writing, reset notification flags.
825 //
826 // Use: internal
827
828 void
829 SoEngineOutput::doneWriting() const
830 //
831 ////////////////////////////////////////////////////////////////////////
832 {
833 for (int i = connections.getLength()-1; i >= 0; i--) {
834 SoField *f = connections.get(i);
835 f->flags.isEngineModifying = FALSE;
836 }
837 }