Debian Codegraph @

lib/database/src/so/sensors/SoFieldSensor.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 | SoFieldSensor
47 |
48 | Author(s) : Paul Strauss
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/fields/SoField.h>
55 #include <Inventor/fields/SoFieldContainer.h>
56 #include <Inventor/sensors/SoFieldSensor.h>
57
58 ////////////////////////////////////////////////////////////////////////
59 //
60 // Description:
61 // Constructor
62 //
63 // Use: public
64
65 SoFieldSensor::SoFieldSensor() : SoDataSensor()
66 //
67 ////////////////////////////////////////////////////////////////////////
68 {
69 field = NULL;
70 }
71
72 ////////////////////////////////////////////////////////////////////////
73 //
74 // Description:
75 // Constructor with function and data.
76 //
77 // Use: public
78
79 SoFieldSensor::SoFieldSensor(SoSensorCB *func, void *data) :
80 SoDataSensor(func, data)
81 //
82 ////////////////////////////////////////////////////////////////////////
83 {
84 field = NULL;
85 }
86
87 ////////////////////////////////////////////////////////////////////////
88 //
89 // Description:
90 // Destructor
91 //
92 // Use: protected
93
94 SoFieldSensor::~SoFieldSensor()
95 //
96 ////////////////////////////////////////////////////////////////////////
97 {
98 detach();
99 }
100
101 ////////////////////////////////////////////////////////////////////////
102 //
103 // Description:
104 // Attaches the sensor to the given field. Will not attach if the
105 // field is not contained in a node or function.
106 //
107 // Use: public
108
109 void
110 SoFieldSensor::attach(SoField *fieldToAttachTo)
111 //
112 ////////////////////////////////////////////////////////////////////////
113 {
114 if (field != NULL)
115 detach();
116
117 field = fieldToAttachTo;
118
119 // Add auditor to field so that we get notified when field changes
120 field->addAuditor(this, SoNotRec::SENSOR);
121 field->evaluate();
122 }
123
124 ////////////////////////////////////////////////////////////////////////
125 //
126 // Description:
127 // Detaches the sensor if it is attached to a field.
128 //
129 // Use: public
130
131 void
132 SoFieldSensor::detach()
133 //
134 ////////////////////////////////////////////////////////////////////////
135 {
136 if (field != NULL) {
137 field->removeAuditor(this, SoNotRec::SENSOR);
138 field = NULL;
139
140 // If we are scheduled, there's no point leaving it scheduled,
141 // since it's not attached any more to whatever caused it to
142 // become scheduled.
143 unschedule();
144 }
145 }
146
147 ////////////////////////////////////////////////////////////////////////
148 //
149 // Description:
150 // This is called by the field's container when it (the container)
151 // is about to be deleted.
152 //
153 // Use: private
154
155 void
156 SoFieldSensor::dyingReference()
157 //
158 ////////////////////////////////////////////////////////////////////////
159 {
160 // We want to detach the sensor if it's still attached to the
161 // dying field's container after we invoke the callback. If the
162 // callback attaches to something else, we don't want to detach
163 // it. So we'll compare the field containers before and after the
164 // callback is invoked and detach only if it's the same one.
165
166 SoFieldContainer *dyingFieldCont = getAttachedField()->getContainer();
167
168 invokeDeleteCallback();
169
170 if (getAttachedField() != NULL &&
171 getAttachedField()->getContainer() == dyingFieldCont)
172 detach();
173 }
174
175 ////////////////////////////////////////////////////////////////////////
176 //
177 // Description:
178 // Propagates modification notification through an instance. This
179 // checks to see if the field that triggered notification matches
180 // the field we are attached to.
181 //
182 // Use: private, virtual
183
184 void
185 SoFieldSensor::notify(SoNotList *list)
186 //
187 ////////////////////////////////////////////////////////////////////////
188 {
189 // See if the field that caused the notification (may be NULL) is
190 // the same as the field we are attached to.
191 if (list->getLastField() == field) {
192 SoDataSensor::notify(list);
193 }
194 }
195
196 ////////////////////////////////////////////////////////////////////////
197 //
198 // Description:
199 // Override trigger to evaluate the field we're connected to, just
200 // in case the trigger method doesn't get the value.
201 //
202 // Use: internal
203
204 void
205 SoFieldSensor::trigger()
206 //
207 ////////////////////////////////////////////////////////////////////////
208 {
209 field->evaluate();
210 SoDataSensor::trigger();
211 }