Debian Codegraph @

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

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoMFEnum
47 |
48 | Author(s) : Paul S. Strauss
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54
55 #include <Inventor/errors/SoDebugError.h>
56 #include <Inventor/errors/SoReadError.h>
57 #include <Inventor/fields/SoMFEnum.h>
58 #include <cstdlib>
59
60 //////////////////////////////////////////////////////////////////////////////
61 //
62 // SoMFEnum class
63 //
64 //////////////////////////////////////////////////////////////////////////////
65
66 // Use standard definitions of methods, except for constructor/destructor
67 SO_MFIELD_REQUIRED_SOURCE(SoMFEnum);
68 SO_MFIELD_VALUE_SOURCE(SoMFEnum, int, int);
69 SO_MFIELD_MALLOC_SOURCE(SoMFEnum, int);
70
71 ////////////////////////////////////////////////////////////////////////
72 //
73 // Description:
74 // Constructor.
75 //
76 // Use: extender
77
78 SoMFEnum::SoMFEnum()
79 //
80 ////////////////////////////////////////////////////////////////////////
81 {
82 values = NULL;
83 enumValues = NULL;
84 enumNames = NULL;
85 numEnums = 0;
86 legalValuesSet = FALSE;
87 }
88
89 ////////////////////////////////////////////////////////////////////////
90 //
91 // Description:
92 // Destructor.
93 //
94 // Use: extender
95
96 SoMFEnum::~SoMFEnum()
97 //
98 ////////////////////////////////////////////////////////////////////////
99 {
100 deleteAllValues();
101 if (!legalValuesSet && numEnums > 0) {
102 delete[] enumValues;
103 delete[] enumNames;
104 }
105 }
106
107 ////////////////////////////////////////////////////////////////////////
108 //
109 // Description:
110 // Sets value from given SbName, which must contain the name of a
111 // valid enumerator.
112 //
113 // Use: public
114
115 void
116 SoMFEnum::setValue(const SbName &name)
117 //
118 ////////////////////////////////////////////////////////////////////////
119 {
120 int enumVal;
121
122 if (findEnumValue(name, enumVal))
123 setValue(enumVal);
124
125 #ifdef DEBUG
126 else
127 SoDebugError::post("SoMFEnum::setValue",
128 "No enum for name \"%s\"", name.getString());
129 #endif /* DEBUG */
130 }
131
132 ////////////////////////////////////////////////////////////////////////
133 //
134 // Description:
135 // Sets indexed value from given SbName, which must contain the
136 // name of a valid enumerator.
137 //
138 // Use: public
139
140 void
141 SoMFEnum::set1Value(int index, const SbName &name)
142 //
143 ////////////////////////////////////////////////////////////////////////
144 {
145 int enumVal;
146
147 if (findEnumValue(name, enumVal))
148 set1Value(index, enumVal);
149
150 #ifdef DEBUG
151 else
152 SoDebugError::post("SoMFEnum::set1Value",
153 "No enum for name \"%s\"", name.getString());
154 #endif /* DEBUG */
155 }
156
157 ////////////////////////////////////////////////////////////////////////
158 //
159 // Description:
160 // Called by nodes to set legal enum names and values
161 //
162 // Use: extender public
163
164 void
165 SoMFEnum::setEnums(int num, int *vals, SbName *names)
166
167 //
168 ////////////////////////////////////////////////////////////////////////
169 {
170 #ifdef DEBUG
171 if (legalValuesSet == TRUE) {
172 SoDebugError::post("SoMFEnum::setEnums",
173 "setEnums called twice");
174 }
175 #endif
176
177 legalValuesSet = TRUE;
178 numEnums = num;
179 enumValues = vals;
180 enumNames = names;
181 }
182
183 ////////////////////////////////////////////////////////////////////////
184 //
185 // Description:
186 // Looks up enum name, returns value. Returns FALSE if not found.
187 //
188 // Use: protected
189
190 SbBool
191 SoMFEnum::findEnumValue(const SbName &name, int &val)
192 //
193 ////////////////////////////////////////////////////////////////////////
194 {
195 int i;
196
197 // Look through names table for one that matches
198 for (i = 0; i < numEnums; i++) {
199 if (name == enumNames[i]) {
200 val = enumValues[i];
201 return TRUE;
202 }
203 }
204
205 if (!legalValuesSet) {
206 // Must be part of an unknown node, add name, value:
207 int *oldValues = enumValues;
208 SbName *oldNames = enumNames;
209 enumValues = new int[numEnums+1];
210 enumNames = new SbName[numEnums+1];
211 if (numEnums != 0) {
212 for (i = 0; i < numEnums; i++) {
213 enumValues[i] = oldValues[i];
214 enumNames[i] = oldNames[i];
215 }
216 delete[] oldValues;
217 delete[] oldNames;
218 }
219 val = numEnums;
220 enumValues[numEnums] = numEnums;
221 enumNames[numEnums] = name;
222 ++numEnums;
223 return TRUE;
224 }
225
226 return FALSE;
227 }
228
229 ////////////////////////////////////////////////////////////////////////
230 //
231 // Description:
232 // Looks up enum value, returns pointer to name. Returns FALSE if
233 // not found.
234 //
235 // Use: protected
236
237 SbBool
238 SoMFEnum::findEnumName(int val, const SbName *&name) const
239 //
240 ////////////////////////////////////////////////////////////////////////
241 {
242 int i;
243
244 // Find string corresponding to given value
245 for (i = 0; i < numEnums; i++) {
246 if (val == enumValues[i]) {
247 name = &enumNames[i];
248 return TRUE;
249 }
250 }
251
252 return FALSE;
253 }
254
255 ////////////////////////////////////////////////////////////////////////
256 //
257 // Description:
258 // Reads one (indexed) value from file. Returns FALSE on error.
259 //
260 // Use: private
261
262 SbBool
263 SoMFEnum::read1Value(SoInput *in, int index)
264 //
265 ////////////////////////////////////////////////////////////////////////
266 {
267 SbName n;
268
269 // Read mnemonic value as a character string identifier
270 if (! in->read(n, TRUE))
271 return FALSE;
272
273 if (findEnumValue(n, values[index]))
274 return TRUE;
275
276 // Not found? Too bad
277 SoReadError::post(in, "Unknown SoMFEnum enumeration value \"%s\"",
278 n.getString());
279 return FALSE;
280 }
281
282 ////////////////////////////////////////////////////////////////////////
283 //
284 // Description:
285 // Writes one (indexed) value to file.
286 //
287 // Use: private
288
289 void
290 SoMFEnum::write1Value(SoOutput *out, int index) const
291 //
292 ////////////////////////////////////////////////////////////////////////
293 {
294 const SbName *n;
295
296 #ifdef DEBUG
297 if (enumValues == NULL) {
298 SoDebugError::post("SoMFEnum::write1Value",
299 "Enum values were never initialized");
300 return;
301 }
302 #endif /* DEBUG */
303
304 // Find and write string corresponding to given value
305 if (findEnumName(values[index], n))
306 out->write(n->getString());
307
308 // Whoops! Something bad was in the field value
309 else
310 SoDebugError::post("SoMFEnum::write1Value",
311 "Illegal value (%d) in field at index %d",
312 values[index], index);
313 }