2010-02-16 04:12:53 +08:00
|
|
|
/* -*-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.
|
|
|
|
*/
|
|
|
|
|
2010-02-19 05:21:12 +08:00
|
|
|
#ifndef OSG_OBSERVER
|
|
|
|
#define OSG_OBSERVER 1
|
|
|
|
|
|
|
|
#include <OpenThreads/Mutex>
|
2010-05-20 21:38:28 +08:00
|
|
|
#include <osg/Referenced>
|
2010-02-19 05:21:12 +08:00
|
|
|
#include <set>
|
2010-02-16 04:12:53 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2012-03-29 23:08:15 +08:00
|
|
|
/** Observer base class for tracking when objects are unreferenced (their reference count goes to 0) and are being deleted.*/
|
2010-02-19 05:21:12 +08:00
|
|
|
class OSG_EXPORT Observer
|
|
|
|
{
|
|
|
|
public:
|
2010-02-20 04:18:48 +08:00
|
|
|
Observer();
|
|
|
|
virtual ~Observer();
|
2010-02-19 05:21:12 +08:00
|
|
|
|
|
|
|
/** objectDeleted is called when the observed object is about to be deleted. The observer will be automatically
|
2012-03-29 23:08:15 +08:00
|
|
|
* removed from the observed object's observer set so there is no need for the objectDeleted implementation
|
2010-02-19 05:21:12 +08:00
|
|
|
* to call removeObserver() on the observed object. */
|
|
|
|
virtual void objectDeleted(void*) {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-06-01 17:21:42 +08:00
|
|
|
/** Class used by osg::Referenced to track the observers associated with it.*/
|
2010-05-20 21:38:28 +08:00
|
|
|
class OSG_EXPORT ObserverSet : public osg::Referenced
|
2010-02-16 04:12:53 +08:00
|
|
|
{
|
2010-02-19 05:21:12 +08:00
|
|
|
public:
|
|
|
|
|
2010-05-20 21:38:28 +08:00
|
|
|
ObserverSet(const Referenced* observedObject);
|
|
|
|
|
|
|
|
Referenced* getObserverdObject() { return _observedObject; }
|
|
|
|
const Referenced* getObserverdObject() const { return _observedObject; }
|
|
|
|
|
|
|
|
/** "Lock" a Referenced object i.e., protect it from being deleted
|
|
|
|
* by incrementing its reference count.
|
|
|
|
*
|
|
|
|
* returns null if object doesn't exist anymore. */
|
|
|
|
Referenced* addRefLock();
|
2010-02-19 05:21:12 +08:00
|
|
|
|
2010-05-14 20:24:13 +08:00
|
|
|
inline OpenThreads::Mutex* getObserverSetMutex() const { return &_mutex; }
|
2010-02-19 05:21:12 +08:00
|
|
|
|
|
|
|
void addObserver(Observer* observer);
|
|
|
|
void removeObserver(Observer* observer);
|
|
|
|
|
|
|
|
void signalObjectDeleted(void* ptr);
|
|
|
|
|
|
|
|
typedef std::set<Observer*> Observers;
|
|
|
|
Observers& getObservers() { return _observers; }
|
|
|
|
const Observers& getObservers() const { return _observers; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2010-07-31 18:22:37 +08:00
|
|
|
ObserverSet(const ObserverSet& rhs): osg::Referenced(rhs) {}
|
2010-11-25 20:07:59 +08:00
|
|
|
ObserverSet& operator = (const ObserverSet& /*rhs*/) { return *this; }
|
2010-05-20 21:38:28 +08:00
|
|
|
virtual ~ObserverSet();
|
2010-05-18 02:37:32 +08:00
|
|
|
|
2010-05-14 20:24:13 +08:00
|
|
|
mutable OpenThreads::Mutex _mutex;
|
2010-05-20 21:38:28 +08:00
|
|
|
Referenced* _observedObject;
|
2010-05-14 20:24:13 +08:00
|
|
|
Observers _observers;
|
2010-02-16 04:12:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-02-19 06:53:15 +08:00
|
|
|
#endif
|