Referenced
Go to the documentation of this file.
/* -*-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.
*/
#ifndef OSG_REFERENCED
#define OSG_REFERENCED 1
#include <osg/Export>
#include <OpenThreads/ScopedLock>
#include <OpenThreads/Mutex>
#include <OpenThreads/Atomic>
#if !defined(_OPENTHREADS_ATOMIC_USE_MUTEX)
# define _OSG_REFERENCED_USE_ATOMIC_OPERATIONS
#endif
namespace osg {
// forward declare, declared after Referenced below.
class DeleteHandler;
class Observer;
class ObserverSet;
class State;
template <typename T, T M()>
struct depends_on
{
depends_on() { M(); }
};
class OSG_EXPORT Referenced
{
public:
Referenced();
explicit Referenced(bool threadSafeRefUnref);
Referenced(const Referenced&);
inline Referenced& operator = (const Referenced&) { return *this; }
virtual void setThreadSafeRefUnref(bool /*threadSafe*/) {}
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
bool getThreadSafeRefUnref() const { return true; }
#else
bool getThreadSafeRefUnref() const { return _refMutex!=0; }
#endif
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
OpenThreads::Mutex* getRefMutex() const { return getGlobalReferencedMutex(); }
#else
OpenThreads::Mutex* getRefMutex() const { return _refMutex; }
#endif
static OpenThreads::Mutex* getGlobalReferencedMutex();
inline int ref() const;
inline int unref() const;
int unref_nodelete() const;
inline int referenceCount() const { return _refCount; }
ObserverSet* getObserverSet() const
{
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
return static_cast<ObserverSet*>(_observerSet.get());
#else
return static_cast<ObserverSet*>(_observerSet);
#endif
}
ObserverSet* getOrCreateObserverSet() const;
void addObserver(Observer* observer) const;
void removeObserver(Observer* observer) const;
#if 0
virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
virtual void releaseGLObjects(osg::State* = 0) const {}
#endif
public:
friend class DeleteHandler;
static void setDeleteHandler(DeleteHandler* handler);
static DeleteHandler* getDeleteHandler();
protected:
virtual ~Referenced();
void signalObserversAndDelete(bool signalDelete, bool doDelete) const;
void deleteUsingDeleteHandler() const;
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
mutable OpenThreads::AtomicPtr _observerSet;
mutable OpenThreads::Atomic _refCount;
#else
mutable OpenThreads::Mutex* _refMutex;
mutable int _refCount;
mutable void* _observerSet;
#endif
};
inline int Referenced::ref() const
{
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
return ++_refCount;
#else
if (_refMutex)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
return ++_refCount;
}
else
{
return ++_refCount;
}
#endif
}
inline int Referenced::unref() const
{
int newRef;
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
newRef = --_refCount;
bool needDelete = (newRef == 0);
#else
bool needDelete = false;
if (_refMutex)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
newRef = --_refCount;
needDelete = newRef==0;
}
else
{
newRef = --_refCount;
needDelete = newRef==0;
}
#endif
if (needDelete)
{
signalObserversAndDelete(true,true);
}
return newRef;
}
// intrusive_ptr_add_ref and intrusive_ptr_release allow
// use of osg Referenced classes with boost::intrusive_ptr
inline void intrusive_ptr_add_ref(Referenced* p) { p->ref(); }
inline void intrusive_ptr_release(Referenced* p) { p->unref(); }
}
#endif
Atomic
Mutex
ScopedLock
OpenThreads::AtomicPtr
This class provides an atomic pointer assignment using cas operations.
Definition Atomic:83
OpenThreads::Atomic
This class provides an atomic increment and decrement operation.
Definition Atomic:48
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
OpenThreads::ScopedLock
Definition ScopedLock:25
osg::ObserverSet
Definition Observer:39
osg::Observer
Definition Observer:25
osg::Referenced
Definition Referenced:44
osg::Referenced::Referenced
Referenced(bool threadSafeRefUnref)
osg::Referenced::unref_nodelete
int unref_nodelete() const
osg::Referenced::addObserver
void addObserver(Observer *observer) const
osg::Referenced::signalObserversAndDelete
void signalObserversAndDelete(bool signalDelete, bool doDelete) const
osg::Referenced::_observerSet
OpenThreads::AtomicPtr _observerSet
Definition Referenced:148
osg::Referenced::~Referenced
virtual ~Referenced()
osg::Referenced::Referenced
Referenced(const Referenced &)
osg::Referenced::unref
int unref() const
Definition Referenced:178
osg::Referenced::getDeleteHandler
static DeleteHandler * getDeleteHandler()
osg::Referenced::Referenced
Referenced()
osg::Referenced::deleteUsingDeleteHandler
void deleteUsingDeleteHandler() const
osg::Referenced::getObserverSet
ObserverSet * getObserverSet() const
Definition Referenced:101
osg::Referenced::getThreadSafeRefUnref
bool getThreadSafeRefUnref() const
Definition Referenced:63
osg::Referenced::setThreadSafeRefUnref
virtual void setThreadSafeRefUnref(bool)
Definition Referenced:59
osg::Referenced::referenceCount
int referenceCount() const
Definition Referenced:97
osg::Referenced::_refCount
OpenThreads::Atomic _refCount
Definition Referenced:150
osg::Referenced::getRefMutex
OpenThreads::Mutex * getRefMutex() const
Definition Referenced:70
osg::Referenced::removeObserver
void removeObserver(Observer *observer) const
osg::Referenced::getGlobalReferencedMutex
static OpenThreads::Mutex * getGlobalReferencedMutex()
osg::Referenced::ref
int ref() const
Definition Referenced:161
osg::Referenced::DeleteHandler
friend class DeleteHandler
Definition Referenced:129
osg::Referenced::setDeleteHandler
static void setDeleteHandler(DeleteHandler *handler)
osg::Referenced::getOrCreateObserverSet
ObserverSet * getOrCreateObserverSet() const
osg::State
Definition State:80
osg
Definition AlphaFunc:19
osg::resizeGLObjectBuffers
void resizeGLObjectBuffers(osg::Object *object, unsigned int maxSize)
Definition Object:396
osg::intrusive_ptr_add_ref
void intrusive_ptr_add_ref(Referenced *p)
Definition Referenced:208
osg::releaseGLObjects
void releaseGLObjects(osg::Object *object, osg::State *state=0)
Definition Object:400
osg::intrusive_ptr_release
void intrusive_ptr_release(Referenced *p)
Definition Referenced:209
Export
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39
osg::depends_on::depends_on
depends_on()
Definition Referenced:39