| 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.2 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoUnknownNode |
| 47 | | |
| 48 | | Author(s) : Paul S. Strauss, Dave Immel, 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 | #include <cstdlib> |
| 56 | #include <Inventor/misc/SoState.h> |
| 57 | #include <Inventor/actions/SoWriteAction.h> |
| 58 | #include <Inventor/fields/SoMFString.h> |
| 59 | #include <Inventor/fields/SoSFNode.h> |
| 60 | #include <SoUnknownNode.h> |
| 61 | |
| 62 | // Because of the special treatment of fieldData, we can't use the |
| 63 | // regular SO_NODE_SOURCE macro. |
| 64 | SO__NODE_VARS(SoUnknownNode); |
| 65 | |
| 66 | SoType |
| 67 | SoUnknownNode::getTypeId() const |
| 68 | { |
| 69 | return classTypeId; |
| 70 | } |
| 71 | |
| 72 | const SoFieldData * |
| 73 | SoUnknownNode::getFieldData() const |
| 74 | { |
| 75 | return instanceFieldData; |
| 76 | } |
| 77 | |
| 78 | void * |
| 79 | SoUnknownNode::createInstance() |
| 80 | { |
| 81 | #ifdef DEBUG |
| 82 | SoDebugError::post("SoUnknownNode::createInstance", |
| 83 | "Unknown nodes should be created only by " |
| 84 | "SoBase::read methods when reading"); |
| 85 | #endif |
| 86 | return (void *)(new SoUnknownNode()); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | //////////////////////////////////////////////////////////////////////// |
| 91 | // |
| 92 | // Description: |
| 93 | // Constructor |
| 94 | // |
| 95 | // Use: public |
| 96 | |
| 97 | SoUnknownNode::SoUnknownNode() : hiddenChildren(this) |
| 98 | // |
| 99 | //////////////////////////////////////////////////////////////////////// |
| 100 | { |
| 101 | SO_NODE_CONSTRUCTOR(SoUnknownNode); |
| 102 | |
| 103 | instanceFieldData = new SoFieldData(fieldData); |
| 104 | className = NULL; |
| 105 | |
| 106 | hasChildren = TRUE; |
| 107 | } |
| 108 | |
| 109 | //////////////////////////////////////////////////////////////////////// |
| 110 | // |
| 111 | // Description: |
| 112 | // Set the class name of the unknown node |
| 113 | // |
| 114 | // Use: public |
| 115 | |
| 116 | void |
| 117 | SoUnknownNode::setClassName( const char *name ) |
| 118 | |
| 119 | // |
| 120 | //////////////////////////////////////////////////////////////////////// |
| 121 | { |
| 122 | className = strdup(name); |
| 123 | } |
| 124 | |
| 125 | //////////////////////////////////////////////////////////////////////// |
| 126 | // |
| 127 | // Description: |
| 128 | // Destructor |
| 129 | // |
| 130 | // Use: private |
| 131 | |
| 132 | SoUnknownNode::~SoUnknownNode() |
| 133 | // |
| 134 | //////////////////////////////////////////////////////////////////////// |
| 135 | { |
| 136 | // |
| 137 | // The fields of the unknown node have been allocated by the SoFieldData |
| 138 | // class. The unknown node will delete them here, because the field |
| 139 | // data does not have enough information to know where its fields are |
| 140 | // stored. This could be redesigned. |
| 141 | // |
| 142 | SoFieldList fieldList; |
| 143 | int numFields = getFields(fieldList); |
| 144 | |
| 145 | for (int i=0; i<numFields; i++) |
| 146 | delete fieldList[i]; |
| 147 | |
| 148 | delete instanceFieldData; |
| 149 | |
| 150 | if (className) free((void *)className); |
| 151 | } |
| 152 | |
| 153 | //////////////////////////////////////////////////////////////////////// |
| 154 | // |
| 155 | // Description: |
| 156 | // Reads field type data, fields, and children into instance of |
| 157 | // SoUnknownNode. Returns FALSE on error. |
| 158 | // |
| 159 | // Use: virtual, protected |
| 160 | |
| 161 | SbBool |
| 162 | SoUnknownNode::readInstance(SoInput *in, unsigned short flags) |
| 163 | |
| 164 | // |
| 165 | //////////////////////////////////////////////////////////////////////// |
| 166 | { |
| 167 | int i; |
| 168 | |
| 169 | // This is mostly the normal SoGroup reading code, but we look for |
| 170 | // an alternateRep field after reading and move our public |
| 171 | // children onto the hidden children list: |
| 172 | |
| 173 | hasChildren = (!in->isBinary() || (flags & IS_GROUP)); |
| 174 | SbBool result; |
| 175 | if (hasChildren) { |
| 176 | result = SoGroup::readInstance(in, flags); |
| 177 | |
| 178 | // If read ASCII AND didn't read any children, set hasChildren |
| 179 | // to FALSE: |
| 180 | if (!in->isBinary() && getNumChildren() == 0) hasChildren = FALSE; |
| 181 | |
| 182 | // Add all kids to hiddenChildren, then remove them all from the |
| 183 | // regular (SoGroup) list |
| 184 | for (i = 0; i < getNumChildren(); i++) { |
| 185 | hiddenChildren.append(getChild(i)); |
| 186 | } |
| 187 | removeAllChildren(); |
| 188 | } |
| 189 | else { |
| 190 | result = SoNode::readInstance(in, flags); |
| 191 | } |
| 192 | |
| 193 | // Check to see if an alternate representation was read and |
| 194 | // store a pointer to it if one is found. |
| 195 | int num = instanceFieldData->getNumFields(); |
| 196 | SbBool haveAlternateRep = FALSE; |
| 197 | for (i=0; i<num; i++) { |
| 198 | if (instanceFieldData->getFieldName(i) == SbName("alternateRep")) { |
| 199 | SoField *f = instanceFieldData->getField(this, i); |
| 200 | if (f->isOfType(SoSFNode::getClassTypeId())) { |
| 201 | haveAlternateRep = TRUE; |
| 202 | SoSFNode *alternateRepField = (SoSFNode *)f; |
| 203 | SoNode *n = alternateRepField->getValue(); |
| 204 | if (n != NULL) |
| 205 | addChild(n); |
| 206 | } |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // If no alternateRep was specified, look for a field named "isA" |
| 212 | // of type MFString and try to automatically create an |
| 213 | // alternateRep: |
| 214 | if (!haveAlternateRep) for (i=0; i<num; i++) { |
| 215 | if (instanceFieldData->getFieldName(i) == SbName("isA")) { |
| 216 | SoField *f = instanceFieldData->getField(this, i); |
| 217 | if (f->isOfType(SoMFString::getClassTypeId())) { |
| 218 | createFromIsA((SoMFString *)f); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return result; |
| 224 | } |
| 225 | |
| 226 | //////////////////////////////////////////////////////////////////////// |
| 227 | // |
| 228 | // Description: |
| 229 | // Given an MFString "isA", create an appropriate alternateRep and |
| 230 | // adds it as a child. |
| 231 | // |
| 232 | // Use: private |
| 233 | |
| 234 | void |
| 235 | SoUnknownNode::createFromIsA(SoMFString *isA) |
| 236 | // |
| 237 | //////////////////////////////////////////////////////////////////////// |
| 238 | { |
| 239 | for (int i = 0; i < isA->getNum(); i++) { |
| 240 | SoType t = SoType::fromName((*isA)[i]); |
| 241 | |
| 242 | if (t.canCreateInstance() && |
| 243 | t.isDerivedFrom(SoNode::getClassTypeId())) { |
| 244 | |
| 245 | SoNode *alternateRep = (SoNode *)t.createInstance(); |
| 246 | alternateRep->ref(); |
| 247 | #ifdef DEBUG |
| 248 | if (alternateRep == NULL) { |
| 249 | SoDebugError::post("SoUnknownNode::createFromIsA", |
| 250 | "SoType.createInstance returned " |
| 251 | "NULL (type %s)", |
| 252 | t.getName().getString()); |
| 253 | return; |
| 254 | } |
| 255 | #endif |
| 256 | // Copy over all fields that are shared: |
| 257 | int num = instanceFieldData->getNumFields(); |
| 258 | for (int j=0; j<num; j++) { |
| 259 | const SbName &fieldName = instanceFieldData->getFieldName(j); |
| 260 | SoField *f = instanceFieldData->getField(this, j); |
| 261 | // Don't copy over fields with default values: |
| 262 | if (f->isDefault()) continue; |
| 263 | |
| 264 | SoField *nf = alternateRep->getField(fieldName); |
| 265 | if (nf != NULL && nf->getTypeId() == f->getTypeId()) { |
| 266 | nf->copyFrom(*f); |
| 267 | if (f->isConnectedFromField()) { |
| 268 | SoField *cf; |
| 269 | f->getConnectedField(cf); |
| 270 | nf->connectFrom(cf); |
| 271 | } else if (f->isConnectedFromEngine()) { |
| 272 | SoEngineOutput *eo; |
| 273 | f->getConnectedEngine(eo); |
| 274 | nf->connectFrom(eo); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | // And if alternateRep is a group, copy over hidden |
| 279 | // children: |
| 280 | if (alternateRep->isOfType(SoGroup::getClassTypeId())) { |
| 281 | SoGroup *g = (SoGroup *)alternateRep; |
| 282 | for (int kid = 0; kid < hiddenChildren.getLength(); |
| 283 | kid++) { |
| 284 | g->addChild(hiddenChildren[kid]); |
| 285 | } |
| 286 | } |
| 287 | addChild(alternateRep); |
| 288 | return; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | //////////////////////////////////////////////////////////////////////// |
| 294 | // |
| 295 | // Description: |
| 296 | // Implements write action. We use SoGroup's write method, after |
| 297 | // fooling around with some stuff so the correct class name and the |
| 298 | // correct children get written. |
| 299 | // |
| 300 | // Use: private |
| 301 | |
| 302 | void |
| 303 | SoUnknownNode::write(SoWriteAction *action) |
| 304 | // |
| 305 | //////////////////////////////////////////////////////////////////////// |
| 306 | { |
| 307 | int i; |
| 308 | |
| 309 | SbBool saveNotify = enableNotify(FALSE); |
| 310 | |
| 311 | // Remember alternateRep, if set: |
| 312 | SoNode *alternateRep = NULL; |
| 313 | |
| 314 | if (hasChildren) { |
| 315 | if (getNumChildren() != 0) { |
| 316 | alternateRep = getChild(0); |
| 317 | alternateRep->ref(); |
| 318 | } |
| 319 | |
| 320 | // Add hiddenChildren to regular child list temporarily: |
| 321 | removeAllChildren(); |
| 322 | for (i = 0; i < hiddenChildren.getLength(); i++) { |
| 323 | addChild(hiddenChildren[i]); |
| 324 | } |
| 325 | // Now write: |
| 326 | SoGroup::write(action); |
| 327 | |
| 328 | removeAllChildren(); |
| 329 | } |
| 330 | else { |
| 331 | SoNode::write(action); |
| 332 | } |
| 333 | |
| 334 | if (alternateRep != NULL) { |
| 335 | addChild(alternateRep); |
| 336 | alternateRep->unref(); |
| 337 | } |
| 338 | |
| 339 | enableNotify(saveNotify); |
| 340 | } |
| 341 | |
| 342 | //////////////////////////////////////////////////////////////////////// |
| 343 | // |
| 344 | // Description: |
| 345 | // Override searchAction so children (alternateRep) aren't |
| 346 | // searched. |
| 347 | // |
| 348 | // Use: public |
| 349 | |
| 350 | void |
| 351 | SoUnknownNode::search(SoSearchAction *action) |
| 352 | // |
| 353 | //////////////////////////////////////////////////////////////////////// |
| 354 | { |
| 355 | SoNode::search(action); |
| 356 | } |
| 357 | |
| 358 | //////////////////////////////////////////////////////////////////////// |
| 359 | // |
| 360 | // Description: |
| 361 | // Recursively adds this node and all nodes under it to the copy |
| 362 | // dictionary. Returns the copy of this node. |
| 363 | // |
| 364 | // Use: protected, virtual |
| 365 | |
| 366 | SoNode * |
| 367 | SoUnknownNode::addToCopyDict() const |
| 368 | // |
| 369 | //////////////////////////////////////////////////////////////////////// |
| 370 | { |
| 371 | // If this node is already in the dictionary, nothing else to do |
| 372 | SoNode *copy = (SoNode *) checkCopy(this); |
| 373 | if (copy == NULL) { |
| 374 | |
| 375 | // Create and add a new instance to the dictionary |
| 376 | copy = new SoUnknownNode; |
| 377 | copy->ref(); |
| 378 | addCopy(this, copy); // Adds a ref() |
| 379 | copy->unrefNoDelete(); |
| 380 | |
| 381 | // Recurse on children, if any |
| 382 | for (int i = 0; i < hiddenChildren.getLength(); i++) |
| 383 | hiddenChildren[i]->addToCopyDict(); |
| 384 | } |
| 385 | |
| 386 | return copy; |
| 387 | } |
| 388 | |
| 389 | //////////////////////////////////////////////////////////////////////// |
| 390 | // |
| 391 | // Description: |
| 392 | // Copies the contents of the given node into this instance. |
| 393 | // |
| 394 | // Use: protected, virtual |
| 395 | |
| 396 | void |
| 397 | SoUnknownNode::copyContents(const SoFieldContainer *fromFC, |
| 398 | SbBool copyConnections) |
| 399 | // |
| 400 | //////////////////////////////////////////////////////////////////////// |
| 401 | { |
| 402 | // Make sure the copy has the correct class name |
| 403 | const SoUnknownNode *fromUnk = (const SoUnknownNode *) fromFC; |
| 404 | setClassName(fromUnk->className); |
| 405 | |
| 406 | // For each field in the original node, create a new field and add |
| 407 | // it to the new node |
| 408 | |
| 409 | // NOTE: We can't use SoNode::copyContents() to copy the field |
| 410 | // data, since that uses SoFieldData::overlay(), which assumes the |
| 411 | // fields have the same offsets in both nodes. Instead, we just |
| 412 | // copy the field values ourselves. |
| 413 | |
| 414 | const SoFieldData *fromData = fromUnk->getFieldData(); |
| 415 | SoFieldData *toData = (SoFieldData *) getFieldData(); |
| 416 | |
| 417 | int i; |
| 418 | for (i = 0; i < fromData->getNumFields(); i++) { |
| 419 | |
| 420 | SoField *fromField = fromData->getField(fromUnk, i); |
| 421 | const SbName fieldName = fromData->getFieldName(i); |
| 422 | SoType fieldType = fromField->getTypeId(); |
| 423 | SoField *toField = (SoField *) (fieldType.createInstance()); |
| 424 | |
| 425 | toField->enableNotify(FALSE); |
| 426 | toField->setContainer(this); |
| 427 | toField->setDefault(TRUE); |
| 428 | toField->enableNotify(TRUE); |
| 429 | |
| 430 | toData->addField(this, fieldName.getString(), toField); |
| 431 | |
| 432 | toField->setContainer(this); |
| 433 | toField->copyFrom(*fromField); |
| 434 | toField->setIgnored(fromField->isIgnored()); |
| 435 | toField->setDefault(fromField->isDefault()); |
| 436 | toField->fixCopy(copyConnections); |
| 437 | if (fromField->isConnected() && copyConnections) |
| 438 | toField->copyConnection(fromField); |
| 439 | } |
| 440 | |
| 441 | // Copy the kids |
| 442 | for (i = 0; i < fromUnk->hiddenChildren.getLength(); i++) { |
| 443 | |
| 444 | // If this node is being copied, it must be "inside" (see |
| 445 | // SoNode::copy() for details.) Therefore, all of its children |
| 446 | // must be inside, as well, since our addToCopyDict() takes |
| 447 | // care of that. |
| 448 | SoNode *fromKid = fromUnk->getChild(i); |
| 449 | SoNode *kidCopy = (SoNode *) findCopy(fromKid, copyConnections); |
| 450 | |
| 451 | #ifdef DEBUG |
| 452 | if (kidCopy == NULL) |
| 453 | SoDebugError::post("(internal) SoUnknownNode::copyContents", |
| 454 | "Child %d has not been copied yet", i); |
| 455 | #endif /* DEBUG */ |
| 456 | |
| 457 | hiddenChildren.append(kidCopy); |
| 458 | } |
| 459 | |
| 460 | // No need to copy the override flag, since this flag will have no |
| 461 | // effect on an unknown node, and it is not read from or written |
| 462 | // to files. |
| 463 | } |
| 464 | |
| 465 | //////////////////////////////////////////////////////////////////////// |
| 466 | // |
| 467 | // Description: |
| 468 | // Returns file format name. |
| 469 | // |
| 470 | // Use: protected |
| 471 | |
| 472 | const char * |
| 473 | SoUnknownNode::getFileFormatName() const |
| 474 | // |
| 475 | //////////////////////////////////////////////////////////////////////// |
| 476 | { |
| 477 | return className; |
| 478 | } |
| 479 |