| 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 | | SoGlobalField |
| 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 | ////////////////////////////////////////////////////////////////////////////// |
| 55 | // |
| 56 | // Class: SoGlobalField |
| 57 | // |
| 58 | // There is an instance of this class for every global field |
| 59 | // created. This class also maintains the dictionary associating |
| 60 | // global field names with global field containers. |
| 61 | // |
| 62 | // This entire class is private. |
| 63 | // |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | #include <Inventor/errors/SoDebugError.h> |
| 67 | #include <Inventor/errors/SoReadError.h> |
| 68 | #include "SoGlobalField.h" |
| 69 | |
| 70 | // SbName to SoGlobalField* dictionary. Keyed by the name. |
| 71 | SbDict *SoGlobalField::nameDict; |
| 72 | |
| 73 | // The GlobalFieldContainer type (the name is simply GlobalField) |
| 74 | SoType SoGlobalField::classTypeId; |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | // |
| 78 | // Description: |
| 79 | // Setup type information. Called by SoDB::init() |
| 80 | // |
| 81 | // Use: internal |
| 82 | |
| 83 | void |
| 84 | SoGlobalField::initClass() |
| 85 | // |
| 86 | ////////////////////////////////////////////////////////////////////////////// |
| 87 | { |
| 88 | classTypeId = SoType::createType(SoFieldContainer::getClassTypeId(), |
| 89 | "GlobalField", NULL); |
| 90 | nameDict = new SbDict(20); // Assume small number of global fields |
| 91 | } |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | // |
| 95 | // Description: |
| 96 | // This either creates a new globalField (if name hasn't been found |
| 97 | // before) or returns the globalField with the given name and type. |
| 98 | // It will return NULL if there is an error. "alreadyExists" will be |
| 99 | // set to TRUE if a global field of the same name and type already |
| 100 | // exists. |
| 101 | // |
| 102 | // Use: internal |
| 103 | |
| 104 | SoGlobalField * |
| 105 | SoGlobalField::create(const SbName &name, SoType type, SbBool &alreadyExists) |
| 106 | // |
| 107 | ////////////////////////////////////////////////////////////////////////////// |
| 108 | { |
| 109 | // Look for an existing field of the same name/type: |
| 110 | SoGlobalField *result = SoGlobalField::find(name); |
| 111 | if (result != NULL) { |
| 112 | if (! result->getType().isDerivedFrom(type)) { |
| 113 | const char *resName = result->getName().getString(); |
| 114 | const char *resTypeName = result->getType().getName().getString(); |
| 115 | SoDebugError::post("SoGlobalField::create", |
| 116 | "Global field %s has two types (%s, %s)", |
| 117 | resName, resTypeName, |
| 118 | type.getName().getString()); |
| 119 | return NULL; |
| 120 | } |
| 121 | else { |
| 122 | alreadyExists = TRUE; |
| 123 | return result; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | alreadyExists = FALSE; |
| 128 | |
| 129 | // Make sure field is a proper field type |
| 130 | |
| 131 | if (! type.isDerivedFrom(SoField::getClassTypeId())) { |
| 132 | const char *resName = result->getName().getString(); |
| 133 | SoDebugError::post("SoGlobalField::create", |
| 134 | "(field %s) Type %s is not a field type", |
| 135 | resName, type.getName().getString()); |
| 136 | return NULL; |
| 137 | } |
| 138 | SoField *field = (SoField *)type.createInstance(); |
| 139 | |
| 140 | // Make sure it isn't an abstract field type... |
| 141 | |
| 142 | if (field == NULL) { |
| 143 | const char *typeName = type.getName().getString(); |
| 144 | SoDebugError::post("SoGlobalField::create", |
| 145 | "Cannot create field %s of type %s", |
| 146 | name.getString(), typeName); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | return new SoGlobalField(name, field); |
| 151 | } |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | // |
| 155 | // Description: |
| 156 | // GlobalField constructor. It creates a globalField, given the name |
| 157 | // of the field and a pointer to the field to use. This is private; |
| 158 | // the create() method is the public interface to creating new global |
| 159 | // fields. |
| 160 | // |
| 161 | // Use: private |
| 162 | |
| 163 | SoGlobalField::SoGlobalField(const SbName &name, SoField *field) |
| 164 | // |
| 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | { |
| 167 | typeField.setValue(field->getTypeId().getName()); |
| 168 | value = field; |
| 169 | #ifdef DEBUG |
| 170 | if (field->getContainer()) |
| 171 | SoDebugError::post("SoGlobalField::SoGlobalField", |
| 172 | "Field already has container!"); |
| 173 | |
| 174 | #endif /* DEBUG */ |
| 175 | field->setContainer(this); |
| 176 | field->setDefault(FALSE); |
| 177 | |
| 178 | unsigned long key = (unsigned long)(name.getString()); |
| 179 | #ifdef DEBUG |
| 180 | void *junk; |
| 181 | if (nameDict->find(key, junk) != FALSE) |
| 182 | SoDebugError::post("SoGlobalField::SoGlobalField", |
| 183 | "There is already a global field named %s", |
| 184 | name.getString()); |
| 185 | #endif |
| 186 | nameDict->enter(key, this); |
| 187 | |
| 188 | fieldData = new SoFieldData; |
| 189 | fieldData->addField(this, name.getString(), field); |
| 190 | |
| 191 | ref(); |
| 192 | } |
| 193 | |
| 194 | ////////////////////////////////////////////////////////////////////////////// |
| 195 | // |
| 196 | // Description: |
| 197 | // Destructor. It is responsible for deleting the globalField from |
| 198 | // the dictionary. |
| 199 | // |
| 200 | // Use: private |
| 201 | |
| 202 | SoGlobalField::~SoGlobalField() |
| 203 | // |
| 204 | ////////////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | { |
| 207 | // If fieldData is NULL, don't delete the entry. This happens only |
| 208 | // for the "dummy" instance that is created to read in a real |
| 209 | // instance of a global field. |
| 210 | if (fieldData == NULL) |
| 211 | return; |
| 212 | |
| 213 | unsigned long key = (unsigned long)(getName().getString()); |
| 214 | nameDict->remove(key); |
| 215 | |
| 216 | delete value; |
| 217 | } |
| 218 | |
| 219 | ////////////////////////////////////////////////////////////////////////////// |
| 220 | // |
| 221 | // Description: |
| 222 | // Find a globalField with the given name in the dictionary. Returns |
| 223 | // NULL if it there is none. |
| 224 | // |
| 225 | // Use: internal |
| 226 | |
| 227 | SoGlobalField * |
| 228 | SoGlobalField::find(const SbName &name) |
| 229 | // |
| 230 | ////////////////////////////////////////////////////////////////////////////// |
| 231 | { |
| 232 | unsigned long key = (unsigned long)(name.getString()); |
| 233 | void *result; |
| 234 | if (nameDict->find(key, result) == FALSE) return NULL; |
| 235 | return (SoGlobalField *)result; |
| 236 | } |
| 237 | |
| 238 | ////////////////////////////////////////////////////////////////////////////// |
| 239 | // |
| 240 | // Description: |
| 241 | // Change the name of a global field. |
| 242 | // |
| 243 | // Use: internal |
| 244 | |
| 245 | void |
| 246 | SoGlobalField::changeName(const SbName &newName) |
| 247 | // |
| 248 | ////////////////////////////////////////////////////////////////////////////// |
| 249 | |
| 250 | { |
| 251 | // Remove old entry from dictionary |
| 252 | unsigned long key = (unsigned long)(getName().getString()); |
| 253 | nameDict->remove(key); |
| 254 | |
| 255 | // Create a new fieldData with the correct name for the field: |
| 256 | SoField *field = value; |
| 257 | delete fieldData; |
| 258 | fieldData = new SoFieldData; |
| 259 | fieldData->addField(this, newName.getString(), field); |
| 260 | key = (unsigned long)(getName().getString()); |
| 261 | |
| 262 | // If there is already an entry with the new name... |
| 263 | void *oldGlobalField; |
| 264 | if (nameDict->find(key, oldGlobalField) == TRUE) { |
| 265 | // Delete the old one |
| 266 | ((SoGlobalField *)oldGlobalField)->unref(); |
| 267 | nameDict->remove(key); |
| 268 | } |
| 269 | |
| 270 | // Enter this node under the new name |
| 271 | nameDict->enter(key, this); |
| 272 | } |
| 273 | |
| 274 | ////////////////////////////////////////////////////////////////////////////// |
| 275 | // |
| 276 | // Description: |
| 277 | // This is a virtual function used by the database so notification and |
| 278 | // field to field connections work. The fieldData for globalFields |
| 279 | // contains only the value field; the other are hidden from the |
| 280 | // database and are used only when reading or writing. |
| 281 | // |
| 282 | |
| 283 | const SoFieldData * |
| 284 | SoGlobalField::getFieldData() const |
| 285 | // |
| 286 | ////////////////////////////////////////////////////////////////////////////// |
| 287 | { |
| 288 | return fieldData; |
| 289 | } |
| 290 | |
| 291 | ////////////////////////////////////////////////////////////////////////////// |
| 292 | // |
| 293 | // Description: |
| 294 | // Returns typeid. |
| 295 | // |
| 296 | // Use: internal |
| 297 | // |
| 298 | SoType |
| 299 | SoGlobalField::getTypeId() const |
| 300 | // |
| 301 | ////////////////////////////////////////////////////////////////////////////// |
| 302 | { |
| 303 | return classTypeId; |
| 304 | } |
| 305 | |
| 306 | ////////////////////////////////////////////////////////////////////////////// |
| 307 | // |
| 308 | // Description: |
| 309 | // Write out an instance of a GlobalField. |
| 310 | // Called by SoBase::write() during a writeAction. |
| 311 | // |
| 312 | // Use: internal |
| 313 | |
| 314 | void |
| 315 | SoGlobalField::writeInstance(SoOutput *out) |
| 316 | // |
| 317 | ////////////////////////////////////////////////////////////////////////////// |
| 318 | { |
| 319 | if (! writeHeader(out, FALSE, FALSE)) { |
| 320 | typeField.write(out, "type"); |
| 321 | value->write(out, getName()); |
| 322 | writeFooter(out); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | ////////////////////////////////////////////////////////////////////////////// |
| 327 | // |
| 328 | // Description: |
| 329 | // Special method used by SoBase::read to read in a global field. |
| 330 | // The regular reading method can't be used because there can be |
| 331 | // only one global field with a given name, so SoBase can't just |
| 332 | // immediately create a new instance when it reads the |
| 333 | // "GlobalField {" string from the file. So, it calls this |
| 334 | // routine instead. This returns NULL if there was a read error. |
| 335 | // |
| 336 | // Use: internal, static |
| 337 | |
| 338 | SoGlobalField * |
| 339 | SoGlobalField::read(SoInput *in) |
| 340 | // |
| 341 | ////////////////////////////////////////////////////////////////////////////// |
| 342 | { |
| 343 | // Read in "type SFFloat": |
| 344 | SbName typeName; |
| 345 | if (!in->read(typeName, TRUE) || typeName != SbName("type")) |
| 346 | return NULL; |
| 347 | |
| 348 | // Read the value of this private "type" field, which is the name |
| 349 | // of this globalField. It has to be read as a field, since that's |
| 350 | // how it's written. (The binary version depends on this!) |
| 351 | SoSFName typeField; |
| 352 | if (! typeField.read(in, "type")) |
| 353 | return FALSE; |
| 354 | typeName = typeField.getValue(); |
| 355 | |
| 356 | SoType type = SoType::fromName(typeName); |
| 357 | if (! type.isDerivedFrom(SoField::getClassTypeId())) { |
| 358 | SoReadError::post(in, "\"%s\" is not a type of field", |
| 359 | typeName.getString()); |
| 360 | return FALSE; |
| 361 | } |
| 362 | |
| 363 | // Now, get the name of the next field, which is the name of this |
| 364 | // globalField: |
| 365 | SbName myName; |
| 366 | if (! in->read(myName)) |
| 367 | return FALSE; |
| 368 | |
| 369 | // Try to create/get an appropriate global field: |
| 370 | SbBool alreadyExists; |
| 371 | SoGlobalField *result = create(myName, type, alreadyExists); |
| 372 | |
| 373 | // If there already was one with the same name and a different type |
| 374 | if (result == NULL) |
| 375 | return NULL; |
| 376 | |
| 377 | if (alreadyExists) { |
| 378 | // We need to read and throw away the value... |
| 379 | SoField *t = (SoField *)type.createInstance(); |
| 380 | // If the field has connections, it needs to have a non-NULL |
| 381 | // container... |
| 382 | t->setContainer(result); |
| 383 | t->enableNotify(FALSE); |
| 384 | if (!t->read(in, myName)) { |
| 385 | delete result; |
| 386 | delete t; |
| 387 | return NULL; |
| 388 | } |
| 389 | // Get rid of any connections that may have been established-- |
| 390 | // this gets rid of any extraneous auditors. |
| 391 | t->disconnect(); |
| 392 | delete t; |
| 393 | } |
| 394 | else { |
| 395 | // Read into newly-created global field. |
| 396 | if (!result->value->read(in, myName)) { |
| 397 | // Problem reading value field |
| 398 | delete result; |
| 399 | return NULL; |
| 400 | } |
| 401 | } |
| 402 | return result; |
| 403 | } |
| 404 | |
| 405 | ////////////////////////////////////////////////////////////////////////////// |
| 406 | // |
| 407 | // Description: |
| 408 | // Return the field this is a container for: |
| 409 | // |
| 410 | // Use: internal |
| 411 | |
| 412 | SoField * |
| 413 | SoGlobalField::getMyField() const |
| 414 | // |
| 415 | ////////////////////////////////////////////////////////////////////////////// |
| 416 | { |
| 417 | return value; |
| 418 | } |
| 419 | |
| 420 | ////////////////////////////////////////////////////////////////////////////// |
| 421 | // |
| 422 | // Description: |
| 423 | // Return the name of this global field. |
| 424 | // |
| 425 | // Use: internal |
| 426 | |
| 427 | SbName |
| 428 | SoGlobalField::getName() const |
| 429 | // |
| 430 | ////////////////////////////////////////////////////////////////////////////// |
| 431 | { |
| 432 | return fieldData->getFieldName(0); |
| 433 | } |
| 434 | |
| 435 | ////////////////////////////////////////////////////////////////////////////// |
| 436 | // |
| 437 | // Description: |
| 438 | // A handy utility routine that saved some typing |
| 439 | // |
| 440 | // Use: private |
| 441 | |
| 442 | SoType |
| 443 | SoGlobalField::getType() const |
| 444 | // |
| 445 | ////////////////////////////////////////////////////////////////////////////// |
| 446 | { |
| 447 | return SoType::fromName(typeField.getValue()); |
| 448 | } |
| 449 | |
| 450 | //////////////////////////////////////////////////////////////////////// |
| 451 | // |
| 452 | // Description: |
| 453 | // Adds a reference to the instance when writing. Always lie about |
| 454 | // isFromField, since it will always be from a field but we want to |
| 455 | // write out the global field anyway. |
| 456 | // |
| 457 | // Use: internal, virtual |
| 458 | |
| 459 | void |
| 460 | SoGlobalField::addWriteReference(SoOutput *out, SbBool) |
| 461 | // |
| 462 | //////////////////////////////////////////////////////////////////////// |
| 463 | { |
| 464 | SoFieldContainer::addWriteReference(out, FALSE); |
| 465 | } |