Debian Codegraph @

lib/database/src/so/fields/SoField.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,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.3 $
44 |
45 | Classes:
46 | SoField
47 | SoSField
48 | SoMField
49 |
50 | Author(s) : Paul S. Strauss, Nick Thompson
51 |
52 ______________ S I L I C O N G R A P H I C S I N C . ____________
53 _______________________________________________________________________
54 */
55
56
57 #include <Inventor/SoDB.h>
58 #include <Inventor/SoInput.h>
59 #include <Inventor/misc/SoNotification.h>
60 #include <Inventor/SoOutput.h>
61 #include <Inventor/actions/SoWriteAction.h>
62 #include <Inventor/engines/SoEngine.h>
63 #include <Inventor/engines/SoFieldConverter.h>
64 #include <Inventor/errors/SoDebugError.h>
65 #include <Inventor/errors/SoReadError.h>
66 #include <Inventor/fields/SoFieldContainer.h>
67 #include <Inventor/fields/SoSubField.h>
68 #include <Inventor/nodes/SoNode.h>
69 #include <Inventor/sensors/SoDataSensor.h>
70 #include <cstdlib>
71 #include "SoGlobalField.h"
72
73 // Special characters in files
74 #define OPEN_BRACE_CHAR '['
75 #define CLOSE_BRACE_CHAR ']'
76 #define VALUE_SEPARATOR_CHAR ','
77 #define IGNORE_CHAR '~'
78 #define CONNECTION_CHAR '='
79 #define FIELD_SEP_CHAR '.'
80
81 // Bit flags in flags token in binary files
82 #define FIELD_IGNORED 0x01
83 #define FIELD_CONNECTED 0x02
84 #define FIELD_DEFAULT 0x04
85
86 // Amount of values to allocate at a time when reading in multiple values.
87 #define VALUE_CHUNK_SIZE 32
88
89 // If the field is connected or there are any FieldSensors attached to
90 // this field, flags.hasAuditors will be TRUE, and this structure is
91 // used to contain the extra information needed. Done this way to
92 // save space in the common case.
93 struct SoFieldAuditorInfo {
94 SoFieldContainer *container;
95
96 // List of auditors: objects to pass notification to.
97 SoAuditorList auditors;
98
99 // The "connection" field points to either an engine output or
100 // another field:
101 union {
102 SoEngineOutput *engineOutput;
103 SoField *field;
104 } connection;
105
106 };
107
108
109 //////////////////////////////////////////////////////////////////////////////
110 //
111 // Class: SoField
112 //
113 // Useful stuff for ALL fields, regardless of type or multiplicity
114 //
115 //////////////////////////////////////////////////////////////////////////////
116
117 SoType SoField::classTypeId; // Type identifier
118 char *SoField::fieldBuf; // Used by SoField::get()
119 int SoField::fieldBufSize; // Used by SoField::get()
120
121 ////////////////////////////////////////////////////////////////////////
122 //
123 // Description:
124 // Helper routine, allocates the auditorInfo structure, if needed:
125 //
126 // Use: private, static
127
128 void
129 SoField::createAuditorInfo()
130 //
131 ////////////////////////////////////////////////////////////////////////
132 {
133 if (!flags.hasAuditors) {
134 SoFieldContainer *myContainer = container;
135 auditorInfo = new SoFieldAuditorInfo;
136 auditorInfo->container = myContainer;
137 auditorInfo->connection.field = NULL;
138 flags.hasAuditors = TRUE;
139 }
140 }
141
142 ////////////////////////////////////////////////////////////////////////
143 //
144 // Description:
145 // Constructor.
146 //
147 // Use: public
148
149 SoField::SoField()
150 //
151 ////////////////////////////////////////////////////////////////////////
152 {
153 container = NULL;
154 flags.hasDefault = TRUE;
155 flags.ignored = FALSE;
156 flags.connected = FALSE;
157 flags.converted = FALSE;
158 flags.connectionEnabled = TRUE;
159 flags.notifyEnabled = FALSE;
160 flags.hasAuditors = FALSE;
161 flags.isEngineModifying = FALSE;
162 flags.readOnly = FALSE;;
163 flags.dirty = FALSE;
164 }
165
166 ////////////////////////////////////////////////////////////////////////
167 //
168 // Description:
169 // Destructor. Also disconnects if connected.
170 //
171 // Use: public
172
173 SoField::~SoField()
174 //
175 ////////////////////////////////////////////////////////////////////////
176 {
177 if (flags.connected)
178 reallyDisconnect();
179
180 // Make sure any auditors are notified of the impending doom of
181 // this field. We don't have to notify the container, since we
182 // can't be destroyed unless it is already being destroyed. So
183 // just notify the rest of the auditors. The only type of auditors
184 // that can be attached to a field are sensors and other fields
185 // connected from this one. Both of these are given a chance to
186 // detach/disconnect themselves.
187
188 if (flags.hasAuditors) {
189 SoAuditorList &auditors = auditorInfo->auditors;
190 for (int i = auditors.getLength() - 1; i >= 0; i--) {
191 switch (auditors.getType(i)) {
192 case SoNotRec::SENSOR:
193 // Tell sensor that we are going away. (This must be a
194 // field sensor, but cast it to a data sensor for ease.)
195 ((SoDataSensor *) auditors.getObject(i))->dyingReference();
196
197 // The call to dyingReference() might remove auditors,
198 // shortening the auditors list; make sure we're not
199 // trying to access past the end.
200 if (i > auditors.getLength())
201 i = auditors.getLength();
202 break;
203
204 case SoNotRec::FIELD:
205 {
206 // Disconnect other field from this one
207 SoField *f = (SoField *)auditors.getObject(i);
208 SoFieldContainer *fc = f->getContainer();
209
210 // If connected to a converter, must remove the other
211 // side of the connection:
212 if (fc->isOfType(SoFieldConverter::getClassTypeId())) {
213 SoFieldConverter *converter =
214 (SoFieldConverter *)fc;
215 SoFieldList fieldList;
216 converter->getForwardConnections(fieldList);
217 for (int j = 0; j < fieldList.getLength(); j++) {
218 fieldList[0]->disconnect();
219 }
220 // The converter will be deleted and will
221 // disconnect itself from us when all of its
222 // forward connections go away.
223 }
224 else {
225 f->disconnect();
226 }
227 }
228 break;
229
230 default:
231 SoDebugError::post("(internal) SoField::~SoField",
232 "Got an auditor of type %d",
233 (int) auditors.getType(i));
234 break;
235 }
236 }
237 delete auditorInfo;
238 }
239 }
240
241 ////////////////////////////////////////////////////////////////////////
242 //
243 // Description:
244 // Returns TRUE if field is an instance of a field of the given type
245 // or an instance of a subclass of it.
246 //
247 // Use: public
248
249 SbBool
250 SoField::isOfType(SoType type) const
251 //
252 ////////////////////////////////////////////////////////////////////////
253 {
254 return getTypeId().isDerivedFrom(type);
255 }
256
257 ////////////////////////////////////////////////////////////////////////
258 //
259 // Description:
260 // Sets whether connection to field or engine is enabled. This
261 // flag may be set even if no connection is currently established.
262 // The flag will remain in effect until changed again.
263 //
264 // Use: public
265
266 void
267 SoField::enableConnection(SbBool flag)
268 //
269 ////////////////////////////////////////////////////////////////////////
270 {
271 if (flags.connectionEnabled == flag) return;
272
273 // Before disabling, pull value through if out-of-date:
274 if (!flag) {
275 evaluate();
276 flags.connectionEnabled = FALSE;
277 flags.readOnly = TRUE;
278
279 if (isConnectedFromField() && !flags.converted)
280 auditorInfo->connection.field->connectionStatusChanged(-1);
281 }
282 else {
283 // Mark the field dirty when re-enabling so it will get
284 // evaluated.
285 flags.readOnly = FALSE;
286 flags.connectionEnabled = TRUE;
287 flags.dirty = TRUE;
288
289 if (isConnectedFromEngine() || flags.converted) {
290 // Mark engine as needing evaluation to force it to write
291 // value (just as if we added a connection):
292 auditorInfo->connection.engineOutput->addConnection(NULL);
293 }
294 else if (isConnectedFromField()) {
295 auditorInfo->connection.field->connectionStatusChanged(1);
296 }
297 evaluate(); // Pull value through
298 }
299 }
300
301 ////////////////////////////////////////////////////////////////////////
302 //
303 // Description:
304 // Connects the field to the given output of an engine. Returns
305 // FALSE if the connection could not be made.
306 //
307 // Use: public
308
309 SbBool
310 SoField::connectFrom(SoEngineOutput *engineOutput)
311 //
312 ////////////////////////////////////////////////////////////////////////
313 {
314 SoFieldContainer *engineCont = engineOutput->getContainer();
315
316 #ifdef DEBUG
317 // Make sure everything is contained, or this won't work at all
318 if (engineCont == NULL) {
319 SoDebugError::post("SoField::connectFrom",
320 "Can't connect to an engine output "
321 "that is not contained in an engine");
322 return FALSE;
323 }
324 #endif
325
326 // Ref the engine, just in case it's currently connected, and this
327 // is its only ref
328 engineCont->ref();
329
330 // Disconnect any previous connection
331 disconnect();
332
333 // Check the type of the field and the output for compatability
334 SoType outputType = engineOutput->getConnectionType();
335 if (getTypeId() != outputType) {
336
337 SbBool ret;
338 SoFieldConverter *converter;
339
340 converter = createConverter(outputType);
341
342 // Check for error
343 if (converter == NULL)
344 ret = FALSE;
345 else {
346 converter->ref();
347
348 // Hook the converter up to the other field first, then
349 // hook this up to the converter, to avoid multiple
350 // notifications or evaluations if something downstream is
351 // pulling values during notification:
352 // converter:
353 SoField *c_input = converter->getInput(outputType);
354 SoEngineOutput *c_output =
355 converter->getOutput(getTypeId());
356 #ifdef DEBUG
357 if (c_input == NULL || c_output == NULL) {
358 SoDebugError::post("SoField::connectFrom",
359 "Created converter, but converter"
360 "input or output is NULL");
361 return FALSE;
362 }
363 #endif
364 // Making the connection may result in downstream engines
365 // requesting evaluation, so this must be set before the
366 // connection is made:
367 flags.converted = TRUE;
368 flags.fromEngine = TRUE;
369
370 c_input->connectFrom(engineOutput);
371 connectFrom(c_output);
372
373 converter->unref();
374 ret = TRUE;
375 }
376 // See comment below
377 engineCont->unrefNoDelete();
378
379 return ret;
380 }
381
382 createAuditorInfo();
383
384 flags.connected = TRUE;
385
386 // If converted, this flag was set when the converter was created:
387 if (!flags.converted)
388 flags.fromEngine = TRUE;
389
390 auditorInfo->connection.engineOutput = engineOutput;
391
392 // Tell the engine output about this connection
393 engineOutput->addConnection(this);
394
395 if (isConnectionEnabled() && engineOutput->isEnabled()) {
396 // A connection means that the field no longer contains the
397 // default value
398 setDefault(FALSE);
399
400 // Notify
401 startNotify();
402 }
403
404 // Get rid of the extra reference. Note that if the container was
405 // handed to us with 0 references, we don't want to delete it,
406 // since presumably whoever asked for the connection still wants
407 // it, and will ref it later.
408 engineCont->unrefNoDelete();
409
410 return TRUE;
411 }
412
413 ////////////////////////////////////////////////////////////////////////
414 //
415 // Description:
416 // Connects the field to the given field. Returns FALSE if the
417 // connection could not be made.
418 //
419 // Use: public
420
421 SbBool
422 SoField::connectFrom(SoField *field)
423 //
424 ////////////////////////////////////////////////////////////////////////
425 {
426 // Disconnect any previous connection
427 disconnect();
428
429 // Check the types of the two fields
430 SoType fieldType = field->getTypeId();
431 if (getTypeId() != fieldType) {
432
433 SbBool ret;
434 SoFieldConverter *converter;
435
436 converter = createConverter(fieldType);
437
438 // Check for error
439 if (converter == NULL)
440 ret = FALSE;
441 else {
442 converter->ref();
443
444 // Hook the converter up to the other field first, then
445 // hook this up to the converter, to avoid multiple
446 // notifications or evaluations if something downstream is
447 // pulling values during notification:
448 // converter:
449 SoField *c_input = converter->getInput(fieldType);
450 SoEngineOutput *c_output =
451 converter->getOutput(getTypeId());
452 #ifdef DEBUG
453 if (c_input == NULL || c_output == NULL) {
454 SoDebugError::post("SoField::connectFrom",
455 "Created converter, but converter"
456 "input or output is NULL");
457 return FALSE;
458 }
459 #endif
460 // Making the connection may result in downstream engines
461 // requesting evaluation, so this must be set before the
462 // connection is made:
463 flags.converted = TRUE;
464 flags.fromEngine = FALSE;
465
466 c_input->connectFrom(field);
467 connectFrom(c_output);
468
469 converter->unref();
470 ret = TRUE;
471 }
472 return ret;
473 }
474
475 createAuditorInfo();
476
477 flags.connected = TRUE;
478
479 // If converted, this flag was set when the converter was created:
480 if (!flags.converted)
481 flags.fromEngine = FALSE;
482
483 auditorInfo->connection.field = field;
484
485 // Make sure this field gets notified when the connected field
486 // changes
487 field->addAuditor(this, SoNotRec::FIELD);
488
489 if (isConnectionEnabled()) {
490 // A connection means that the field no longer contains the
491 // default value
492 setDefault(FALSE);
493
494 // Notify
495 startNotify();
496 }
497
498 return TRUE;
499 }
500
501 ////////////////////////////////////////////////////////////////////////
502 //
503 // Description:
504 // Disconnects the field from whatever it's connected to. Harmless
505 // if not already connected.
506 //
507 // Use: public
508
509 void
510 SoField::disconnect()
511 //
512 ////////////////////////////////////////////////////////////////////////
513 {
514 if (flags.connected) {
515
516 // Make sure the field is evaluated first. (There may have
517 // been a notification of the field but no corresponding
518 // evaluation before the field was disconnected.)
519 evaluate();
520
521 reallyDisconnect();
522 }
523 }
524
525 ////////////////////////////////////////////////////////////////////////
526 //
527 // Description:
528 // Returns the engine output the field is connected to. Returns
529 // FALSE if there is no connection to an engine.
530 //
531 // Use: public
532
533 SbBool
534 SoField::getConnectedEngine(SoEngineOutput *&engineOut) const
535 //
536 ////////////////////////////////////////////////////////////////////////
537 {
538 if (! isConnectedFromEngine())
539 return FALSE;
540
541 // Skip over field converter, if any
542 const SoField *connectedField =
543 (! flags.converted ? this : getConverter()->getConnectedInput());
544
545 engineOut = connectedField->auditorInfo->connection.engineOutput;
546
547 return TRUE;
548 }
549
550 ////////////////////////////////////////////////////////////////////////
551 //
552 // Description:
553 // Returns the field the field is connected to. Returns FALSE if
554 // there is no connection to a field.
555 //
556 // Use: public
557
558 SbBool
559 SoField::getConnectedField(SoField *&field) const
560 //
561 ////////////////////////////////////////////////////////////////////////
562 {
563 if (! isConnectedFromField())
564 return FALSE;
565
566 // Skip over field converter, if any
567 const SoField *connectedField =
568 (! flags.converted ? this : getConverter()->getConnectedInput());
569
570 field = connectedField->auditorInfo->connection.field;
571
572 return TRUE;
573 }
574
575 ////////////////////////////////////////////////////////////////////////
576 //
577 // Description:
578 // Returns the number of fields this field is writing into, and
579 // adds pointers to those fields to the given field list.
580 //
581 // Use: public
582
583 int
584 SoField::getForwardConnections(SoFieldList &list) const
585 //
586 ////////////////////////////////////////////////////////////////////////
587 {
588 if (!flags.hasAuditors) return 0;
589
590 int numAuditors, numConnections = 0, i;
591
592 // Loop through our auditor list, finding fields we are connected
593 // to.
594 SoAuditorList &auditors = auditorInfo->auditors;
595 numAuditors = auditors.getLength();
596 for (i = 0; i < numAuditors; i++) {
597 if (auditors.getType(i) == SoNotRec::FIELD) {
598
599 SoField *field = (SoField *) auditors.getObject(i);
600
601 // Skip over converter, if any
602 SoFieldContainer *container = field->getContainer();
603 if (container->isOfType(SoFieldConverter::getClassTypeId()))
604 numConnections += ((SoFieldConverter *) container)->
605 getForwardConnections(list);
606
607 else {
608 list.append(field);
609 numConnections++;
610 }
611 }
612 }
613
614 return numConnections;
615 }
616
617 ////////////////////////////////////////////////////////////////////////
618 //
619 // Description:
620 // Returns the containing node or engine.
621 //
622 // Use: public
623
624 SoFieldContainer *
625 SoField::getContainer() const
626 //
627 ////////////////////////////////////////////////////////////////////////
628 {
629 if (flags.hasAuditors)
630 return auditorInfo->container;
631 else
632 return container;
633 }
634
635 ////////////////////////////////////////////////////////////////////////
636 //
637 // Description:
638 // Sets value of field from the Inventor data file format
639 // information in the value string. Returns TRUE if successful,
640 // FALSE otherwise.
641 //
642 // Use: public
643
644 SbBool
645 SoField::set(const char *valueString)
646 //
647 ////////////////////////////////////////////////////////////////////////
648 {
649 SoInput in;
650 in.setBuffer((void *) valueString, strlen(valueString));
651 return read(&in, "<field passed to SoField::set>");
652 }
653
654 ////////////////////////////////////////////////////////////////////////
655 //
656 // Description:
657 // Stores field value (in the same format expected by the set()
658 // method) in the given SbString. This always returns a string,
659 // even if the field has a default value.
660 //
661 // Use: public
662
663 void
664 SoField::get(SbString &valueString)
665 //
666 ////////////////////////////////////////////////////////////////////////
667 {
668 SoOutput out;
669
670 SbBool wasDefault = isDefault();
671
672 if (wasDefault)
673 setDefault(FALSE);
674
675 // Make sure the field value is up to date
676 evaluate();
677
678 // Prepare a character buffer and SoOutput for writing field strings
679 if (fieldBufSize == 0) {
680 fieldBufSize = 1028;
681 fieldBuf = (char *) malloc((unsigned) fieldBufSize);
682 }
683
684 // Set up output into a string buffer
685 out.setBuffer((void *) fieldBuf, fieldBufSize, &SoField::reallocFieldBuf);
686
687 // Make sure that the file header and lots of white space will NOT
688 // be written into the string
689 out.setCompact(TRUE);
690
691 // Prepare to begin writing by incrementing the
692 // current write counter in SoBase
693 SoBase::incrementCurrentWriteCounter();
694
695 // Count references first
696 out.setStage(SoOutput::COUNT_REFS);
697 countWriteRefs(&out);
698
699 // Next, do actual writing
700 out.setStage(SoOutput::WRITE);
701 writeValue(&out);
702
703 // SoField::get() is used by field converters that convert to
704 // strings when evaluating. Because this can happen in the middle
705 // of writing a scene graph, we have to restore things to the way
706 // they were before the get().
707 SoBase::decrementCurrentWriteCounter();
708 out.setStage(SoOutput::COUNT_REFS);
709 countWriteRefs(&out);
710
711 // Make sure the string is terminated
712 out.reset();
713
714 // Store the result in the passed SbString
715 valueString = fieldBuf;
716
717 setDefault(wasDefault);
718 }
719
720 ////////////////////////////////////////////////////////////////////////
721 //
722 // Description:
723 // Touches a field, starts notification
724 //
725 // Use: public
726
727 void
728 SoField::touch()
729 //
730 ////////////////////////////////////////////////////////////////////////
731 {
732 evaluate();
733 valueChanged(FALSE);
734 }
735
736 ////////////////////////////////////////////////////////////////////////
737 //
738 // Description:
739 // Sets the ignore flag on a field. Causes notification.
740 //
741 // Use: public
742
743 void
744 SoField::setIgnored(SbBool ig)
745 //
746 ////////////////////////////////////////////////////////////////////////
747 {
748 if (flags.ignored != ig) {
749 flags.ignored = ig;
750
751 // Indicate that the value changed, but leave the default flag as is
752 valueChanged(FALSE);
753 }
754 }
755
756 ////////////////////////////////////////////////////////////////////////
757 //
758 // Description:
759 // Initiates notification due to a change in the field.
760 //
761 // Use: internal
762
763 void
764 SoField::startNotify()
765 //
766 ////////////////////////////////////////////////////////////////////////
767 {
768 if (!flags.notifyEnabled || flags.isEngineModifying)
769 return;
770
771 SoDB::startNotify();
772
773 // Create a new notification list:
774 SoNotList list;
775
776 // Use notify() method to do all the work:
777 notify(&list);
778
779 SoDB::endNotify();
780 }
781
782 ////////////////////////////////////////////////////////////////////////
783 //
784 // Description:
785 // Propagates notification through the field. The notification must
786 // be coming from the engine output or field from which this
787 // field is connected.
788 //
789 // Use: internal
790
791 void
792 SoField::notify(SoNotList *list)
793 //
794 ////////////////////////////////////////////////////////////////////////
795 {
796 // If notification is disabled, don't do anything.
797 if (flags.dirty || ! flags.notifyEnabled || flags.isEngineModifying)
798 return;
799
800 // If being notified through a connection and the connection is
801 // disabled, don't notify:
802 if (!flags.connectionEnabled && (list->getLastRec() != NULL)) {
803
804 SoNotRec::Type t = list->getLastRec()->getType();
805 if (t == SoNotRec::ENGINE || t == SoNotRec::FIELD)
806 return;
807 }
808
809 // Indicate that we are notifying, to break future cycles.
810 // SFTrigger relies on this being done BEFORE checking for a NULL
811 // field container.
812 flags.dirty = TRUE;
813
814 // Propagate to all auditors.
815 // NOTE: Since this may be done for fields that are being
816 // constructed, we have to check for a NULL container first.
817 // NOTE: SFTrigger fields set their container to NULL temporarily
818 // when being read in to prevent notification propogating when
819 // they're read.
820 SoFieldContainer *cont = getContainer();
821
822 if (cont != NULL) {
823
824 // First create and append a new record to indicate we passed
825 // through this field.
826 SoNotRec rec(cont);
827 list->append(&rec, this);
828 list->setLastType(SoNotRec::CONTAINER);
829
830 // If no auditors, just notify container
831 if (! flags.hasAuditors)
832 cont->notify(list);
833
834 // Otherwise, notify container and auditors. We have to make
835 // sure to copy the list before notifying anyone.
836 else {
837 SoNotList listCopy(*list);
838 cont->notify(list);
839 auditorInfo->auditors.notify(&listCopy);
840 }
841 }
842 }
843
844 ////////////////////////////////////////////////////////////////////////
845 //
846 // Description:
847 // Sets the containing node, engine, or global. Also marks this
848 // field as Default, and turns on notification (this saves the
849 // nodes from having to do it).
850 //
851 // Use: internal
852
853 void
854 SoField::setContainer(SoFieldContainer *cont)
855 //
856 ////////////////////////////////////////////////////////////////////////
857 {
858 if (flags.hasAuditors)
859 auditorInfo->container = cont;
860 else
861 container = cont;
862 setDefault(TRUE);
863 enableNotify(TRUE);
864 }
865
866 ////////////////////////////////////////////////////////////////////////
867 //
868 // Description:
869 // Returns TRUE if the field (or its description) needs to be
870 // written out.
871 //
872 // Use: internal
873
874 SbBool
875 SoField::shouldWrite() const
876 //
877 ////////////////////////////////////////////////////////////////////////
878 {
879 if (!isDefault() || isConnected() || isIgnored()) return TRUE;
880
881 // The hard case; if we have any forward connections, should also
882 // write ourselves out (actually, that's only strictly necessary
883 // if the field is part of a non-builtin node...).
884
885 if (!flags.hasAuditors) return FALSE;
886
887 // Loop through our auditor list, finding fields we are connected
888 // to.
889 SoAuditorList &auditors = auditorInfo->auditors;
890 int numAuditors = auditors.getLength();
891 for (int i = 0; i < numAuditors; i++) {
892 if (auditors.getType(i) == SoNotRec::FIELD) {
893 return TRUE;
894 }
895 }
896 return FALSE;
897 }
898
899 ////////////////////////////////////////////////////////////////////////
900 //
901 // Description:
902 // Adds an auditor to list of auditors.
903 //
904 // Use: private
905
906 void
907 SoField::addAuditor(void *auditor, SoNotRec::Type type)
908 //
909 ////////////////////////////////////////////////////////////////////////
910 {
911 #ifdef DEBUG
912 if (auditor == NULL) {
913 SoDebugError::post("SoField::addAuditor",
914 "ACK! Trying to add a NULL auditor");
915 return;
916 }
917 #endif
918 createAuditorInfo();
919
920 auditorInfo->auditors.append(auditor, type);
921
922 // Connection status is different
923 connectionStatusChanged(1);
924 }
925
926 ////////////////////////////////////////////////////////////////////////
927 //
928 // Description:
929 // Removes an auditor from list of auditors.
930 //
931 // Use: private
932
933 void
934 SoField::removeAuditor(void *auditor, SoNotRec::Type type)
935 //
936 ////////////////////////////////////////////////////////////////////////
937 {
938 int audIndex = -1;
939 if (flags.hasAuditors)
940 audIndex = auditorInfo->auditors.find(auditor, type);
941
942 #ifdef DEBUG
943 if (audIndex < 0) {
944 SoDebugError::post("SoField::removeAuditor",
945 "can't find auditor %#x\n", auditor);
946 return;
947 }
948 #endif /* DEBUG */
949
950 auditorInfo->auditors.remove(audIndex);
951
952 // Connection status is different
953 connectionStatusChanged(-1);
954 }
955
956 ////////////////////////////////////////////////////////////////////////
957 //
958 // Description:
959 // Enables or disables notification
960 //
961 // Use: internal
962
963 SbBool
964 SoField::enableNotify(SbBool flag)
965 //
966 ////////////////////////////////////////////////////////////////////////
967 {
968 if (flags.notifyEnabled == flag) return flag;
969
970 if (flag) evaluate();
971
972 flags.notifyEnabled = flag;
973 return !flag; // Previous state of flag is returned
974 }
975
976 ////////////////////////////////////////////////////////////////////////
977 //
978 // Description:
979 // Indicates to a field that a change has been made involving a
980 // connection from it (as source) to another field. The default
981 // method does nothing. Used by the subclass of SFTime used by the
982 // realTime global field to disable/enable the real time update
983 // sensor as things are connected from it or disconnected from it.
984 //
985 // Use: internal, virtual
986
987 void
988 SoField::connectionStatusChanged(int)
989 //
990 ////////////////////////////////////////////////////////////////////////
991 {
992 }
993
994 ////////////////////////////////////////////////////////////////////////
995 //
996 // Description:
997 // After a field value has been copied using copyFrom(),
998 // this is called to allow fields to update the copy. This is used
999 // by node, engine, and path fields to make sure instances are
1000 // handled properly. The default implementation does nothing.
1001 //
1002 // Use: internal, virtual
1003
1004 void
1005 SoField::fixCopy(SbBool)
1006 //
1007 ////////////////////////////////////////////////////////////////////////
1008 {
1009 }
1010
1011 ////////////////////////////////////////////////////////////////////////
1012 //
1013 // Description:
1014 // This returns TRUE if this field contains a reference to a node
1015 // or engine that is copied during a copy operation (i.e., it is
1016 // "inside"). The default method just checks if the field is
1017 // connected to such a node or engine. Subclasses may contain other
1018 // tests, such as those that contain pointers to nodes or engines.
1019 //
1020 // Use: internal, virtual
1021
1022 SbBool
1023 SoField::referencesCopy() const
1024 //
1025 ////////////////////////////////////////////////////////////////////////
1026 {
1027 // If this field is not connected, it doesn't reference anything
1028 if (! isConnected())
1029 return FALSE;
1030
1031 // Find the container of the connected field or engine
1032 // output, and determine if it is an engine
1033 SoFieldContainer *container;
1034 SbBool containerKnownToBeEngine = FALSE;
1035
1036 if (isConnectedFromField()) {
1037 SoField *connectedField;
1038 getConnectedField(connectedField);
1039 container = connectedField->getContainer();
1040 containerKnownToBeEngine = FALSE;
1041 }
1042 else {
1043 SoEngineOutput *connectedOutput;
1044 getConnectedEngine(connectedOutput);
1045 container = (SoEngine *) connectedOutput->getContainer();
1046 containerKnownToBeEngine = TRUE;
1047 }
1048
1049 // If a copy of the container exists, this field references a copy
1050 if (SoFieldContainer::checkCopy(container) != NULL)
1051 return TRUE;
1052
1053 // If the container is an engine, see if that engine should be
1054 // copied, recursively.
1055 // ??? Optimize this by keeping a dict of "outside"
1056 // ??? engines so we never do the same work twice?
1057 // ??? We could actually store a pointer to the original engine in
1058 // ??? the dictionary if it is outside, but then we have to be
1059 // ??? careful to test for this when we call checkCopy(). It's
1060 // ??? probably not worth the effort, since people won't copy
1061 // ??? complicated engine networks often.
1062 if (containerKnownToBeEngine ||
1063 container->isOfType(SoEngine::getClassTypeId()) &&
1064 ((SoEngine *) container)->shouldCopy())
1065 return TRUE;
1066
1067 return FALSE;
1068 }
1069
1070 ////////////////////////////////////////////////////////////////////////
1071 //
1072 // Description:
1073 // Copies connection from one field to another. Assumes fields are
1074 // the same subclass and that fromField is connected. The index of
1075 // the field in the SoFieldData instance is passed in.
1076 //
1077 // Use: internal
1078
1079 void
1080 SoField::copyConnection(const SoField *fromField)
1081 //
1082 ////////////////////////////////////////////////////////////////////////
1083 {
1084 // Connected from another field:
1085 if (fromField->isConnectedFromField()) {
1086 SoField *connectedField;
1087 fromField->getConnectedField(connectedField);
1088
1089 // Find the index of the connected field in its container
1090 SoFieldContainer *connectedFC = connectedField->getContainer();
1091 const SoFieldData *fieldData = connectedFC->getFieldData();
1092 int index = fieldData->getIndex(connectedFC, connectedField);
1093
1094 // We may need to copy the field's container
1095 SoFieldContainer *FCCopy = connectedFC->copyThroughConnection();
1096
1097 // Get the other field data in case it is different
1098 const SoFieldData *otherFieldData = FCCopy->getFieldData();
1099
1100 // Connect from the corresponding field in the container
1101 connectFrom(otherFieldData->getField(FCCopy, index));
1102 }
1103
1104 // Connected from engine:
1105 else {
1106 SoEngineOutput *connectedOutput;
1107 fromField->getConnectedEngine(connectedOutput);
1108
1109 // Find the index of this output in the containing engine
1110 SoEngine *connectedEngine = connectedOutput->getContainer();
1111 const SoEngineOutputData *outputData =connectedEngine->getOutputData();
1112 int outputIndex = outputData->getIndex(connectedEngine,
1113 connectedOutput);
1114
1115 // We may need to copy the engine itself
1116 SoEngine *engineCopy =
1117 (SoEngine *) connectedEngine->copyThroughConnection();
1118
1119 // Get the other output data in case it is different
1120 const SoEngineOutputData *outputDataCopy = engineCopy->getOutputData();
1121
1122 // Connect from the corresponding output in the engine copy
1123 connectFrom(outputDataCopy->getOutput(engineCopy, outputIndex));
1124 }
1125
1126 // Make sure state of connection is identical
1127 if (! fromField->isConnectionEnabled())
1128 enableConnection(FALSE);
1129 }
1130
1131 ////////////////////////////////////////////////////////////////////////
1132 //
1133 // Description:
1134 // Called by an instance to indicate that a value has changed. If
1135 // resetDefault is TRUE, this turns off default flag. Initiates
1136 // notification to clear downstream dirty bits, and evaluates
1137 // (after protecting the value in the field) to clear upstream
1138 // dirty bits.
1139 //
1140 // Use: protected
1141
1142 void
1143 SoField::valueChanged(SbBool resetDefault) // Default is TRUE
1144 //
1145 ////////////////////////////////////////////////////////////////////////
1146 {
1147 if (resetDefault)
1148 setDefault(FALSE);
1149
1150 flags.readOnly = TRUE;
1151 flags.dirty = FALSE; // So notification WILL happen, at least as
1152 // far as sensors/field container
1153 startNotify();
1154 evaluate();
1155 flags.readOnly = !flags.connectionEnabled;
1156 }
1157
1158 ////////////////////////////////////////////////////////////////////////
1159 //
1160 // Description:
1161 // Really disconnects the field from whatever it's connected to.
1162 //
1163 // Use: private
1164
1165 void
1166 SoField::reallyDisconnect()
1167 //
1168 ////////////////////////////////////////////////////////////////////////
1169 {
1170 if (flags.fromEngine || flags.converted) {
1171 SoEngineOutput *out = auditorInfo->connection.engineOutput;
1172 out->removeConnection(this);
1173 auditorInfo->connection.engineOutput = NULL;
1174 }
1175 else {
1176 SoField *field = auditorInfo->connection.field;
1177 field->removeAuditor(this, SoNotRec::FIELD);
1178 auditorInfo->connection.field = NULL;
1179 }
1180 flags.connected = FALSE;
1181 flags.converted = FALSE;
1182 }
1183
1184 ////////////////////////////////////////////////////////////////////////
1185 //
1186 // Description:
1187 // Evaluates the field or engine the field is connected to,
1188 // storing the result in the field.
1189 //
1190 // Use: private
1191
1192 void
1193 SoField::evaluateConnection() const
1194 //
1195 ////////////////////////////////////////////////////////////////////////
1196 {
1197 // Cast away the nasty const...
1198 SoField *f = (SoField *) this;
1199 f->flags.dirty = FALSE; // This must be done before upstream
1200 // evaluation to break evaluation cycles.
1201
1202 if (flags.isEngineModifying ||
1203 !flags.connected || !flags.connectionEnabled) return;
1204
1205 // If connected to an engine, evaluate that engine. This will
1206 // cause the value to be written into the field by the engine.
1207 if (flags.fromEngine || flags.converted) {
1208
1209 SoEngine *e =
1210 auditorInfo->connection.engineOutput->getContainer();
1211 e->evaluateWrapper();
1212 }
1213
1214 // If connected to a field, just copy the value from that field
1215 // UNLESS the readOnly bit is set.
1216 // This uses the virtual "=" operator, which should do the right thing.
1217 else if (!f->isReadOnly()) {
1218 // Disable notification since we already did it
1219 SbBool notifySave = f->flags.notifyEnabled;
1220 f->flags.notifyEnabled = FALSE;
1221
1222 // Copy value
1223 f->copyFrom(*auditorInfo->connection.field);
1224
1225 // Reenable notification
1226 f->flags.notifyEnabled = notifySave;
1227 }
1228 }
1229
1230 ////////////////////////////////////////////////////////////////////////
1231 //
1232 // Description:
1233 // Creates a converter engine to convert from the given field
1234 // type to the type of this field. Returns NULL on error.
1235 //
1236 // Use: private
1237
1238 SoFieldConverter *
1239 SoField::createConverter(const SoType &fromFieldType) const
1240 //
1241 ////////////////////////////////////////////////////////////////////////
1242 {
1243 SoFieldConverter *converter;
1244
1245 // Find the type of the conversion engine (if any)
1246 SoType converterType = SoDB::getConverter(fromFieldType, getTypeId());
1247
1248 // If no engine exists for these types, don't connect
1249 if (converterType.isBad()) {
1250
1251 #ifdef DEBUG
1252 const char *fromName = fromFieldType.getName().getString();
1253 SoDebugError::post("SoField::connectFrom",
1254 "Connection failed - no conversion supported "
1255 "for types (%s --> %s)",
1256 fromName, getTypeId().getName().getString());
1257 #endif /* DEBUG */
1258 return NULL;
1259 }
1260
1261 // If there is a converter, instantiate it and link it in
1262 converter = (SoFieldConverter *) converterType.createInstance();
1263
1264 #ifdef DEBUG
1265 // If it's an abstract class, error. This should never happen.
1266 if (converter == NULL)
1267 SoDebugError::post("SoField::connectFrom",
1268 "Connection failed - unable to create an "
1269 "instance of \"%s\" converter",
1270 converterType.getName().getString());
1271 #endif /* DEBUG */
1272
1273 return converter;
1274 }
1275
1276 ////////////////////////////////////////////////////////////////////////
1277 //
1278 // Description:
1279 // Reads field from file. Works for ASCII and binary input. The
1280 // name of the field has already been read and is passed here (for
1281 // error messages).
1282 //
1283 // Use: private
1284
1285 SbBool
1286 SoField::read(SoInput *in, const SbName &name)
1287 //
1288 ////////////////////////////////////////////////////////////////////////
1289 {
1290 char c;
1291 SbBool shouldReadConnection = FALSE;
1292 SbBool gotValue = FALSE;
1293
1294 // Turn off notification during reading process. It is turned
1295 // back on and notification is started below:
1296 SbBool wasNotifyEnabled = flags.notifyEnabled;
1297 flags.notifyEnabled = FALSE;
1298
1299 if (in->isBinary()) {
1300 short readFlags;
1301
1302 if (! readValue(in)) {
1303 SoReadError::post(in,
1304 "Couldn't read binary value for field \"%s\"",
1305 name.getString());
1306 flags.notifyEnabled = wasNotifyEnabled;
1307 return FALSE;
1308 }
1309
1310 // Read flags
1311 if (! in->read(readFlags)) {
1312 SoReadError::post(in,
1313 "Couldn't read binary flags for field \"%s\"",
1314 name.getString());
1315 flags.notifyEnabled = wasNotifyEnabled;
1316 return FALSE;
1317 }
1318
1319 // (Don't use setIgnored() to set this, since it would cause
1320 // notification, which we don't want to have happen for trigger
1321 // fields.)
1322 flags.ignored = (readFlags & FIELD_IGNORED) != 0;
1323 shouldReadConnection = (readFlags & FIELD_CONNECTED) != 0;
1324 setDefault((readFlags & FIELD_DEFAULT) != 0);
1325 gotValue = TRUE;
1326 }
1327
1328 // ASCII version...
1329 else {
1330 // Check for ignore flag with no value
1331 if (in->read(c) && c == IGNORE_CHAR) {
1332 setDefault(TRUE);
1333 setIgnored(TRUE);
1334
1335 // Check for connection to engine/field
1336 if (in->read(c) && c == CONNECTION_CHAR)
1337 shouldReadConnection = TRUE;
1338 else
1339 in->putBack(c);
1340 gotValue = FALSE;
1341 }
1342
1343 else {
1344 setIgnored(FALSE);
1345
1346 // If character is connection character, we just use the
1347 // default value and skip the reading-value stuff
1348 if (c != CONNECTION_CHAR) {
1349 in->putBack(c);
1350
1351 if (! readValue(in)) {
1352 SoReadError::post(in,
1353 "Couldn't read value for field \"%s\"",
1354 name.getString());
1355 flags.notifyEnabled = wasNotifyEnabled;
1356 return FALSE;
1357 }
1358
1359 gotValue = TRUE;
1360 setDefault(FALSE);
1361
1362 // Check for ignore flag after value
1363 if (in->read(c) && c == IGNORE_CHAR) {
1364 // (Don't use setIgnored() to set this, since it
1365 // would cause notification, which we don't want
1366 // to have happen for trigger fields.)
1367 flags.ignored = TRUE;
1368
1369 // Get character to check for connection to
1370 // engine/field below.
1371 in->read(c);
1372 }
1373 } else {
1374 gotValue = FALSE;
1375 }
1376
1377 // Check for connection to engine/field
1378 if (c == CONNECTION_CHAR)
1379 shouldReadConnection = TRUE;
1380 else
1381 in->putBack(c);
1382 }
1383 }
1384
1385 // Read connection info if necessary.
1386 if (shouldReadConnection && !readConnection(in)) {
1387 flags.notifyEnabled = wasNotifyEnabled;
1388 return FALSE;
1389 }
1390
1391 // Turn notification back the way it was:
1392 flags.notifyEnabled = wasNotifyEnabled;
1393
1394 // If a value was read (even if there's a connection), call
1395 // valueChanged. Otherwise, just notify.
1396 if (gotValue) {
1397 valueChanged(FALSE);
1398 } else {
1399 startNotify();
1400 }
1401
1402 return TRUE;
1403 }
1404
1405 ////////////////////////////////////////////////////////////////////////
1406 //
1407 // Description:
1408 // Reads field connection from file. Works for ASCII and binary
1409 // input. Returns FALSE on error. The upstream connection is
1410 // evaluated before connecting so we don't end up with dirty fields
1411 // upstream from clean ones (fields are clean once they've been
1412 // read).
1413 //
1414 // Use: private
1415
1416 SbBool
1417 SoField::readConnection(SoInput *in)
1418 //
1419 ////////////////////////////////////////////////////////////////////////
1420 {
1421 SoFieldContainer *connContainer;
1422 SbName fieldName;
1423 SbBool gotChar;
1424 char c;
1425
1426 // The field is connected to either an engine output or to a field
1427 // of an engine or node. First, read the node or engine as a base.
1428 SoBase *baseTemp;
1429 if (! SoBase::read(in, baseTemp, SoFieldContainer::getClassTypeId()))
1430 return FALSE;
1431
1432 if (baseTemp == NULL) {
1433 SoReadError::post(in, "Missing node or engine name in "
1434 "connection specification");
1435 return FALSE;
1436 }
1437
1438 connContainer = (SoFieldContainer *) baseTemp;
1439
1440 // Read the field separator character (ASCII only)
1441 if (! in->isBinary() &&
1442 (! (gotChar = in->read(c)) || c != FIELD_SEP_CHAR)) {
1443
1444 if (gotChar)
1445 SoReadError::post(in, "Expected '%c'; got '%c'",
1446 FIELD_SEP_CHAR, c);
1447 else
1448 SoReadError::post(in, "Expected '%c'; got EOF", FIELD_SEP_CHAR);
1449
1450 return FALSE;
1451 }
1452
1453 // Read the field name or output name
1454 if (! in->read(fieldName, TRUE)) {
1455 SoReadError::post(in,
1456 "Premature end of file before connection was read");
1457 return FALSE;
1458 }
1459
1460 // If the base is a node, make sure the field name is valid and
1461 // then connect to it.
1462 if (connContainer->isOfType(SoNode::getClassTypeId())) {
1463 SoNode *node = (SoNode *) connContainer;
1464 SoField *connField;
1465
1466 connField = node->getField(fieldName);
1467
1468 if (connField == NULL) {
1469 const char *nodeName = node->getTypeId().getName().getString();
1470 SoReadError::post(in, "No such field \"%s\" in node %s",
1471 fieldName.getString(), nodeName);
1472 return FALSE;
1473 }
1474
1475 if (! connectFrom(connField)) {
1476 const char *nodeName = node->getTypeId().getName().getString();
1477 SoReadError::post(in, "Can't connect to field \"%s.%s\"",
1478 nodeName, fieldName.getString());
1479 return FALSE;
1480 }
1481 }
1482
1483 // If the base is an engine, make sure the name is a valid field
1484 // or output and then connect to it
1485 else if (connContainer->isOfType(SoEngine::getClassTypeId())) {
1486 SoEngine *engine = (SoEngine *) connContainer;
1487 SoField *connField;
1488 SoEngineOutput *connOutput;
1489
1490 connField = engine->getField(fieldName);
1491
1492 if (connField == NULL) {
1493
1494 // See if it's an output
1495 connOutput = engine->getOutput(fieldName);
1496
1497 if (connOutput == NULL) {
1498 const char *eName = engine->getTypeId().getName().getString();
1499 SoReadError::post(in, "No such field or output \"%s\" "
1500 "in engine %s",
1501 fieldName.getString(), eName);
1502 return FALSE;
1503 }
1504
1505 if (! connectFrom(connOutput)) {
1506 const char *eName = engine->getTypeId().getName().getString();
1507 SoReadError::post(in,
1508 "Can't connect to engine output \"%s.%s\"",
1509 eName, fieldName.getString());
1510 return FALSE;
1511 }
1512 }
1513 else {
1514 if (! connectFrom(connField)) {
1515 const char *eName = engine->getTypeId().getName().getString();
1516 SoReadError::post(in, "Can't connect to field \"%s.%s\"",
1517 eName, fieldName.getString());
1518 return FALSE;
1519 }
1520 }
1521 }
1522
1523 // If it is a global field...
1524 else if (connContainer->isOfType(SoGlobalField::getClassTypeId())) {
1525
1526 SoGlobalField *gf = (SoGlobalField *) connContainer;
1527 SoField *connField = gf->getMyField();
1528
1529 // Make sure the name in the input is the name of the global field
1530 if (fieldName != gf->getName()) {
1531 const char *globalName = gf->getName().getString();
1532 SoReadError::post(in, "Wrong field name (\"%s\") for global "
1533 "field \"%s\"",
1534 fieldName.getString(), globalName);
1535 return FALSE;
1536 }
1537
1538 if (! connectFrom(connField)) {
1539 SoReadError::post(in, "Can't connect to global field \"%s\"",
1540 gf->getName().getString());
1541 return FALSE;
1542 }
1543 }
1544
1545 // If it's not a node, engine or global field, problem
1546 else {
1547 SoReadError::post(in, "Trying to connect to a %s",
1548 connContainer->getTypeId().getName().getString());
1549 return FALSE;
1550 }
1551
1552 return TRUE;
1553 }
1554
1555 ////////////////////////////////////////////////////////////////////////
1556 //
1557 // Description:
1558 // Writes field to file. Works for ASCII and binary output. May be
1559 // called for either stage of writing.
1560 //
1561 // Use: internal
1562
1563 void
1564 SoField::write(SoOutput *out, const SbName &name) const
1565 //
1566 ////////////////////////////////////////////////////////////////////////
1567 {
1568 if (out->getStage() == SoOutput::COUNT_REFS)
1569 countWriteRefs(out);
1570
1571 else {
1572 // Actually write the field
1573 evaluate();
1574
1575 SbBool writeConn = isConnected() && isConnectionEnabled();
1576
1577 // We should write a field-to-field connection if one exists
1578 // and the container at the other end should be written
1579 if (writeConn && isConnectedFromField()) {
1580 SoField *field;
1581 getConnectedField(field);
1582 SoFieldContainer *container = field->getContainer();
1583 if (container == NULL || ! container->shouldWrite())
1584 writeConn = FALSE;
1585 }
1586
1587 if (out->isBinary()) {
1588 short flags = 0;
1589
1590 out->write(name.getString());
1591 writeValue(out);
1592
1593 if (isIgnored())
1594 flags |= FIELD_IGNORED;
1595 if (writeConn)
1596 flags |= FIELD_CONNECTED;
1597 if (isDefault())
1598 flags |= FIELD_DEFAULT;
1599
1600 out->write(flags);
1601
1602 // Recurse on connected stuff if necessary
1603 if (writeConn)
1604 writeConnection(out);
1605 }
1606
1607 // Only write out IF connected, not default, or ignored:
1608 else if (writeConn || !isDefault() || isIgnored()) {
1609 out->indent();
1610 out->write(name.getString());
1611 out->write(out->isCompact() ? ' ' : '\t');
1612
1613 if (! isDefault())
1614 writeValue(out);
1615
1616 if (isIgnored()) {
1617 if (! isDefault())
1618 out->write(' ');
1619 out->write(IGNORE_CHAR);
1620 }
1621
1622 // Recurse on connected stuff if necessary
1623 if (writeConn)
1624 writeConnection(out);
1625
1626 // Annotate if necessary
1627 if (out->getAnnotation() & SoOutput::ADDRESSES) {
1628 char buf[100];
1629 sprintf(buf, " # %p", this);
1630 out->write(buf);
1631 }
1632 out->write(out->isCompact() ? ' ' : '\n');
1633 }
1634 }
1635 }
1636
1637 ////////////////////////////////////////////////////////////////////////
1638 //
1639 // Description:
1640 // Counts write-references on field to prepare for writing.
1641 //
1642 // Use: internal
1643
1644 void
1645 SoField::countWriteRefs(SoOutput *out) const
1646 //
1647 ////////////////////////////////////////////////////////////////////////
1648 {
1649 if (! isConnected())
1650 return;
1651
1652 // If we are connected from an engine output, add a reference
1653 // to that engine
1654 if (flags.fromEngine || flags.converted) {
1655 // We can't use getConnectedEngine() here because we want to
1656 // make sure we get the field converter if we're connected to one
1657 SoEngine *container =
1658 auditorInfo->connection.engineOutput->getContainer();
1659 if (container != NULL)
1660 container->addWriteReference(out);
1661 }
1662
1663 // If we are connected from another field, add a reference to
1664 // the field's container, but mark it as having come from a
1665 // field-to-field connection
1666 else {
1667 SoField *field;
1668 getConnectedField(field);
1669 SoFieldContainer *container = field->getContainer();
1670 if (container != NULL)
1671 container->addWriteReference(out, TRUE);
1672 }
1673 }
1674
1675 ////////////////////////////////////////////////////////////////////////
1676 //
1677 // Description:
1678 // Writes field connection to file. Works for ASCII and binary
1679 // output. This is called only for the last stage of writing.
1680 //
1681 // Use: private
1682
1683 void
1684 SoField::writeConnection(SoOutput *out) const
1685 //
1686 ////////////////////////////////////////////////////////////////////////
1687 {
1688 SoFieldContainer *container;
1689 SbName fieldName;
1690
1691 // Write out connection character (ASCII only)
1692 if (! out->isBinary()) {
1693 out->write(' ');
1694 out->write(CONNECTION_CHAR);
1695 if (out->getAnnotation()&SoOutput::ADDRESSES) {
1696 char buf[100];
1697 sprintf(buf, " # %p", this);
1698 out->write(buf);
1699 }
1700 out->write('\n');
1701 }
1702
1703 // Base will be indented a little more
1704 out->incrementIndent();
1705
1706 getConnectionInfo(container, fieldName);
1707
1708 // Recurse on container (node, engine, or global field)
1709 container->writeInstance(out);
1710
1711 // Write out separator character (ASCII only)
1712 if (! out->isBinary()) {
1713 out->indent();
1714 out->write(FIELD_SEP_CHAR);
1715 out->write(' ');
1716 }
1717
1718 out->write(fieldName.getString());
1719
1720 if (out->getAnnotation()&SoOutput::ADDRESSES) {
1721 char buf[100];
1722 void *ptr;
1723
1724 if (flags.fromEngine) {
1725 SoEngineOutput *eo;
1726 getConnectedEngine(eo);
1727 ptr = eo;
1728 }
1729 else {
1730 SoField *f;
1731 getConnectedField(f);
1732 ptr = f;
1733 }
1734 sprintf(buf, " # %p", ptr);
1735 out->write(buf);
1736 }
1737
1738 if (! out->isBinary())
1739 out->write('\n');
1740
1741 // Base was indented
1742 out->decrementIndent();
1743 }
1744
1745 ////////////////////////////////////////////////////////////////////////
1746 //
1747 // Description:
1748 // Utility function that returns converter through which this field
1749 // is connected.
1750 //
1751 // Use: private
1752
1753 SoFieldConverter *
1754 SoField::getConverter() const
1755 {
1756 return (SoFieldConverter *)
1757 auditorInfo->connection.engineOutput->getContainer();
1758 }
1759
1760 ////////////////////////////////////////////////////////////////////////
1761 //
1762 // Description:
1763 // This is a utility function that determines what a field is
1764 // connected from. Transparently skips over field converter engines,
1765 // so it reconstructs the original connection that was specified.
1766 //
1767 // Use: private
1768
1769 void
1770 SoField::getConnectionInfo(SoFieldContainer *&container,
1771 SbName &fieldName) const
1772 {
1773 // If it's connected through a converter, recurse
1774 if (flags.converted)
1775 getConverter()->getConnectedInput()->getConnectionInfo(container,
1776 fieldName);
1777
1778 // Field-to-field connection?
1779 else if (! flags.fromEngine) {
1780 container = auditorInfo->connection.field->getContainer();
1781 container->getFieldName(auditorInfo->connection.field, fieldName);
1782 return;
1783 }
1784
1785 // It's an engine.
1786 else {
1787 container = auditorInfo->connection.engineOutput->getContainer();
1788 ((SoEngine *) container)->getOutputName(
1789 auditorInfo->connection.engineOutput, fieldName);
1790 }
1791 }
1792
1793 ////////////////////////////////////////////////////////////////////////
1794 //
1795 // Description:
1796 // This is a callback function passed to SoOutput::setBuffer() by
1797 // SoField::get(). It is used to reallocate the buffer in which the
1798 // returned field info string is stored. Since we need to keep
1799 // track of the pointer to the buffer and its size, we can't just
1800 // call realloc() instead.
1801 //
1802 // Use: private, static
1803
1804 void *
1805 SoField::reallocFieldBuf(void *ptr, size_t newSize)
1806 //
1807 ////////////////////////////////////////////////////////////////////////
1808 {
1809 fieldBuf = (char *) realloc(ptr, newSize);
1810 fieldBufSize = newSize;
1811
1812 return fieldBuf;
1813 }
1814
1815 //////////////////////////////////////////////////////////////////////////////
1816 //
1817 // Class: SoSField
1818 //
1819 // Single-valued fields
1820 //
1821 //////////////////////////////////////////////////////////////////////////////
1822
1823 SoType SoSField::classTypeId;
1824
1825 ////////////////////////////////////////////////////////////////////////
1826 //
1827 // Description:
1828 // Initializes the SoSField class.
1829 //
1830 // Use: internal
1831
1832 void
1833 SoSField::initClass()
1834 //
1835 ////////////////////////////////////////////////////////////////////////
1836 {
1837 SO__FIELD_INIT_CLASS(SoSField, "SField", SoField);
1838 }
1839
1840 ////////////////////////////////////////////////////////////////////////
1841 //
1842 // Description:
1843 // Destructor.
1844 //
1845 // Use: public
1846
1847 SoSField::~SoSField()
1848 //
1849 ////////////////////////////////////////////////////////////////////////
1850 {
1851 }
1852
1853 ////////////////////////////////////////////////////////////////////////
1854 //
1855 // Description:
1856 // Use by SoType::createInstance; in this case, return NULL, since
1857 // this is an abstract type that has no instances...
1858 //
1859 // Use: public
1860
1861 void *
1862 SoSField::createInstance()
1863 //
1864 ////////////////////////////////////////////////////////////////////////
1865 {
1866 return NULL;
1867 }
1868
1869 ////////////////////////////////////////////////////////////////////////
1870 //
1871 // Description:
1872 // Constructor - keeps subclass constructors from being too complex
1873 // for poor old C++.
1874 //
1875 // Use: protected
1876
1877 SoSField::SoSField()
1878 //
1879 ////////////////////////////////////////////////////////////////////////
1880 {
1881 }
1882
1883 //////////////////////////////////////////////////////////////////////////////
1884 //
1885 // Class: SoMField
1886 //
1887 // Multiple-valued fields
1888 //
1889 //////////////////////////////////////////////////////////////////////////////
1890
1891 char *SoMField::fieldBuf; // Used by SoMField::get1()
1892 int SoMField::fieldBufSize; // Used by SoMField::get1()
1893 SoType SoMField::classTypeId;
1894
1895 ////////////////////////////////////////////////////////////////////////
1896 //
1897 // Description:
1898 // Initializes the SoMField class.
1899 //
1900 // Use: internal
1901
1902 void
1903 SoMField::initClass()
1904 //
1905 ////////////////////////////////////////////////////////////////////////
1906 {
1907 SO__FIELD_INIT_CLASS(SoMField, "MField", SoField);
1908 }
1909
1910 ////////////////////////////////////////////////////////////////////////
1911 //
1912 // Description:
1913 // Destructor.
1914 //
1915 // Use: public
1916
1917 SoMField::~SoMField()
1918 //
1919 ////////////////////////////////////////////////////////////////////////
1920 {
1921 }
1922
1923 ////////////////////////////////////////////////////////////////////////
1924 //
1925 // Description:
1926 // Use by SoType::createInstance; in this case, return NULL, since
1927 // this is an abstract type that has no instances...
1928 //
1929 // Use: public
1930
1931 void *
1932 SoMField::createInstance()
1933 //
1934 ////////////////////////////////////////////////////////////////////////
1935 {
1936 return NULL;
1937 }
1938
1939 ////////////////////////////////////////////////////////////////////////
1940 //
1941 // Description:
1942 // Constructor
1943 //
1944 // Use: protected
1945
1946 SoMField::SoMField()
1947 //
1948 ////////////////////////////////////////////////////////////////////////
1949 {
1950 maxNum = num = 0;
1951 }
1952
1953 ////////////////////////////////////////////////////////////////////////
1954 //
1955 // Description:
1956 // Inserts or deletes values to adjust to the given size
1957 //
1958 // Use: public
1959
1960 void
1961 SoMField::setNum(int n)
1962 {
1963 if (n > num)
1964 insertSpace(num, n-num);
1965 else if (n < num)
1966 deleteValues(n);
1967 }
1968
1969
1970 ////////////////////////////////////////////////////////////////////////
1971 //
1972 // Description:
1973 // Deletes num values starting at index start. A num of -1 (the default)
1974 // means delete all values after start, inclusive.
1975 //
1976 // Use: public
1977
1978 void
1979 SoMField::deleteValues(int start, // Starting index
1980 int numToDelete) // Number of values to delete
1981 //
1982 ////////////////////////////////////////////////////////////////////////
1983 {
1984 int lastToCopy, i;
1985
1986 if (numToDelete < 0)
1987 numToDelete = getNum() - start;
1988
1989 // Special case of deleting all values
1990 if (numToDelete == getNum())
1991 deleteAllValues();
1992
1993 else {
1994 // Copy from the end of the array to the middle
1995 lastToCopy = (getNum() - 1) - numToDelete;
1996 for (i = start; i <= lastToCopy; i++)
1997 copyValue(i, i + numToDelete);
1998
1999 // Truncate the array
2000 makeRoom(getNum() - numToDelete);
2001 }
2002
2003 // The field value has changed...
2004 valueChanged();
2005 }
2006
2007 ////////////////////////////////////////////////////////////////////////
2008 //
2009 // Description:
2010 // Inserts space for num values starting at start. The initial
2011 // values in the new space are undefined.
2012 //
2013 // Use: public
2014
2015 void
2016 SoMField::insertSpace(int start, // Starting index
2017 int numToInsert) // Number of spaces to insert
2018 //
2019 ////////////////////////////////////////////////////////////////////////
2020 {
2021 // Expand the array
2022 makeRoom(getNum() + numToInsert);
2023
2024 // Copy stuff out of the inserted area to later in the array
2025 for (int i = num - 1; i >= start + numToInsert; --i)
2026 copyValue(i, i - numToInsert);
2027
2028 // The field value has changed...
2029 valueChanged();
2030 }
2031
2032 ////////////////////////////////////////////////////////////////////////
2033 //
2034 // Description:
2035 // This is equivalent to the SoField::set() method, but operates on
2036 // only the value given by the index.
2037 //
2038 // Use: public
2039
2040 SbBool
2041 SoMField::set1(int index, const char *valueString)
2042 //
2043 ////////////////////////////////////////////////////////////////////////
2044 {
2045 SoInput in;
2046 in.setBuffer((void *) valueString, strlen(valueString));
2047
2048 if (read1Value(&in, index)) {
2049
2050 // We have to do this here because read1Value() doesn't
2051 // indicate that values have changed, since it's usually used
2052 // in a larger reading context.
2053 valueChanged();
2054
2055 return TRUE;
2056 }
2057 return FALSE;
2058 }
2059
2060 ////////////////////////////////////////////////////////////////////////
2061 //
2062 // Description:
2063 // This is equivalent to the SoField::get() method, but operates on
2064 // only the value given by the index.
2065 //
2066 // Use: public
2067
2068 void
2069 SoMField::get1(int index, SbString &valueString)
2070 //
2071 ////////////////////////////////////////////////////////////////////////
2072 {
2073 SoOutput out;
2074
2075 // Make sure the field value is up to date
2076 evaluate();
2077
2078 // Prepare a character buffer and SoOutput for writing field strings
2079 if (fieldBufSize == 0) {
2080 fieldBufSize = 1028;
2081 fieldBuf = (char *) malloc((unsigned) fieldBufSize);
2082 }
2083
2084 // Set up output into a string buffer
2085 out.setBuffer((void *) fieldBuf, fieldBufSize, &SoMField::reallocFieldBuf);
2086
2087 // Make sure that the file header and lots of white space will NOT
2088 // be written into the string
2089 out.setCompact(TRUE);
2090
2091 // Write the data
2092 write1Value(&out, index);
2093
2094 // Make sure the string is terminated
2095 out.reset();
2096
2097 // Store the result in the passed SbString
2098 valueString = fieldBuf;
2099 }
2100
2101 ////////////////////////////////////////////////////////////////////////
2102 //
2103 // Description:
2104 // This makes sure there is enough memory allocated to hold
2105 // "newNum" values. It reallocates the values if necessary.
2106 //
2107 // Use: protected
2108
2109 void
2110 SoMField::makeRoom(int newNum) // New number of values
2111 //
2112 ////////////////////////////////////////////////////////////////////////
2113 {
2114 // Allocate room to hold all values if necessary
2115 if (newNum != num) {
2116 allocValues(newNum);
2117 }
2118 }
2119
2120 ////////////////////////////////////////////////////////////////////////
2121 //
2122 // Description:
2123 // Reads all field values from file. Works for ASCII and binary
2124 // output. Binary values are read as a chunk.
2125 //
2126 // Use: private
2127
2128 SbBool
2129 SoMField::readValue(SoInput *in)
2130 //
2131 ////////////////////////////////////////////////////////////////////////
2132 {
2133 if (in->isBinary()) {
2134 int numToRead;
2135
2136 // Read number of values
2137 if (! in->read(numToRead)) {
2138 SoReadError::post(in, "Couldn't read number of binary values "
2139 "in multiple-value field");
2140 return FALSE;
2141 }
2142
2143 // Make space for values; also sets number of values
2144 makeRoom(numToRead);
2145
2146 // Read values
2147 if (! readBinaryValues(in, numToRead))
2148 return FALSE;
2149 }
2150
2151 else {
2152 char c;
2153 int curIndex = 0;
2154
2155 // Check for multiple field values
2156 if (in->read(c) && c == OPEN_BRACE_CHAR) {
2157
2158 // Check for no values: just an open and close brace
2159 if (in->read(c) && c == CLOSE_BRACE_CHAR)
2160 ; // Do nothing now
2161
2162 else {
2163 in->putBack(c);
2164
2165 while (TRUE) {
2166
2167 // Make some room at end if necessary
2168 if (curIndex >= getNum())
2169 makeRoom(getNum() + VALUE_CHUNK_SIZE);
2170
2171 if (! read1Value(in, curIndex++) || ! in->read(c)) {
2172 SoReadError::post(in,
2173 "Couldn't read value %d of field",
2174 curIndex);
2175 return FALSE;
2176 }
2177
2178 if (c == VALUE_SEPARATOR_CHAR) {
2179
2180 // See if this is a trailing separator (right before
2181 // the closing brace). This is legal, but ignored.
2182
2183 if (in->read(c)) {
2184 if (c == CLOSE_BRACE_CHAR)
2185 break;
2186 else
2187 in->putBack(c);
2188 }
2189 }
2190
2191 else if (c == CLOSE_BRACE_CHAR)
2192 break;
2193
2194 else {
2195 SoReadError::post(in,
2196 "Expected '%c' or '%c' but got "
2197 "'%c' while reading value %d",
2198 VALUE_SEPARATOR_CHAR,
2199 CLOSE_BRACE_CHAR, c,
2200 curIndex);
2201 return FALSE;
2202 }
2203 }
2204 }
2205
2206 // If extra space left over, nuke it
2207 if (curIndex < getNum())
2208 makeRoom(curIndex);
2209 }
2210
2211 else {
2212 // Try reading 1 value
2213 in->putBack(c);
2214 makeRoom(1);
2215 if (! read1Value(in, 0))
2216 return FALSE;
2217 }
2218 }
2219
2220 return TRUE;
2221 }
2222
2223 ////////////////////////////////////////////////////////////////////////
2224 //
2225 // Description:
2226 // Writes all values of field to file. Works for ASCII and binary
2227 // output. Binary output writes out the number of values and then
2228 // the values as one big chunk o'memory.
2229 //
2230 // Use: private
2231
2232 void
2233 SoMField::writeValue(SoOutput *out) const
2234 //
2235 ////////////////////////////////////////////////////////////////////////
2236 {
2237 int i;
2238
2239 if (out->isBinary()) {
2240 out->write(num);
2241 writeBinaryValues(out);
2242 }
2243
2244 else {
2245 if (num == 1)
2246 write1Value(out, 0);
2247
2248 else {
2249 int numOnLine = 0, maxOnLine = getNumValuesPerLine();
2250
2251 out->write(OPEN_BRACE_CHAR);
2252 out->write(' ');
2253
2254 out->incrementIndent(4);
2255
2256 for (i = 0; i < num; i++) {
2257
2258 write1Value(out, i);
2259
2260 if (i < num - 1) {
2261 out->write(VALUE_SEPARATOR_CHAR);
2262 if (++numOnLine == maxOnLine && ! out->isCompact()) {
2263 out->write('\n');
2264 out->indent();
2265 out->write(' ');
2266 out->write(' ');
2267 numOnLine = 0;
2268 }
2269 else
2270 out->write(' ');
2271 }
2272 }
2273
2274 out->write(' ');
2275 out->write(CLOSE_BRACE_CHAR);
2276
2277 out->decrementIndent(4);
2278 }
2279 }
2280 }
2281
2282 ////////////////////////////////////////////////////////////////////////
2283 //
2284 // Description:
2285 // Writes array of binary values to file as one chunk.
2286 //
2287 // Use: private
2288
2289 void
2290 SoMField::writeBinaryValues(SoOutput *out) const // Defines writing action
2291
2292 //
2293 ////////////////////////////////////////////////////////////////////////
2294 {
2295 int i;
2296
2297 for (i = 0; i < num; i++)
2298 write1Value(out, i);
2299 }
2300
2301 ////////////////////////////////////////////////////////////////////////
2302 //
2303 // Description:
2304 // Reads array of binary values from file as one chunk.
2305 //
2306 // Use: private
2307
2308 SbBool
2309 SoMField::readBinaryValues(SoInput *in, // Reading specification
2310 int numToRead) // Number of values to read
2311 //
2312 ////////////////////////////////////////////////////////////////////////
2313 {
2314 int i;
2315
2316 for (i = 0; i < numToRead; i++)
2317 if (! read1Value(in, i))
2318 return FALSE;
2319
2320 return TRUE;
2321 }
2322
2323 ////////////////////////////////////////////////////////////////////////
2324 //
2325 // Description:
2326 // Returns number of ASCII values to write per output line. This
2327 // defines the default to be 1, just to play it safe.
2328 //
2329 // Use: private
2330
2331 int
2332 SoMField::getNumValuesPerLine() const
2333 //
2334 ////////////////////////////////////////////////////////////////////////
2335 {
2336 return 1;
2337 }
2338
2339 ////////////////////////////////////////////////////////////////////////
2340 //
2341 // Description:
2342 // This is a callback function passed to SoOutput::setBuffer() by
2343 // SoMField::get1(). It is used to reallocate the buffer in which the
2344 // returned field info string is stored. Since we need to keep
2345 // track of the pointer to the buffer and its size, we can't just
2346 // call realloc() instead.
2347 //
2348 // Use: private, static
2349
2350 void *
2351 SoMField::reallocFieldBuf(void *ptr, size_t newSize)
2352 //
2353 ////////////////////////////////////////////////////////////////////////
2354 {
2355 fieldBuf = (char *) realloc(ptr, newSize);
2356 fieldBufSize = newSize;
2357
2358 return fieldBuf;
2359 }
2360
2361
2362