| 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 | // This may look like C code, but it is really -*- C++ -*- |
| 38 | |
| 39 | /* |
| 40 | * Copyright (C) 1990,91,92 Silicon Graphics, Inc. |
| 41 | * |
| 42 | _______________________________________________________________________ |
| 43 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 44 | | |
| 45 | | $Revision: 1.3 $ |
| 46 | | |
| 47 | | Classes: |
| 48 | | SoFieldContainer |
| 49 | | |
| 50 | | Author(s) : Gavin Bell |
| 51 | | |
| 52 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 53 | _______________________________________________________________________ |
| 54 | */ |
| 55 | |
| 56 | #include <Inventor/SbDict.h> |
| 57 | #include <Inventor/SoDB.h> |
| 58 | #include <Inventor/SoInput.h> |
| 59 | #include <Inventor/SoLists.h> |
| 60 | #include <Inventor/SoOutput.h> |
| 61 | #include <Inventor/errors/SoDebugError.h> |
| 62 | #include <Inventor/errors/SoReadError.h> |
| 63 | #include <Inventor/fields/SoField.h> |
| 64 | #include <Inventor/fields/SoFieldContainer.h> |
| 65 | #include <Inventor/fields/SoFieldData.h> |
| 66 | #include <Inventor/misc/SoNotification.h> |
| 67 | |
| 68 | #include <cstdlib> |
| 69 | |
| 70 | // Syntax for reading/writing type information to files |
| 71 | #define OPEN_BRACE_CHAR '[' |
| 72 | #define CLOSE_BRACE_CHAR ']' |
| 73 | #define VALUE_SEPARATOR_CHAR ',' |
| 74 | |
| 75 | // This is a list of SbDict instances used duting copy operations to |
| 76 | // keep track of instances. It is a list to allow recursive copying. |
| 77 | SbPList *SoFieldContainer::copyDictList = NULL; |
| 78 | |
| 79 | // These are used by SoFieldContainer::get() to hold the returned |
| 80 | // field string |
| 81 | // We rely on the compiler to initialize them to zero for us... |
| 82 | char *SoFieldContainer::fieldBuf; |
| 83 | int SoFieldContainer::fieldBufSize; |
| 84 | |
| 85 | SoType SoFieldContainer::classTypeId; |
| 86 | |
| 87 | //////////////////////////////////////////////////////////////////////// |
| 88 | // |
| 89 | // Description: |
| 90 | // Set up type info. |
| 91 | // |
| 92 | // Use: internal |
| 93 | |
| 94 | void |
| 95 | SoFieldContainer::initClass() |
| 96 | // |
| 97 | //////////////////////////////////////////////////////////////////////// |
| 98 | { |
| 99 | classTypeId = SoType::createType(SoBase::getClassTypeId(), |
| 100 | "FieldContainer"); |
| 101 | } |
| 102 | |
| 103 | //////////////////////////////////////////////////////////////////////// |
| 104 | // |
| 105 | // Description: |
| 106 | // Constructor |
| 107 | // |
| 108 | // Use: protected |
| 109 | |
| 110 | SoFieldContainer::SoFieldContainer() |
| 111 | // |
| 112 | //////////////////////////////////////////////////////////////////////// |
| 113 | { |
| 114 | notifyEnabled = TRUE; |
| 115 | } |
| 116 | |
| 117 | //////////////////////////////////////////////////////////////////////// |
| 118 | // |
| 119 | // Description: |
| 120 | // Destructor |
| 121 | // |
| 122 | // Use: protected |
| 123 | |
| 124 | SoFieldContainer::~SoFieldContainer() |
| 125 | // |
| 126 | //////////////////////////////////////////////////////////////////////// |
| 127 | { |
| 128 | // Fields must be disconnected when things are deleted. Backwards |
| 129 | // connections (feeding into my fields) are handled by the SoField |
| 130 | // destructor. Forward connections are handled in SoBase's |
| 131 | // destructor (it loops through the auditor list and calls |
| 132 | // disconnect for any field to field connections it finds). |
| 133 | } |
| 134 | |
| 135 | //////////////////////////////////////////////////////////////////////// |
| 136 | // |
| 137 | // Description: |
| 138 | // Sets all fields to their default values and turns on their |
| 139 | // default flags. |
| 140 | // |
| 141 | // Use: public |
| 142 | |
| 143 | void |
| 144 | SoFieldContainer::setToDefaults() |
| 145 | // |
| 146 | //////////////////////////////////////////////////////////////////////// |
| 147 | { |
| 148 | // Create an instance of the same type - it will have default values |
| 149 | SoFieldContainer *def = (SoFieldContainer *) getTypeId().createInstance(); |
| 150 | def->ref(); |
| 151 | |
| 152 | const SoFieldData *fieldData = getFieldData(); |
| 153 | |
| 154 | if ((fieldData != NULL) && (def != NULL)) |
| 155 | fieldData->overlay(this, def, FALSE); |
| 156 | |
| 157 | def->unref(); |
| 158 | } |
| 159 | |
| 160 | //////////////////////////////////////////////////////////////////////// |
| 161 | // |
| 162 | // Description: |
| 163 | // Returns TRUE if all fields have their default values (even if |
| 164 | // the isDefault() flags are not all set). |
| 165 | // |
| 166 | // Use: public |
| 167 | |
| 168 | SbBool |
| 169 | SoFieldContainer::hasDefaultValues() const |
| 170 | // |
| 171 | //////////////////////////////////////////////////////////////////////// |
| 172 | { |
| 173 | // Create an instance of the same type - it will have default values |
| 174 | SoFieldContainer *def = (SoFieldContainer *) getTypeId().createInstance(); |
| 175 | def->ref(); |
| 176 | |
| 177 | // Compare with this instance |
| 178 | SbBool sameValues = fieldsAreEqual(def); |
| 179 | |
| 180 | // Get rid of default instance |
| 181 | def->unref(); |
| 182 | |
| 183 | return sameValues; |
| 184 | } |
| 185 | |
| 186 | //////////////////////////////////////////////////////////////////////// |
| 187 | // |
| 188 | // Description: |
| 189 | // Returns TRUE if the field values match those of the passed |
| 190 | // instance, which is assumed to be of the same type as this. |
| 191 | // |
| 192 | // Use: public |
| 193 | |
| 194 | SbBool |
| 195 | SoFieldContainer::fieldsAreEqual(const SoFieldContainer *fc) const |
| 196 | // |
| 197 | //////////////////////////////////////////////////////////////////////// |
| 198 | { |
| 199 | #ifdef DEBUG |
| 200 | if (getTypeId() != fc->getTypeId()) { |
| 201 | SoDebugError::post("SoFieldContainer::fieldsAreEqual", |
| 202 | "Instances are of different types"); |
| 203 | return FALSE; |
| 204 | } |
| 205 | #endif /* DEBUG */ |
| 206 | |
| 207 | // Compare fields in field data |
| 208 | return getFieldData()->isSame(this, fc); |
| 209 | } |
| 210 | |
| 211 | //////////////////////////////////////////////////////////////////////// |
| 212 | // |
| 213 | // Description: |
| 214 | // Copies field values from the passed instance, which is assumed |
| 215 | // to be of the same type as this. If the copyConnections flag is |
| 216 | // TRUE (it is FALSE by default), any connections to (but not from) |
| 217 | // fields of the instance are copied, as well. |
| 218 | // |
| 219 | // Use: public |
| 220 | |
| 221 | void |
| 222 | SoFieldContainer::copyFieldValues(const SoFieldContainer *fc, |
| 223 | SbBool copyConnections) |
| 224 | // |
| 225 | //////////////////////////////////////////////////////////////////////// |
| 226 | { |
| 227 | #ifdef DEBUG |
| 228 | if (getTypeId() != fc->getTypeId()) { |
| 229 | SoDebugError::post("SoFieldContainer::copyFieldValues", |
| 230 | "Instances are of different types"); |
| 231 | return; |
| 232 | } |
| 233 | #endif /* DEBUG */ |
| 234 | |
| 235 | // Copy fields in field data instances |
| 236 | const SoFieldData *fieldData = getFieldData(); |
| 237 | if (fieldData != NULL) |
| 238 | fieldData->overlay(this, fc, copyConnections); |
| 239 | } |
| 240 | |
| 241 | //////////////////////////////////////////////////////////////////////// |
| 242 | // |
| 243 | // Description: |
| 244 | // Sets fields in object from info in passed string. The format of |
| 245 | // the string is the same as the ASCII file format, i.e., the stuff |
| 246 | // that could appear between the '{' and '}' for the object in a file. |
| 247 | // |
| 248 | // The SoInput parameter is NULL in the public method and may be |
| 249 | // non-NULL in the internal one. If it is non-NULL, it specifies |
| 250 | // the SoInput from which to get the reference dictionary to use |
| 251 | // when reading from the string. |
| 252 | // |
| 253 | // Use: public/internal |
| 254 | |
| 255 | SbBool |
| 256 | SoFieldContainer::set(const char *fieldDataString, SoInput *dictIn) |
| 257 | // |
| 258 | //////////////////////////////////////////////////////////////////////// |
| 259 | { |
| 260 | const SoFieldData *fieldData = getFieldData(); |
| 261 | |
| 262 | if (fieldData != NULL) { |
| 263 | SoInput in(dictIn); |
| 264 | in.setBuffer((void *) fieldDataString, strlen(fieldDataString)); |
| 265 | SbBool isBuiltIn; // Not used |
| 266 | return fieldData->read(&in, this, FALSE, isBuiltIn); |
| 267 | } |
| 268 | else |
| 269 | return FALSE; |
| 270 | } |
| 271 | |
| 272 | //////////////////////////////////////////////////////////////////////// |
| 273 | // |
| 274 | // Description: |
| 275 | // Does opposite of "set()" - stores data from fields in passed |
| 276 | // SbString. The format of the string is the same as the ASCII file |
| 277 | // format, i.e., the stuff that could appear between the '{' and |
| 278 | // '}' for the object in a file. |
| 279 | // |
| 280 | // The SoOutput parameter is NULL in the public method and may be |
| 281 | // non-NULL in the internal one. If it is non-NULL, it specifies |
| 282 | // the SoOutput from which to get the reference dictionary to use |
| 283 | // when writing into the string. |
| 284 | // |
| 285 | // Use: public/internal |
| 286 | |
| 287 | void |
| 288 | SoFieldContainer::get(SbString &fieldDataString, SoOutput *dictOut) |
| 289 | // |
| 290 | //////////////////////////////////////////////////////////////////////// |
| 291 | { |
| 292 | const SoFieldData *fieldData = getFieldData(); |
| 293 | |
| 294 | #ifdef DEBUG |
| 295 | if (fieldData == NULL) { |
| 296 | SoDebugError::post("SoFieldContainer::get", |
| 297 | "Field data is NULL!"); |
| 298 | return; |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | SoOutput out(dictOut); |
| 303 | |
| 304 | // Prepare a character buffer and SoOutput for writing field strings |
| 305 | if (fieldBufSize == 0) { |
| 306 | fieldBufSize = 1028; |
| 307 | fieldBuf = (char *) malloc((unsigned) fieldBufSize); |
| 308 | } |
| 309 | |
| 310 | // Set up output into a string buffer |
| 311 | out.setBuffer((void *) fieldBuf, fieldBufSize, |
| 312 | &SoFieldContainer::reallocFieldBuf); |
| 313 | |
| 314 | // Make sure that the file header and lots of white space will NOT |
| 315 | // be written into the string |
| 316 | out.setCompact(TRUE); |
| 317 | |
| 318 | // Prepare to begin writing by incrementing the |
| 319 | // current write counter in SoBase |
| 320 | SoBase::incrementCurrentWriteCounter(); |
| 321 | |
| 322 | // Count references first |
| 323 | out.setStage(SoOutput::COUNT_REFS); |
| 324 | getFieldData()->write(&out, this); |
| 325 | |
| 326 | // Next, do actual writing |
| 327 | out.setStage(SoOutput::WRITE); |
| 328 | getFieldData()->write(&out, this); |
| 329 | |
| 330 | // Make sure the string is terminated |
| 331 | out.reset(); |
| 332 | |
| 333 | // Store the result in the passed SbString |
| 334 | fieldDataString = fieldBuf; |
| 335 | } |
| 336 | |
| 337 | //////////////////////////////////////////////////////////////////////// |
| 338 | // |
| 339 | // Description: |
| 340 | // Reads stuff into instance of subclass of SoFieldContainer. |
| 341 | // Returns FALSE on error. |
| 342 | // |
| 343 | // Use: protected |
| 344 | |
| 345 | SbBool |
| 346 | SoFieldContainer::readInstance(SoInput *in, |
| 347 | unsigned short /* flags not used */) |
| 348 | // |
| 349 | //////////////////////////////////////////////////////////////////////// |
| 350 | { |
| 351 | SbBool notBuiltIn; // Not used |
| 352 | return getFieldData()->read(in, this, TRUE, notBuiltIn); |
| 353 | } |
| 354 | |
| 355 | //////////////////////////////////////////////////////////////////////// |
| 356 | // |
| 357 | // Description: |
| 358 | // Adds a reference to the instance when writing. isFromField |
| 359 | // indicates whether the reference is from a field-to-field |
| 360 | // connection. |
| 361 | // |
| 362 | // Use: internal, virtual |
| 363 | |
| 364 | void |
| 365 | SoFieldContainer::addWriteReference(SoOutput *out, SbBool isFromField) |
| 366 | // |
| 367 | //////////////////////////////////////////////////////////////////////// |
| 368 | { |
| 369 | // Do standard stuff |
| 370 | SoBase::addWriteReference(out, isFromField); |
| 371 | |
| 372 | // If this is the first reference to this instance and we are not |
| 373 | // being referenced from a field-to-field connection, give fields |
| 374 | // a chance to do their thing |
| 375 | if (! isFromField && ! hasMultipleWriteRefs()) { |
| 376 | SoFieldData *fieldData = (SoFieldData *) getFieldData(); |
| 377 | |
| 378 | // Since the SoOutput is in the COUNT_REFS stage, this call to |
| 379 | // write() will just take care of counting references |
| 380 | if (fieldData != NULL) |
| 381 | fieldData->write(out, this); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | //////////////////////////////////////////////////////////////////////// |
| 386 | // |
| 387 | // Description: |
| 388 | // Writes instance to SoOutput. |
| 389 | // |
| 390 | // Use: internal |
| 391 | |
| 392 | void |
| 393 | SoFieldContainer::writeInstance(SoOutput *out) |
| 394 | // |
| 395 | //////////////////////////////////////////////////////////////////////// |
| 396 | { |
| 397 | if (! writeHeader(out, FALSE, FALSE)) { |
| 398 | |
| 399 | getFieldData()->write(out, this); |
| 400 | |
| 401 | writeFooter(out); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | //////////////////////////////////////////////////////////////////////// |
| 406 | // |
| 407 | // Description: |
| 408 | // Default method returns a NULL SoFieldData structure for the object. |
| 409 | // |
| 410 | // Use: internal |
| 411 | |
| 412 | const SoFieldData * |
| 413 | SoFieldContainer::getFieldData() const |
| 414 | // |
| 415 | //////////////////////////////////////////////////////////////////////// |
| 416 | { |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | //////////////////////////////////////////////////////////////////////// |
| 421 | // |
| 422 | // Description: |
| 423 | // This is a callback function passed to SoOutput::setBuffer() by |
| 424 | // SoFieldContainer::get(). It is used to reallocate the buffer in which the |
| 425 | // returned field info string is stored. Since we need to keep |
| 426 | // track of the pointer to the buffer and its size, we can't just |
| 427 | // call realloc() instead. |
| 428 | // |
| 429 | // Use: private, static |
| 430 | |
| 431 | void * |
| 432 | SoFieldContainer::reallocFieldBuf(void *ptr, size_t newSize) |
| 433 | // |
| 434 | //////////////////////////////////////////////////////////////////////// |
| 435 | { |
| 436 | fieldBuf = (char *) realloc(ptr, newSize); |
| 437 | fieldBufSize = newSize; |
| 438 | |
| 439 | return fieldBuf; |
| 440 | } |
| 441 | |
| 442 | //////////////////////////////////////////////////////////////////////// |
| 443 | // |
| 444 | // Description: |
| 445 | // Return a list of my fields. |
| 446 | // |
| 447 | // Use: public |
| 448 | |
| 449 | int |
| 450 | SoFieldContainer::getFields(SoFieldList &list) const |
| 451 | // |
| 452 | //////////////////////////////////////////////////////////////////////// |
| 453 | { |
| 454 | int i; |
| 455 | const SoFieldData *fd = getFieldData(); |
| 456 | |
| 457 | if (fd == NULL) |
| 458 | return 0; |
| 459 | |
| 460 | for (i = 0; i < fd->getNumFields(); i++) { |
| 461 | list.append(fd->getField(this, i)); |
| 462 | } |
| 463 | |
| 464 | return fd->getNumFields(); |
| 465 | } |
| 466 | |
| 467 | //////////////////////////////////////////////////////////////////////// |
| 468 | // |
| 469 | // Description: |
| 470 | // Returns a pointer to the field with the given name. If no such |
| 471 | // field exists, NULL is returned. |
| 472 | // |
| 473 | // Use: public |
| 474 | |
| 475 | SoField * |
| 476 | SoFieldContainer::getField(const SbName &fieldName) const |
| 477 | // |
| 478 | //////////////////////////////////////////////////////////////////////// |
| 479 | { |
| 480 | int i; |
| 481 | const SoFieldData *fd = getFieldData(); |
| 482 | |
| 483 | if (fd == NULL) |
| 484 | return NULL; |
| 485 | |
| 486 | // Search fields for one with given name |
| 487 | for (i = 0; i < fd->getNumFields(); i++) |
| 488 | if (fd->getFieldName(i) == fieldName) |
| 489 | return fd->getField(this, i); |
| 490 | |
| 491 | // Not found... |
| 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | //////////////////////////////////////////////////////////////////////// |
| 496 | // |
| 497 | // Description: |
| 498 | // Returns (in fieldName) the name of the field pointed to. Returns |
| 499 | // FALSE if the field is not contained within the field container |
| 500 | // instance. |
| 501 | // |
| 502 | // Use: public |
| 503 | |
| 504 | SbBool |
| 505 | SoFieldContainer::getFieldName(const SoField *field, SbName &fieldName) const |
| 506 | // |
| 507 | //////////////////////////////////////////////////////////////////////// |
| 508 | { |
| 509 | int i; |
| 510 | const SoFieldData *fd = getFieldData(); |
| 511 | |
| 512 | if (fd == NULL) |
| 513 | return FALSE; |
| 514 | |
| 515 | // Search fields for one with given pointer |
| 516 | for (i = 0; i < fd->getNumFields(); i++) { |
| 517 | if (fd->getField(this, i) == field) { |
| 518 | fieldName = fd->getFieldName(i); |
| 519 | return TRUE; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | // Not found... |
| 524 | return FALSE; |
| 525 | } |
| 526 | |
| 527 | //////////////////////////////////////////////////////////////////////// |
| 528 | // |
| 529 | // Description: |
| 530 | // Propagates notification. |
| 531 | // |
| 532 | // Use: internal |
| 533 | |
| 534 | void |
| 535 | SoFieldContainer::notify(SoNotList *list) |
| 536 | // |
| 537 | //////////////////////////////////////////////////////////////////////// |
| 538 | { |
| 539 | #ifdef DEBUG |
| 540 | if (list == NULL) { |
| 541 | SoDebugError::post("SoFieldContainer::notify", |
| 542 | "Notification list pointer is NULL"); |
| 543 | return; |
| 544 | } |
| 545 | #endif /* DEBUG */ |
| 546 | |
| 547 | // Don't notify if application has disabled notification for some reason |
| 548 | if (! isNotifyEnabled()) |
| 549 | return; |
| 550 | |
| 551 | // There are only 2 types of notification possible here: CONTAINER |
| 552 | // (a field notifying its container), or PARENT (a node notifying |
| 553 | // its parent). The other types are handled by the other classes. |
| 554 | |
| 555 | switch (list->getLastRec()->getType()) { |
| 556 | case SoNotRec::CONTAINER: |
| 557 | // We don't need to create a new record, since the last |
| 558 | // record's base is already set to this. Just notify all |
| 559 | // auditors. |
| 560 | SoBase::notify(list); |
| 561 | break; |
| 562 | |
| 563 | case SoNotRec::PARENT: |
| 564 | // If we're notifying from a child node to a parent node, |
| 565 | // create a new record that contains the parent node as a |
| 566 | // base, then notify. |
| 567 | { |
| 568 | SoNotRec rec(this); |
| 569 | list->append(&rec); |
| 570 | SoBase::notify(list); |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | default: |
| 575 | #ifdef DEBUG |
| 576 | SoDebugError::post("SoFieldContainer::notify (internal)", |
| 577 | "Got notification of type %d", |
| 578 | list->getLastRec()->getType()); |
| 579 | #endif /* DEBUG */ |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | //////////////////////////////////////////////////////////////////////// |
| 585 | // |
| 586 | // Description: |
| 587 | // Initializes the copy dictionary. Returns FALSE if the dictionary |
| 588 | // already exists (probably indicating a recursive copy is taking |
| 589 | // place). |
| 590 | // |
| 591 | // Use: internal, static |
| 592 | |
| 593 | void |
| 594 | SoFieldContainer::initCopyDict() |
| 595 | // |
| 596 | //////////////////////////////////////////////////////////////////////// |
| 597 | { |
| 598 | if (copyDictList == NULL) |
| 599 | copyDictList = new SbPList; |
| 600 | |
| 601 | SbDict *copyDict = new SbDict; |
| 602 | |
| 603 | // Insert the new dictionary at the beginning. Since most copies |
| 604 | // are non-recursive, having to make room in the list won't happen |
| 605 | // too frequently. Accessing the list happens a lot, so using slot |
| 606 | // 0 will speed that up some. |
| 607 | copyDictList->insert(copyDict, 0); |
| 608 | } |
| 609 | |
| 610 | //////////////////////////////////////////////////////////////////////// |
| 611 | // |
| 612 | // Description: |
| 613 | // Adds an entry to the copy dictionary. |
| 614 | // |
| 615 | // Use: internal, static |
| 616 | |
| 617 | void |
| 618 | SoFieldContainer::addCopy(const SoFieldContainer *orig, |
| 619 | const SoFieldContainer *copy) |
| 620 | // |
| 621 | //////////////////////////////////////////////////////////////////////// |
| 622 | { |
| 623 | SbDict *copyDict = (SbDict *) (*copyDictList)[0]; |
| 624 | |
| 625 | // Add a reference when entering an instance into the |
| 626 | // dictionary. The references are removed before the dictionary is |
| 627 | // deleted. |
| 628 | copy->ref(); |
| 629 | |
| 630 | // ??? HACK ALERT: We use the notifyEnabled flag in the copy to |
| 631 | // indicate whether its contents have been copied yet. If the flag |
| 632 | // is FALSE, the copy has not yet been done. It's pretty safe to do |
| 633 | // this because the notifyEnabled flag is not copied between |
| 634 | // instances. When copyContents() is called for the first time on |
| 635 | // a copied instance, we reset the flag to TRUE. |
| 636 | ((SoFieldContainer *) copy)->notifyEnabled = FALSE; |
| 637 | |
| 638 | copyDict->enter((unsigned long) orig, (void *) copy); |
| 639 | } |
| 640 | |
| 641 | //////////////////////////////////////////////////////////////////////// |
| 642 | // |
| 643 | // Description: |
| 644 | // If a copy of the given instance is in the dictionary, this |
| 645 | // returns it. Otherwise, it returns NULL. The copy is not changed |
| 646 | // in any way. |
| 647 | // |
| 648 | // Use: internal, static |
| 649 | |
| 650 | SoFieldContainer * |
| 651 | SoFieldContainer::checkCopy(const SoFieldContainer *orig) |
| 652 | // |
| 653 | //////////////////////////////////////////////////////////////////////// |
| 654 | { |
| 655 | SbDict *copyDict = (SbDict *) (*copyDictList)[0]; |
| 656 | |
| 657 | void *copyPtr; |
| 658 | if (! copyDict->find((unsigned long) orig, copyPtr)) |
| 659 | return NULL; |
| 660 | |
| 661 | return (SoFieldContainer *) copyPtr; |
| 662 | } |
| 663 | |
| 664 | //////////////////////////////////////////////////////////////////////// |
| 665 | // |
| 666 | // Description: |
| 667 | // If a copy of the given instance is not in the dictionary, this |
| 668 | // returns NULL. Otherwise, this copies the contents of the |
| 669 | // original into the copy (if not already done) and returns a |
| 670 | // pointer to the copy. |
| 671 | // |
| 672 | // Use: internal, static |
| 673 | |
| 674 | SoFieldContainer * |
| 675 | SoFieldContainer::findCopy(const SoFieldContainer *orig, |
| 676 | SbBool copyConnections) |
| 677 | // |
| 678 | //////////////////////////////////////////////////////////////////////// |
| 679 | { |
| 680 | if (! copyDictList || ! (*copyDictList)[0]) |
| 681 | return NULL; |
| 682 | |
| 683 | SbDict *copyDict = (SbDict *) (*copyDictList)[0]; |
| 684 | |
| 685 | void *copyPtr; |
| 686 | if (! copyDict->find((unsigned long) orig, copyPtr)) |
| 687 | return NULL; |
| 688 | |
| 689 | SoFieldContainer *copyFC = (SoFieldContainer *) copyPtr; |
| 690 | |
| 691 | // ??? Copy the contents only if the notifyEnabled flag is FALSE, |
| 692 | // indicating that the copy has not yet been done. See the HACK |
| 693 | // ALERT above. |
| 694 | if (! copyFC->notifyEnabled) { |
| 695 | copyFC->notifyEnabled = TRUE; |
| 696 | copyFC->copyContents(orig, copyConnections); |
| 697 | } |
| 698 | |
| 699 | return copyFC; |
| 700 | } |
| 701 | |
| 702 | //////////////////////////////////////////////////////////////////////// |
| 703 | // |
| 704 | // Description: |
| 705 | // Cleans up the copy dictionary. |
| 706 | // |
| 707 | // Use: internal, static |
| 708 | |
| 709 | void |
| 710 | SoFieldContainer::copyDone() |
| 711 | // |
| 712 | //////////////////////////////////////////////////////////////////////// |
| 713 | { |
| 714 | #ifdef DEBUG |
| 715 | if (copyDictList->getLength() <= 0) { |
| 716 | SoDebugError::post("SoFieldContainer::copyDone", |
| 717 | "No dictionary left to get rid of"); |
| 718 | return; |
| 719 | } |
| 720 | #endif /* DEBUG */ |
| 721 | |
| 722 | SbDict *copyDict = (SbDict *) (*copyDictList)[0]; |
| 723 | |
| 724 | // Unref every instance in the copy dictionary |
| 725 | copyDict->applyToAll(unrefCopy); |
| 726 | |
| 727 | delete copyDict; |
| 728 | |
| 729 | copyDictList->remove(0); |
| 730 | } |
| 731 | |
| 732 | //////////////////////////////////////////////////////////////////////// |
| 733 | // |
| 734 | // Description: |
| 735 | // This callback is used to unref() all instances in the |
| 736 | // copyDict when copyDone() is called. |
| 737 | // |
| 738 | // Use: private, static |
| 739 | |
| 740 | void |
| 741 | SoFieldContainer::unrefCopy(unsigned long, void *instPtr) |
| 742 | // |
| 743 | //////////////////////////////////////////////////////////////////////// |
| 744 | { |
| 745 | SoFieldContainer *inst = (SoFieldContainer *) instPtr; |
| 746 | |
| 747 | // Set the notifyEnabled bit to TRUE if it wasn't already done |
| 748 | if (! inst->notifyEnabled) |
| 749 | inst->notifyEnabled = TRUE; |
| 750 | |
| 751 | inst->unref(); |
| 752 | } |
| 753 | |
| 754 | //////////////////////////////////////////////////////////////////////// |
| 755 | // |
| 756 | // Description: |
| 757 | // Copies the contents of the given node into this instance. The |
| 758 | // default implementation copies just field values and the name. |
| 759 | // |
| 760 | // Use: internal, virtual |
| 761 | |
| 762 | void |
| 763 | SoFieldContainer::copyContents(const SoFieldContainer *fromFC, |
| 764 | SbBool copyConnections) |
| 765 | // |
| 766 | //////////////////////////////////////////////////////////////////////// |
| 767 | { |
| 768 | // Access the field data and overlay it |
| 769 | const SoFieldData *fieldData = getFieldData(); |
| 770 | if (fieldData != NULL) |
| 771 | fieldData->overlay(this, fromFC, copyConnections); |
| 772 | |
| 773 | // Copy the name, if it has one |
| 774 | const SbName &name = fromFC->getName(); |
| 775 | if (! ! name) |
| 776 | setName(name); |
| 777 | } |
| 778 | |
| 779 | //////////////////////////////////////////////////////////////////////// |
| 780 | // |
| 781 | // Description: |
| 782 | // During a copy operation, this copies an instance that is |
| 783 | // encountered through a field connection. The default |
| 784 | // implementation just returns the original pointer - no copy is |
| 785 | // done. Subclasses such as nodes and engines handle this |
| 786 | // differently. |
| 787 | // |
| 788 | // Use: internal, virtual |
| 789 | |
| 790 | SoFieldContainer * |
| 791 | SoFieldContainer::copyThroughConnection() const |
| 792 | // |
| 793 | //////////////////////////////////////////////////////////////////////// |
| 794 | { |
| 795 | return (SoFieldContainer *) this; |
| 796 | } |