/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include #include using namespace osg; Observer::Observer() { } Observer::~Observer() { } ObserverSet::ObserverSet(const Referenced* observedObject): _observedObject(const_cast(observedObject)) { //OSG_NOTICE<<"ObserverSet::ObserverSet() "< lock(_mutex); _observers.insert(observer); } void ObserverSet::removeObserver(Observer* observer) { //OSG_NOTICE<<"ObserverSet::removeObserver("< lock(_mutex); _observers.erase(observer); } Referenced* ObserverSet::addRefLock() { OpenThreads::ScopedLock lock(_mutex); if (!_observedObject) return 0; int refCount = _observedObject->ref(); if (refCount == 1) { // The object is in the process of being deleted, but our // objectDeleted() method hasn't been run yet (and we're // blocking it -- and the final destruction -- with our lock). _observedObject->unref_nodelete(); return 0; } return _observedObject; } void ObserverSet::signalObjectDeleted(void* ptr) { OpenThreads::ScopedLock lock(_mutex); for(Observers::iterator itr = _observers.begin(); itr != _observers.end(); ++itr) { (*itr)->objectDeleted(ptr); } _observers.clear(); // reset the observed object so that we know that it's now detached. _observedObject = 0; }