| 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 | | SoSFNode |
| 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/nodes/SoNode.h> |
| 56 | #include <Inventor/actions/SoWriteAction.h> |
| 57 | #include <Inventor/fields/SoSFNode.h> |
| 58 | |
| 59 | ////////////////////////////////////////////////////////////////////////////// |
| 60 | // |
| 61 | // SoSFNode class - contains a pointer to an SoNode. This has to |
| 62 | // implement some extra node 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(SoSFNode); |
| 70 | |
| 71 | //////////////////////////////////////////////////////////////////////// |
| 72 | // |
| 73 | // Description: |
| 74 | // Constructor |
| 75 | // |
| 76 | // Use: public |
| 77 | |
| 78 | SoSFNode::SoSFNode() |
| 79 | // |
| 80 | //////////////////////////////////////////////////////////////////////// |
| 81 | { |
| 82 | // This will prevent treating random memory as an SoNode |
| 83 | value = NULL; |
| 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////// |
| 87 | // |
| 88 | // Description: |
| 89 | // Destructor |
| 90 | // |
| 91 | // Use: public |
| 92 | |
| 93 | SoSFNode::~SoSFNode() |
| 94 | // |
| 95 | //////////////////////////////////////////////////////////////////////// |
| 96 | { |
| 97 | if (value != NULL) { |
| 98 | value->removeAuditor(this, SoNotRec::FIELD); |
| 99 | value->unref(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | //////////////////////////////////////////////////////////////////////// |
| 104 | // |
| 105 | // Description: |
| 106 | // Sets value to given node pointer. |
| 107 | // |
| 108 | // Use: public |
| 109 | |
| 110 | void |
| 111 | SoSFNode::setValue(SoNode *newValue) |
| 112 | // |
| 113 | //////////////////////////////////////////////////////////////////////// |
| 114 | { |
| 115 | setVal(newValue); |
| 116 | valueChanged(); |
| 117 | } |
| 118 | |
| 119 | //////////////////////////////////////////////////////////////////////// |
| 120 | // |
| 121 | // Description: |
| 122 | // Equality test. (Standard definition.) |
| 123 | // |
| 124 | // Use: public |
| 125 | |
| 126 | int |
| 127 | SoSFNode::operator ==(const SoSFNode &f) const |
| 128 | // |
| 129 | //////////////////////////////////////////////////////////////////////// |
| 130 | { |
| 131 | return getValue() == f.getValue(); |
| 132 | } |
| 133 | |
| 134 | //////////////////////////////////////////////////////////////////////// |
| 135 | // |
| 136 | // Description: |
| 137 | // Reads value from file. Returns FALSE on error. |
| 138 | // |
| 139 | // Use: private |
| 140 | |
| 141 | SbBool |
| 142 | SoSFNode::readValue(SoInput *in) |
| 143 | // |
| 144 | //////////////////////////////////////////////////////////////////////// |
| 145 | { |
| 146 | SbName name; |
| 147 | SoBase *base; |
| 148 | |
| 149 | // See if it's a null pointer |
| 150 | if (in->read(name)) { |
| 151 | if (name == "NULL") { |
| 152 | setVal(NULL); |
| 153 | return TRUE; |
| 154 | } |
| 155 | else |
| 156 | in->putBack(name.getString()); |
| 157 | } |
| 158 | |
| 159 | // Read node |
| 160 | if (! SoBase::read(in, base, SoNode::getClassTypeId())) { |
| 161 | setVal(NULL); |
| 162 | return FALSE; |
| 163 | } |
| 164 | |
| 165 | setVal((SoNode *) base); |
| 166 | |
| 167 | return TRUE; |
| 168 | } |
| 169 | |
| 170 | //////////////////////////////////////////////////////////////////////// |
| 171 | // |
| 172 | // Description: |
| 173 | // Overrides SoField::countWriteRefs() to maintain write-references |
| 174 | // in node. |
| 175 | // |
| 176 | // Use: private |
| 177 | |
| 178 | void |
| 179 | SoSFNode::countWriteRefs(SoOutput *out) const |
| 180 | // |
| 181 | //////////////////////////////////////////////////////////////////////// |
| 182 | { |
| 183 | // Do standard counting for field |
| 184 | SoField::countWriteRefs(out); |
| 185 | |
| 186 | // Count node |
| 187 | if (value != NULL) |
| 188 | value->writeInstance(out); |
| 189 | } |
| 190 | |
| 191 | //////////////////////////////////////////////////////////////////////// |
| 192 | // |
| 193 | // Description: |
| 194 | // Writes value of field to file. |
| 195 | // |
| 196 | // Use: private |
| 197 | |
| 198 | void |
| 199 | SoSFNode::writeValue(SoOutput *out) const |
| 200 | // |
| 201 | //////////////////////////////////////////////////////////////////////// |
| 202 | { |
| 203 | if (value != NULL) |
| 204 | value->writeInstance(out); |
| 205 | else |
| 206 | out->write("NULL"); |
| 207 | } |
| 208 | |
| 209 | //////////////////////////////////////////////////////////////////////// |
| 210 | // |
| 211 | // Description: |
| 212 | // Changes value in field without doing other notification stuff. |
| 213 | // Keeps track of references and auditors. |
| 214 | // |
| 215 | // Use: private |
| 216 | |
| 217 | void |
| 218 | SoSFNode::setVal(SoNode *newValue) |
| 219 | // |
| 220 | //////////////////////////////////////////////////////////////////////// |
| 221 | { |
| 222 | // Play it safe if we are replacing one pointer with the same pointer |
| 223 | if (newValue != NULL) |
| 224 | newValue->ref(); |
| 225 | |
| 226 | // Get rid of old node, if any |
| 227 | if (value != NULL) { |
| 228 | value->removeAuditor(this, SoNotRec::FIELD); |
| 229 | value->unref(); |
| 230 | } |
| 231 | |
| 232 | value = newValue; |
| 233 | |
| 234 | if (value != NULL) { |
| 235 | value->ref(); |
| 236 | value->addAuditor(this, SoNotRec::FIELD); |
| 237 | } |
| 238 | |
| 239 | if (newValue != NULL) |
| 240 | newValue->unref(); |
| 241 | } |
| 242 | |
| 243 | //////////////////////////////////////////////////////////////////////// |
| 244 | // |
| 245 | // Description: |
| 246 | // Update a copied field to use the copy of the node if there is |
| 247 | // one. |
| 248 | // |
| 249 | // Use: internal |
| 250 | |
| 251 | void |
| 252 | SoSFNode::fixCopy(SbBool copyConnections) |
| 253 | // |
| 254 | //////////////////////////////////////////////////////////////////////// |
| 255 | { |
| 256 | if (value != NULL) { |
| 257 | SoNode *nodeCopy = (SoNode *) |
| 258 | SoFieldContainer::findCopy(value, copyConnections); |
| 259 | if (nodeCopy != NULL) |
| 260 | setVal(nodeCopy); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | //////////////////////////////////////////////////////////////////////// |
| 265 | // |
| 266 | // Description: |
| 267 | // Override this to also check the stored node. |
| 268 | // |
| 269 | // Use: internal, virtual |
| 270 | |
| 271 | SbBool |
| 272 | SoSFNode::referencesCopy() const |
| 273 | // |
| 274 | //////////////////////////////////////////////////////////////////////// |
| 275 | { |
| 276 | // Do the normal test, and also see if the stored node is a copy |
| 277 | return (SoSField::referencesCopy() || |
| 278 | (value != NULL && SoFieldContainer::checkCopy(value) != NULL)); |
| 279 | } |