Debian Codegraph @

lib/database/src/so/upgraders/SoUpgrader.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoUpgrader
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 <assert.h>
55 #include <Inventor/SoInput.h>
56 #include <Inventor/SbDict.h>
57 #include <Inventor/misc/upgraders/SoUpgrader.h>
58
59 SO_NODE_ABSTRACT_SOURCE(SoUpgrader);
60
61 // Static variables:
62 SbDict *SoUpgrader::upgradeDictV1 = NULL;
63 SbDict *SoUpgrader::upgradeDictV2 = NULL;
64
65 ////////////////////////////////////////////////////////////////////////
66 //
67 // Description:
68 // Constructor
69 //
70 // Use: public
71
72 SoUpgrader::SoUpgrader()
73 //
74 ////////////////////////////////////////////////////////////////////////
75 {
76 SO_NODE_CONSTRUCTOR(SoUpgrader);
77
78 isGroup = TRUE;
79 }
80
81 ////////////////////////////////////////////////////////////////////////
82 //
83 // Description:
84 // Destructor
85 //
86 // Use: private
87
88 SoUpgrader::~SoUpgrader()
89 //
90 ////////////////////////////////////////////////////////////////////////
91 {
92 }
93
94 ////////////////////////////////////////////////////////////////////////
95 //
96 // Return either the V1.0 or V2.0 dictionary
97 //
98 SbDict *
99 SoUpgrader::getUpgradeDict(float version)
100 //
101 ////////////////////////////////////////////////////////////////////////
102 {
103 SbDict *upgradeDict = NULL;
104 if (version == 1.0)
105 upgradeDict = upgradeDictV1;
106 else if (version == 2.0)
107 upgradeDict = upgradeDictV2;
108
109 return upgradeDict;
110 }
111
112 ////////////////////////////////////////////////////////////////////////
113 //
114 // Description:
115 // Register a converter with the converter dictionary.
116 //
117 // Use: protected, static
118
119 void
120 SoUpgrader::registerUpgrader(const SoType &type, const SbName &className,
121 float version)
122 //
123 ////////////////////////////////////////////////////////////////////////
124 {
125 const char *str = className.getString();
126
127 #ifdef DEBUG
128 // There are 1.0 and 2.0 upgraders right now...
129 if (version != 1.0 && version != 2.0) {
130 SoDebugError::post("SoUpgrader::register", "Cannot handle "
131 "converters from version %f, can only "
132 "handle converters from version 1.0 or 2.0",
133 version);
134 }
135 #endif
136
137 SbDict *upgradeDict = getUpgradeDict(version);
138 assert(upgradeDict != NULL);
139
140 #ifdef DEBUG
141 void *t;
142 if (upgradeDict->find((unsigned long)str, t) != 0) {
143 SoDebugError::post("SoUpgrader::register", "Upgrader already "
144 "registered for class %s", str);
145 }
146 #endif
147
148 upgradeDict->enter((unsigned long)str, (void *)&type);
149 }
150
151 ////////////////////////////////////////////////////////////////////////
152 //
153 // Description:
154 // Looks in the dictionary to figure out if a converter exists...
155 //
156 // Use: public, static
157
158 SoUpgrader *
159 SoUpgrader::getUpgrader(const SbName &className, float version)
160 //
161 ////////////////////////////////////////////////////////////////////////
162 {
163 // There are 1.0 and 2.0 upgraders right now...
164 if (version != 1.0 && version != 2.0) return NULL;
165
166 // Look in dictionary for converter type:
167 void *t;
168 if (! getUpgradeDict(version)->find((unsigned long) className.getString(), t))
169 return NULL;
170
171 SoType *type = (SoType *) t;
172
173 return (SoUpgrader *)type->createInstance();
174 }
175
176 ////////////////////////////////////////////////////////////////////////
177 //
178 // Description:
179 // Does the grunt work of reading in the old node-- relies on
180 // createNewNode() to actually create a node or scene graph that
181 // can be added to the scene. Returns TRUE if everything was
182 // successful.
183 //
184 // Use: extender, virtual
185
186 SbBool
187 SoUpgrader::upgrade(SoInput *in, const SbName &refName, SoBase *&result)
188 //
189 ////////////////////////////////////////////////////////////////////////
190 {
191 SbBool returnValue = TRUE;
192
193 // Read in fields:
194 const SoFieldData *fieldData = getFieldData();
195 SbBool notBuiltIn; // Not used
196 if (! fieldData->read(in, this, FALSE, notBuiltIn))
197 return FALSE;
198
199 // Create instance:
200 result = createNewNode();
201
202 // Oops, something wrong...
203 if (result == NULL) return FALSE;
204
205 // Add to name dictionary:
206 if (! (!refName))
207 in->addReference(refName, result);
208
209 // And read children, if node being upgraded is derived from group:
210 if (isGroup && result->isOfType(SoGroup::getClassTypeId())) {
211 returnValue = readChildren(in);
212 if (returnValue) {
213
214 SoGroup *g = (SoGroup *)result;
215
216 // Turn off notification on result while we set it up:
217 SbBool notifySave = g->enableNotify(FALSE);
218
219 for (int i = 0; i < getNumChildren(); i++) {
220 g->addChild(getChild(i));
221 }
222
223 // Re-enable notification.
224 g->enableNotify(notifySave);
225 }
226 // No need to remove children, we're about to be unreffed
227 // anyway (by SoBase::readBaseInstance).
228 }
229
230 return returnValue;
231 }