1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
/*!
\class SoChildList SoChildList.h Inventor/misc/SoChildList.h
\brief The SoChildList class is a container for node children.
\ingroup general
This class does automatic notification on the parent nodes upon
adding or removing children.
Methods for action traversal of the children are also provided.
*/
#include <Inventor/misc/SoChildList.h>
#include <Inventor/actions/SoAction.h>
#include <Inventor/nodes/SoNode.h>
#include <Inventor/SbName.h>
#if COIN_DEBUG
#include <Inventor/errors/SoDebugError.h>
#endif // COIN_DEBUG
/*!
Default constructor, sets parent container and initializes a minimal
list.
*/
SoChildList::SoChildList(SoNode * const parentptr)
: SoNodeList()
{
this->parent = parentptr;
}
/*!
Constructor with hint about list size.
\sa SoNodeList::SoNodeList(const int)
*/
SoChildList::SoChildList(SoNode * const parentptr, const int size)
: SoNodeList(size)
{
this->parent = parentptr;
}
/*!
Copy constructor.
\sa SoNodeList::SoNodeList(const SoNodeList &)
*/
SoChildList::SoChildList(SoNode * const parentptr, const SoChildList & cl)
: SoNodeList()
{
this->parent = parentptr;
this->copy(cl);
}
/*!
Destructor.
*/
SoChildList::~SoChildList()
{
this->truncate(0);
}
/*!
Append a new \a node instance as a child of our parent container.
Automatically notifies parent node and any SoPath instances auditing
paths with nodes from this list.
*/
void
SoChildList::append(SoNode * const node)
{
if (this->parent) {
node->addAuditor(this->parent, SoNotRec::PARENT);
}
SoNodeList::append(node);
if (this->parent) {
this->parent->startNotify();
}
// Doesn't need to notify SoPath auditors, as adding a new node at
// _the end_ won't affect any path "passing through" this childlist.
}
/*!
Insert a new \a node instance as a child of our parent container at
position \a addbefore.
Automatically notifies parent node and any SoPath instances auditing
paths with nodes from this list.
*/
void
SoChildList::insert(SoNode * const node, const int addbefore)
{
assert(addbefore <= this->getLength());
if (this->parent) {
node->addAuditor(this->parent, SoNotRec::PARENT);
}
SoNodeList::insert(node, addbefore);
// FIXME: shouldn't we move this startNotify() call to the end of
// the function? pederb, 2002-10-02
if (this->parent) {
this->parent->startNotify();
for (int i=0; i < this->auditors.getLength(); i++) {
this->auditors[i]->insertIndex(this->parent, addbefore);
}
}
}
/*!
Remove the child node pointer at \a index.
Automatically notifies parent node and any SoPath instances auditing
paths with nodes from this list.
*/
void
SoChildList::remove(const int index)
{
assert(index >= 0 && index < this->getLength());
if (this->parent) {
SoNodeList::operator[](index)->removeAuditor(this->parent, SoNotRec::PARENT);
}
// FIXME: we experienced memory corruption if the
// SoNodeList::remove(index) statement was placed here (before
// updating paths). It seems to be working ok now, but we should
// figure out exactly why we can't remove the node before updating
// the paths. pederb, 2002-10-02
if (this->parent) {
for (int i=0; i < this->auditors.getLength(); i++) {
this->auditors[i]->removeIndex(this->parent, index);
}
}
SoNodeList::remove(index);
if (this->parent) {
this->parent->startNotify();
}
}
// Documented in superclass. Overridden to handle notification.
void
SoChildList::truncate(const int length)
{
const int n = this->getLength();
assert(length >= 0 && length <= n);
if (length != n) {
if (this->parent) {
for (int i = length; i < n; i++) {
SoNodeList::operator[](i)->removeAuditor(this->parent, SoNotRec::PARENT);
}
}
SoNodeList::truncate(length);
// FIXME: shouldn't we move this startNotify() call to the end of
// the function? pederb, 2002-10-02
if (this->parent) {
this->parent->startNotify();
for (int k=0; k < this->auditors.getLength(); k++) {
for (int j=length; j < n; j++) {
this->auditors[k]->removeIndex(this->parent, j);
}
}
}
}
}
/*!
Copy contents of \a cl into this list.
*/
void
SoChildList::copy(const SoChildList & cl)
{
// Overridden from superclass to handle notification.
if (this == &cl) return;
// Call truncate() explicitly here to get the path notification.
this->truncate(0);
SoBaseList::copy(cl);
// it's important to add parent as auditor for all nodes (this is
// usually done in SoChildList::append/insert)
if (this->parent) {
for (int i = 0; i < this->getLength(); i++) {
(*this)[i]->addAuditor(this->parent, SoNotRec::PARENT);
}
this->parent->startNotify();
}
}
/*!
Index operator to set element at \a index. Does \e not expand array
bounds if \a index is outside the list.
*/
void
SoChildList::set(const int index, SoNode * const node)
{
// Overridden from superclass to handle notification.
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoChildList::set",
"(%p) index=%d, node=%p, oldnode=%p",
this, index, node, (*this)[index]);
#endif // debug
assert(index >= 0 && index < this->getLength());
if (this->parent) {
SoNodeList::operator[](index)->removeAuditor(this->parent, SoNotRec::PARENT);
}
SoBaseList::set(index, (SoBase *)node);
if (this->parent) {
node->addAuditor(this->parent, SoNotRec::PARENT);
}
// FIXME: shouldn't we move this startNotify() call to the end of
// the function? pederb, 2002-10-02
if (this->parent) {
this->parent->startNotify();
for (int i=0; i < this->auditors.getLength(); i++)
this->auditors[i]->replaceIndex(this->parent, index, node);
}
}
/*!
Optimized IN_PATH traversal method.
This method is an extension versus the Open Inventor API.
*/
void
SoChildList::traverseInPath(SoAction * const action,
const int numindices,
const int * indices)
{
assert(action->getCurPathCode() == SoAction::IN_PATH);
// only traverse nodes in path list, and nodes off path that
// affects state.
int childidx = 0;
for (int i = 0; i < numindices && !action->hasTerminated(); i++) {
int stop = indices[i];
for (; childidx < stop && !action->hasTerminated(); childidx++) {
// we are off path. Check if node affects state before traversing
SoNode * node = (*this)[childidx];
if (node->affectsState()) {
action->pushCurPath(childidx, node);
action->traverse(node);
action->popCurPath(SoAction::IN_PATH);
}
}
if (!action->hasTerminated()) {
// here we are in path. Always traverse
SoNode * node = (*this)[childidx];
action->pushCurPath(childidx, node);
action->traverse(node);
action->popCurPath(SoAction::IN_PATH);
childidx++;
}
}
}
/*!
Traverse child nodes in the list from index \a first up to and
including index \a last, or until the SoAction::hasTerminated() flag
of \a action has been set.
*/
void
SoChildList::traverse(SoAction * const action, const int first, const int last)
{
int i;
SoNode * node = NULL;
assert((first >= 0) && (first < this->getLength()) && "index out of bounds");
assert((last >= 0) && (last < this->getLength()) && "index out of bounds");
assert((last >= first) && "erroneous indices");
#if COIN_DEBUG
// Calculate a checksum over the children node pointers, to later
// catch attempts at changing the scene graph layout mid-traversal
// with an assert. (chksum reversed to initial value and controlled
// at the bottom end of this function.)
//
// Note: we might find this to be overly strict, because there are
// cases where this will stop an unharmful attempt at changing the
// current group node's set of children. But that's only if the
// application programmer _really_, _really_ know what he is doing,
// and it's still a slippery slope.. so "better safe than sorry" and
// all that.
//
// mortene.
uintptr_t chksum = 0xdeadbeef;
for (i = first; i <= last; i++) { chksum ^= (uintptr_t)(*this)[i]; }
SbBool changedetected = FALSE;
#endif // COIN_DEBUG
SoAction::PathCode pathcode = action->getCurPathCode();
switch (pathcode) {
case SoAction::NO_PATH:
case SoAction::BELOW_PATH:
// always traverse all nodes.
action->pushCurPath();
for (i = first; (i <= last) && !action->hasTerminated(); i++) {
#if COIN_DEBUG
if (i >= this->getLength()) {
changedetected = TRUE;
break;
}
#endif // COIN_DEBUG
node = (*this)[i];
action->popPushCurPath(i, node);
action->traverse(node);
}
action->popCurPath();
break;
case SoAction::OFF_PATH:
for (i = first; (i <= last) && !action->hasTerminated(); i++) {
#if COIN_DEBUG
if (i >= this->getLength()) {
changedetected = TRUE;
break;
}
#endif // COIN_DEBUG
node = (*this)[i];
// only traverse nodes that affects state
if (node->affectsState()) {
action->pushCurPath(i, node);
action->traverse(node);
action->popCurPath(pathcode);
}
}
break;
case SoAction::IN_PATH:
for (i = first; (i <= last) && !action->hasTerminated(); i++) {
#if COIN_DEBUG
if (i >= this->getLength()) {
changedetected = TRUE;
break;
}
#endif // COIN_DEBUG
node = (*this)[i];
action->pushCurPath(i, node);
// if we're OFF_PATH after pushing, we only traverse if the node
// affects the state.
if ((action->getCurPathCode() != SoAction::OFF_PATH) ||
node->affectsState()) {
action->traverse(node);
}
action->popCurPath(pathcode);
}
break;
default:
assert(0 && "unknown path code.");
break;
}
#if COIN_DEBUG
if (!changedetected) {
for (i = last; i >= first; i--) { chksum ^= (uintptr_t)(*this)[i]; }
if (chksum != 0xdeadbeef) changedetected = TRUE;
}
if (changedetected) {
SoDebugError::postWarning("SoChildList::traverse",
"Detected modification of scene graph layout "
"during action traversal. This is considered to "
"be hazardous and error prone, and we "
"strongly advice you to change your code "
"and/or reorganize your scene graph so that "
"this is not necessary.");
}
#endif // COIN_DEBUG
}
/*!
Traverse all nodes in the list, invoking their methods for the given
\a action.
*/
void
SoChildList::traverse(SoAction * const action)
{
if (this->getLength() == 0) return;
this->traverse(action, 0, this->getLength() - 1);
}
/*!
Traverse the node at \a index (and possibly its children, if its a
group node), applying the nodes' method for the given \a action.
*/
void
SoChildList::traverse(SoAction * const action, const int index)
{
assert((index >= 0) && (index < this->getLength()) && "index out of bounds");
this->traverse(action, index, index);
}
/*!
Traverse the \a node (and possibly its children, if its a group
node), applying the nodes' method for the given \a action.
*/
void
SoChildList::traverse(SoAction * const action, SoNode * node)
{
int idx = this->find(node);
assert(idx != -1);
this->traverse(action, idx);
}
/*!
Notify \a path whenever this list of node children changes.
*/
void
SoChildList::addPathAuditor(SoPath * const path)
{
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoChildList::addPathAuditor",
"add SoPath auditor %p to list %p", path, this);
#endif // debug
this->auditors.append(path);
}
/*!
Remove \a path as an auditor for our list of node children.
*/
void
SoChildList::removePathAuditor(SoPath * const path)
{
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoChildList::removePathAuditor",
"remove SoPath auditor %p from list %p", path, this);
#endif // debug
const int index = this->auditors.find(path);
#if COIN_DEBUG
if (index == -1) {
SoDebugError::post("SoChildList::removePathAuditor",
"no SoPath %p is auditing list %p! (of parent %p (%s))",
path,
this,
this->parent,
this->parent ? this->parent->getTypeId().getName().getString() : "<no type>");
return;
}
#endif // COIN_DEBUG
this->auditors.remove(index);
}
|