2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_REFERENCED
|
|
|
|
#define OSG_REFERENCED 1
|
|
|
|
|
2010-05-20 21:38:28 +08:00
|
|
|
#include <osg/Export>
|
2005-04-12 01:14:17 +08:00
|
|
|
|
2005-02-23 20:50:10 +08:00
|
|
|
#include <OpenThreads/ScopedLock>
|
2004-09-27 22:15:13 +08:00
|
|
|
#include <OpenThreads/Mutex>
|
2008-06-20 01:30:38 +08:00
|
|
|
#include <OpenThreads/Atomic>
|
2010-05-20 21:38:28 +08:00
|
|
|
|
2008-06-20 01:30:38 +08:00
|
|
|
#if !defined(_OPENTHREADS_ATOMIC_USE_MUTEX)
|
|
|
|
# define _OSG_REFERENCED_USE_ATOMIC_OPERATIONS
|
|
|
|
#endif
|
2007-02-02 20:41:13 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
namespace osg {
|
|
|
|
|
2006-02-28 03:44:33 +08:00
|
|
|
// forward declare, declared after Referenced below.
|
2002-12-16 18:25:31 +08:00
|
|
|
class DeleteHandler;
|
2006-02-28 03:44:33 +08:00
|
|
|
class Observer;
|
2010-02-19 05:21:12 +08:00
|
|
|
class ObserverSet;
|
2019-01-09 03:32:50 +08:00
|
|
|
class State;
|
2002-12-16 18:25:31 +08:00
|
|
|
|
2009-06-05 19:48:35 +08:00
|
|
|
/** template class to help enforce static initialization order. */
|
|
|
|
template <typename T, T M()>
|
|
|
|
struct depends_on
|
|
|
|
{
|
|
|
|
depends_on() { M(); }
|
|
|
|
};
|
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
/** Base class for providing reference counted objects.*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Referenced
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2004-09-27 22:15:13 +08:00
|
|
|
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
Referenced();
|
|
|
|
|
2018-04-21 00:18:22 +08:00
|
|
|
/** Deprecated, Referenced is now always uses thread safe ref/unref, use default Referenced() constructor instead */
|
2012-03-22 01:36:20 +08:00
|
|
|
explicit Referenced(bool threadSafeRefUnref);
|
2006-05-02 23:52:46 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
Referenced(const Referenced&);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2003-10-17 18:26:31 +08:00
|
|
|
inline Referenced& operator = (const Referenced&) { return *this; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2016-09-05 22:35:54 +08:00
|
|
|
/** Deprecated, Referenced is always theadsafe so there method now has no effect and does not need to be called.*/
|
|
|
|
virtual void setThreadSafeRefUnref(bool /*threadSafe*/) {}
|
2002-12-16 18:25:31 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
/** Get whether a mutex is used to ensure ref() and unref() are thread safe.*/
|
2008-06-20 01:30:38 +08:00
|
|
|
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
|
|
|
bool getThreadSafeRefUnref() const { return true; }
|
|
|
|
#else
|
2005-02-23 04:25:58 +08:00
|
|
|
bool getThreadSafeRefUnref() const { return _refMutex!=0; }
|
2008-06-20 01:30:38 +08:00
|
|
|
#endif
|
2002-12-16 18:25:31 +08:00
|
|
|
|
2007-09-01 04:14:36 +08:00
|
|
|
/** Get the mutex used to ensure thread safety of ref()/unref(). */
|
2008-06-20 01:30:38 +08:00
|
|
|
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
2008-10-14 22:27:41 +08:00
|
|
|
OpenThreads::Mutex* getRefMutex() const { return getGlobalReferencedMutex(); }
|
2008-06-20 01:30:38 +08:00
|
|
|
#else
|
2007-09-01 04:14:36 +08:00
|
|
|
OpenThreads::Mutex* getRefMutex() const { return _refMutex; }
|
2008-06-20 01:30:38 +08:00
|
|
|
#endif
|
2007-09-01 04:14:36 +08:00
|
|
|
|
2008-10-14 22:27:41 +08:00
|
|
|
/** Get the optional global Referenced mutex, this can be shared between all osg::Referenced.*/
|
|
|
|
static OpenThreads::Mutex* getGlobalReferencedMutex();
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Increment the reference count by one, indicating that
|
2001-01-11 00:32:10 +08:00
|
|
|
this object has another pointer which is referencing it.*/
|
2010-05-14 20:14:14 +08:00
|
|
|
inline int ref() const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
/** Decrement the reference count by one, indicating that
|
2011-12-24 00:14:51 +08:00
|
|
|
a pointer to this object is no longer referencing it. If the
|
2001-10-01 19:15:55 +08:00
|
|
|
reference count goes to zero, it is assumed that this object
|
|
|
|
is no longer referenced and is automatically deleted.*/
|
2010-05-14 20:14:14 +08:00
|
|
|
inline int unref() const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
/** Decrement the reference count by one, indicating that
|
2011-12-24 00:14:51 +08:00
|
|
|
a pointer to this object is no longer referencing it. However, do
|
2012-03-22 01:36:20 +08:00
|
|
|
not delete it, even if ref count goes to 0. Warning, unref_nodelete()
|
2002-03-20 22:03:30 +08:00
|
|
|
should only be called if the user knows exactly who will
|
2012-03-22 01:36:20 +08:00
|
|
|
be responsible for, one should prefer unref() over unref_nodelete()
|
2010-09-15 20:00:12 +08:00
|
|
|
as the latter can lead to memory leaks.*/
|
2010-05-17 22:21:53 +08:00
|
|
|
int unref_nodelete() const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
/** Return the number of pointers currently referencing this object. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline int referenceCount() const { return _refCount; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2010-02-19 05:21:12 +08:00
|
|
|
|
|
|
|
/** Get the ObserverSet if one is attached, otherwise return NULL.*/
|
|
|
|
ObserverSet* getObserverSet() const
|
|
|
|
{
|
|
|
|
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
|
|
|
return static_cast<ObserverSet*>(_observerSet.get());
|
|
|
|
#else
|
|
|
|
return static_cast<ObserverSet*>(_observerSet);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the ObserverSet if one is attached, otherwise create an ObserverSet, attach it, then return this newly created ObserverSet.*/
|
|
|
|
ObserverSet* getOrCreateObserverSet() const;
|
2006-02-28 03:44:33 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
/** Add a Observer that is observing this object, notify the Observer when this object gets deleted.*/
|
2010-05-20 21:38:28 +08:00
|
|
|
void addObserver(Observer* observer) const;
|
2010-02-19 05:21:12 +08:00
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
/** Remove Observer that is observing this object.*/
|
2010-05-20 21:38:28 +08:00
|
|
|
void removeObserver(Observer* observer) const;
|
2019-01-12 19:27:18 +08:00
|
|
|
#if 0
|
2019-01-09 03:32:50 +08:00
|
|
|
/** Resize any per context GLObject buffers to specified size. */
|
|
|
|
virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
|
|
|
|
|
|
|
|
/** If State is non-zero, this function releases any associated OpenGL objects for
|
|
|
|
* the specified graphics context. Otherwise, releases OpenGL objects
|
|
|
|
* for all graphics contexts. */
|
|
|
|
virtual void releaseGLObjects(osg::State* = 0) const {}
|
2019-01-12 19:27:18 +08:00
|
|
|
#endif
|
2019-01-09 03:32:50 +08:00
|
|
|
public:
|
2004-09-27 22:15:13 +08:00
|
|
|
|
|
|
|
friend class DeleteHandler;
|
|
|
|
|
|
|
|
/** Set a DeleteHandler to which deletion of all referenced counted objects
|
2010-09-15 20:00:12 +08:00
|
|
|
* will be delegated.*/
|
2004-09-27 22:15:13 +08:00
|
|
|
static void setDeleteHandler(DeleteHandler* handler);
|
|
|
|
|
|
|
|
/** Get a DeleteHandler.*/
|
|
|
|
static DeleteHandler* getDeleteHandler();
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-02-07 09:11:20 +08:00
|
|
|
virtual ~Referenced();
|
2010-02-16 04:12:53 +08:00
|
|
|
|
2010-05-17 22:21:53 +08:00
|
|
|
void signalObserversAndDelete(bool signalDelete, bool doDelete) const;
|
2010-02-16 04:12:53 +08:00
|
|
|
|
2007-06-28 04:36:16 +08:00
|
|
|
void deleteUsingDeleteHandler() const;
|
2008-06-20 01:30:38 +08:00
|
|
|
|
|
|
|
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
2010-02-19 05:21:12 +08:00
|
|
|
mutable OpenThreads::AtomicPtr _observerSet;
|
2008-06-20 01:30:38 +08:00
|
|
|
|
|
|
|
mutable OpenThreads::Atomic _refCount;
|
|
|
|
#else
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-02-28 03:44:33 +08:00
|
|
|
mutable OpenThreads::Mutex* _refMutex;
|
2004-09-27 22:15:13 +08:00
|
|
|
|
2006-02-28 03:44:33 +08:00
|
|
|
mutable int _refCount;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-02-19 05:21:12 +08:00
|
|
|
mutable void* _observerSet;
|
2008-06-20 01:30:38 +08:00
|
|
|
#endif
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
2010-05-14 20:14:14 +08:00
|
|
|
inline int Referenced::ref() const
|
2005-02-23 20:50:10 +08:00
|
|
|
{
|
2008-06-20 01:30:38 +08:00
|
|
|
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
2010-05-14 20:14:14 +08:00
|
|
|
return ++_refCount;
|
2008-06-20 01:30:38 +08:00
|
|
|
#else
|
2005-02-23 20:50:10 +08:00
|
|
|
if (_refMutex)
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
|
2010-05-14 20:14:14 +08:00
|
|
|
return ++_refCount;
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-14 20:14:14 +08:00
|
|
|
return ++_refCount;
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
2008-06-20 01:30:38 +08:00
|
|
|
#endif
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
|
|
|
|
2010-05-14 20:14:14 +08:00
|
|
|
inline int Referenced::unref() const
|
2005-02-23 20:50:10 +08:00
|
|
|
{
|
2010-05-14 20:14:14 +08:00
|
|
|
int newRef;
|
2008-06-20 01:30:38 +08:00
|
|
|
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
2010-05-14 20:14:14 +08:00
|
|
|
newRef = --_refCount;
|
|
|
|
bool needDelete = (newRef == 0);
|
2008-06-20 01:30:38 +08:00
|
|
|
#else
|
2005-02-23 20:50:10 +08:00
|
|
|
bool needDelete = false;
|
|
|
|
if (_refMutex)
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
|
2010-05-14 20:14:14 +08:00
|
|
|
newRef = --_refCount;
|
|
|
|
needDelete = newRef==0;
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-14 20:14:14 +08:00
|
|
|
newRef = --_refCount;
|
|
|
|
needDelete = newRef==0;
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
2008-06-20 01:30:38 +08:00
|
|
|
#endif
|
2005-02-23 20:50:10 +08:00
|
|
|
|
|
|
|
if (needDelete)
|
|
|
|
{
|
2010-05-17 22:21:53 +08:00
|
|
|
signalObserversAndDelete(true,true);
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
2010-05-14 20:14:14 +08:00
|
|
|
return newRef;
|
2005-02-23 20:50:10 +08:00
|
|
|
}
|
|
|
|
|
2008-10-03 21:34:32 +08:00
|
|
|
// 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(); }
|
2005-02-23 20:50:10 +08:00
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|