| 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 | | SoSFPath |
| 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 | #include <Inventor/SoDB.h> |
| 55 | #include <Inventor/SoPath.h> |
| 56 | #include <Inventor/actions/SoWriteAction.h> |
| 57 | #include <Inventor/fields/SoSFPath.h> |
| 58 | |
| 59 | ////////////////////////////////////////////////////////////////////////////// |
| 60 | // |
| 61 | // SoSFPath class - contains a pointer to an SoPath. This has to |
| 62 | // implement some extra path bookkeeping. |
| 63 | // |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | // Use standard definitions of required methods, but we have to |
| 67 | // redefine the rest to keep track of references and auditors. |
| 68 | |
| 69 | SO_SFIELD_REQUIRED_SOURCE(SoSFPath); |
| 70 | |
| 71 | |
| 72 | //////////////////////////////////////////////////////////////////////// |
| 73 | // |
| 74 | // Description: |
| 75 | // Constructor |
| 76 | // |
| 77 | // Use: public |
| 78 | |
| 79 | SoSFPath::SoSFPath() |
| 80 | // |
| 81 | //////////////////////////////////////////////////////////////////////// |
| 82 | { |
| 83 | // This will prevent treating random memory as an SoPath |
| 84 | value = NULL; |
| 85 | head = NULL; |
| 86 | } |
| 87 | |
| 88 | //////////////////////////////////////////////////////////////////////// |
| 89 | // |
| 90 | // Description: |
| 91 | // Destructor |
| 92 | // |
| 93 | // Use: public |
| 94 | |
| 95 | SoSFPath::~SoSFPath() |
| 96 | // |
| 97 | //////////////////////////////////////////////////////////////////////// |
| 98 | { |
| 99 | if (value != NULL) { |
| 100 | if (value->getHead() != NULL) |
| 101 | value->getHead()->removeAuditor(this, SoNotRec::FIELD); |
| 102 | value->removeAuditor(this, SoNotRec::FIELD); |
| 103 | value->unref(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | //////////////////////////////////////////////////////////////////////// |
| 108 | // |
| 109 | // Description: |
| 110 | // Sets value to given path pointer. |
| 111 | // |
| 112 | // Use: public |
| 113 | |
| 114 | void |
| 115 | SoSFPath::setValue(SoPath *newValue) |
| 116 | // |
| 117 | //////////////////////////////////////////////////////////////////////// |
| 118 | { |
| 119 | setVal(newValue); |
| 120 | valueChanged(); |
| 121 | } |
| 122 | |
| 123 | //////////////////////////////////////////////////////////////////////// |
| 124 | // |
| 125 | // Description: |
| 126 | // Equality test. (Standard definition.) |
| 127 | // |
| 128 | // Use: public |
| 129 | |
| 130 | int |
| 131 | SoSFPath::operator ==(const SoSFPath &f) const |
| 132 | // |
| 133 | //////////////////////////////////////////////////////////////////////// |
| 134 | { |
| 135 | return getValue() == f.getValue(); |
| 136 | } |
| 137 | |
| 138 | //////////////////////////////////////////////////////////////////////// |
| 139 | // |
| 140 | // Description: |
| 141 | // Propagates notification through the field. We have to override |
| 142 | // this to track changes to the path we are watching. For example, |
| 143 | // if the path is truncated or the head node changes, we need to |
| 144 | // reset our head node auditor. |
| 145 | // |
| 146 | // NOTE: This code is essentially the same as the code in |
| 147 | // SoPathSensor::notify(). |
| 148 | // |
| 149 | // Use: internal |
| 150 | |
| 151 | void |
| 152 | SoSFPath::notify(SoNotList *list) |
| 153 | // |
| 154 | //////////////////////////////////////////////////////////////////////// |
| 155 | { |
| 156 | // If there is nothing on the list, which would be the case if the |
| 157 | // field has been read from file, do the notify thing. |
| 158 | if (list->getFirstRec() == NULL) { |
| 159 | SoSField::notify(list); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | SbBool doNotify; |
| 164 | |
| 165 | // If the path started notification: |
| 166 | if (list->getFirstRec()->getBase() == value) |
| 167 | doNotify = TRUE; |
| 168 | |
| 169 | // If the notification came through the head node |
| 170 | else |
| 171 | doNotify = value->isRelevantNotification(list); |
| 172 | |
| 173 | // If the head node of the path changed, detach from old head and |
| 174 | // attach to new: |
| 175 | if (value->getHead() != head) { |
| 176 | // Detach from old: |
| 177 | if (head != NULL) |
| 178 | head->removeAuditor(this, SoNotRec::FIELD); |
| 179 | head = value->getHead(); |
| 180 | // Attach to new: |
| 181 | if (head != NULL) |
| 182 | head->addAuditor(this, SoNotRec::FIELD); |
| 183 | } |
| 184 | |
| 185 | // Notify if we're supposed to... |
| 186 | if (doNotify) |
| 187 | SoSField::notify(list); |
| 188 | } |
| 189 | |
| 190 | //////////////////////////////////////////////////////////////////////// |
| 191 | // |
| 192 | // Description: |
| 193 | // Reads value from file. Returns FALSE on error. |
| 194 | // |
| 195 | // Use: private |
| 196 | |
| 197 | SbBool |
| 198 | SoSFPath::readValue(SoInput *in) |
| 199 | // |
| 200 | //////////////////////////////////////////////////////////////////////// |
| 201 | { |
| 202 | SbName name; |
| 203 | SoBase *base; |
| 204 | |
| 205 | // See if it's a null pointer |
| 206 | if (in->read(name)) { |
| 207 | if (name == "NULL") { |
| 208 | setVal(NULL); |
| 209 | return TRUE; |
| 210 | } |
| 211 | else |
| 212 | in->putBack(name.getString()); |
| 213 | } |
| 214 | |
| 215 | // Read path |
| 216 | if (! SoBase::read(in, base, SoPath::getClassTypeId())) { |
| 217 | setVal(NULL); |
| 218 | return FALSE; |
| 219 | } |
| 220 | |
| 221 | setVal((SoPath *) base); |
| 222 | |
| 223 | return TRUE; |
| 224 | } |
| 225 | |
| 226 | //////////////////////////////////////////////////////////////////////// |
| 227 | // |
| 228 | // Description: |
| 229 | // Overrides SoField::countWriteRefs() to maintain write-references |
| 230 | // in path. |
| 231 | // |
| 232 | // Use: private |
| 233 | |
| 234 | void |
| 235 | SoSFPath::countWriteRefs(SoOutput *out) const |
| 236 | // |
| 237 | //////////////////////////////////////////////////////////////////////// |
| 238 | { |
| 239 | // Do standard counting for field |
| 240 | SoField::countWriteRefs(out); |
| 241 | |
| 242 | // Count path |
| 243 | if (value != NULL) { |
| 244 | SoWriteAction wa(out); |
| 245 | wa.continueToApply(value); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | //////////////////////////////////////////////////////////////////////// |
| 250 | // |
| 251 | // Description: |
| 252 | // Writes value of field to file. |
| 253 | // |
| 254 | // Use: private |
| 255 | |
| 256 | void |
| 257 | SoSFPath::writeValue(SoOutput *out) const |
| 258 | // |
| 259 | //////////////////////////////////////////////////////////////////////// |
| 260 | { |
| 261 | if (value != NULL) { |
| 262 | SoWriteAction wa(out); |
| 263 | wa.continueToApply(value); |
| 264 | } |
| 265 | else |
| 266 | out->write("NULL"); |
| 267 | } |
| 268 | |
| 269 | //////////////////////////////////////////////////////////////////////// |
| 270 | // |
| 271 | // Description: |
| 272 | // Changes value in field without doing other notification stuff. |
| 273 | // Keeps track of references and auditors. |
| 274 | // |
| 275 | // Use: private |
| 276 | |
| 277 | void |
| 278 | SoSFPath::setVal(SoPath *newValue) |
| 279 | // |
| 280 | //////////////////////////////////////////////////////////////////////// |
| 281 | { |
| 282 | // Play it safe if we are replacing one pointer with the same pointer |
| 283 | if (newValue != NULL) |
| 284 | newValue->ref(); |
| 285 | |
| 286 | // Get rid of old path, if any |
| 287 | if (value != NULL) { |
| 288 | |
| 289 | // Remove auditor on head node |
| 290 | if (head != NULL) |
| 291 | head->removeAuditor(this, SoNotRec::FIELD); |
| 292 | |
| 293 | // Remove auditor on path itself |
| 294 | value->removeAuditor(this, SoNotRec::FIELD); |
| 295 | |
| 296 | value->unref(); |
| 297 | } |
| 298 | |
| 299 | value = newValue; |
| 300 | |
| 301 | if (value != NULL) { |
| 302 | |
| 303 | value->ref(); |
| 304 | |
| 305 | // Add auditor on path |
| 306 | value->addAuditor(this, SoNotRec::FIELD); |
| 307 | |
| 308 | // Add auditor on head node. (For efficiency, a path does not |
| 309 | // audit its head node directly. Things that rely on this |
| 310 | // notification - e.g., this field and path sensors - have to |
| 311 | // do it themselves.) |
| 312 | head = value->getHead(); |
| 313 | if (head != NULL) |
| 314 | head->addAuditor(this, SoNotRec::FIELD); |
| 315 | } |
| 316 | |
| 317 | if (newValue != NULL) |
| 318 | newValue->unref(); |
| 319 | } |
| 320 | |
| 321 | //////////////////////////////////////////////////////////////////////// |
| 322 | // |
| 323 | // Description: |
| 324 | // Update a copied field to use node copies if the head node has |
| 325 | // been copied. |
| 326 | // |
| 327 | // Use: internal |
| 328 | |
| 329 | void |
| 330 | SoSFPath::fixCopy(SbBool copyConnections) |
| 331 | // |
| 332 | //////////////////////////////////////////////////////////////////////// |
| 333 | { |
| 334 | if (value != NULL) { |
| 335 | |
| 336 | // If the head of the path has been copied, then each of the |
| 337 | // other nodes in the path must have been copied as |
| 338 | // well. Change the copied path to start at the copy and go |
| 339 | // through the copied nodes. |
| 340 | |
| 341 | SoNode *headCopy = (SoNode *) |
| 342 | SoFieldContainer::findCopy(value->getHead(), copyConnections); |
| 343 | |
| 344 | if (headCopy != NULL) { |
| 345 | |
| 346 | // Create a new path through the copied nodes |
| 347 | SoPath *pathCopy = new SoPath(headCopy); |
| 348 | pathCopy->ref(); |
| 349 | for (int i = 1; i < ((SoFullPath *) value)->getLength(); i++) |
| 350 | pathCopy->append(value->getIndex(i)); |
| 351 | |
| 352 | setVal(pathCopy); |
| 353 | pathCopy->unref(); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | //////////////////////////////////////////////////////////////////////// |
| 359 | // |
| 360 | // Description: |
| 361 | // Override this to also check the head of the stored path. |
| 362 | // |
| 363 | // Use: internal, virtual |
| 364 | |
| 365 | SbBool |
| 366 | SoSFPath::referencesCopy() const |
| 367 | // |
| 368 | //////////////////////////////////////////////////////////////////////// |
| 369 | { |
| 370 | // Do the normal test, and also see if the head of the stored path |
| 371 | // is a copy |
| 372 | return (SoSField::referencesCopy() || |
| 373 | (value != NULL && |
| 374 | SoFieldContainer::checkCopy(value->getHead()) != NULL)); |
| 375 | } |