2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-01-22 00:45:36 +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
|
|
|
|
* (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.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_REFERENCED
|
|
|
|
#define OSG_REFERENCED 1
|
|
|
|
|
2004-11-02 00:14:53 +08:00
|
|
|
// When building OSG with Java need to derive from Noodle::CBridgable class,
|
|
|
|
// therefore so OSG_JAVA_BUILD must be defined. Also the thread-safe ref/unref test
|
2004-11-15 21:02:43 +08:00
|
|
|
// as built in for the NoodleGlue wrapping. NoodleGlue has a Garbage collector mechanism
|
2004-11-02 00:14:53 +08:00
|
|
|
// which is very similar to osg::DeletionManager. So these aspects of osg::Referenced
|
|
|
|
// have been removed
|
|
|
|
//#define OSG_JAVA_BUILD
|
2004-07-20 13:34:02 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/Export>
|
2005-04-12 01:14:17 +08:00
|
|
|
|
2004-11-02 00:14:53 +08:00
|
|
|
#ifdef OSG_JAVA_BUILD
|
2004-11-15 21:02:43 +08:00
|
|
|
#include <NoodleGlue/Bridgable.h>
|
2004-11-02 00:14:53 +08:00
|
|
|
#else
|
2005-02-23 20:50:10 +08:00
|
|
|
#include <OpenThreads/ScopedLock>
|
2004-09-27 22:15:13 +08:00
|
|
|
#include <OpenThreads/Mutex>
|
2004-11-02 00:14:53 +08:00
|
|
|
#endif
|
2004-07-20 13:34:02 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
namespace osg {
|
|
|
|
|
2004-11-02 00:14:53 +08:00
|
|
|
#ifndef OSG_JAVA_BUILD
|
|
|
|
// forward declar, declared after Referenced below.
|
2002-12-16 18:25:31 +08:00
|
|
|
class DeleteHandler;
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Base class from providing referencing 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
|
|
|
|
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
Referenced();
|
|
|
|
|
|
|
|
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
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
|
|
|
|
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.*/
|
|
|
|
bool getThreadSafeRefUnref() const { return _refMutex!=0; }
|
2002-12-16 18:25:31 +08:00
|
|
|
|
2005-02-23 04:25:58 +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.*/
|
2005-02-23 20:50:10 +08:00
|
|
|
inline void ref() const;
|
2002-03-20 22:03:30 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
/** Decrement the reference count by one, indicating that
|
2001-01-11 00:32:10 +08:00
|
|
|
a pointer to this object is 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.*/
|
2005-02-23 20:50:10 +08:00
|
|
|
inline void unref() const;
|
2002-03-20 22:03:30 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
/** Decrement the reference count by one, indicating that
|
2002-03-20 22:03:30 +08:00
|
|
|
a pointer to this object is referencing it. However, do
|
|
|
|
not delete it, even if ref count goes to 0. Warning, unref_nodelete()
|
|
|
|
should only be called if the user knows exactly who will
|
|
|
|
be resonsible for, one should prefer unref() over unref_nodelete()
|
|
|
|
as the later can lead to memory leaks.*/
|
2004-09-27 22:15:13 +08:00
|
|
|
void unref_nodelete() const;
|
2002-03-20 22:03:30 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
/** Return the number 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
|
|
|
|
2004-09-27 22:15:13 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** Set whether reference counting should be use a mutex to create thread reference counting.*/
|
|
|
|
static void setThreadSafeReferenceCounting(bool enableThreadSafeReferenceCounting);
|
|
|
|
|
|
|
|
/** Get whether reference counting is active.*/
|
|
|
|
static bool getThreadSafeReferenceCounting();
|
|
|
|
|
|
|
|
friend class DeleteHandler;
|
|
|
|
|
|
|
|
/** Set a DeleteHandler to which deletion of all referenced counted objects
|
|
|
|
* will be delegated to.*/
|
|
|
|
static void setDeleteHandler(DeleteHandler* handler);
|
|
|
|
|
|
|
|
/** Get a DeleteHandler.*/
|
|
|
|
static DeleteHandler* getDeleteHandler();
|
|
|
|
|
2003-01-17 22:11:34 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
protected:
|
2002-02-07 09:11:20 +08:00
|
|
|
virtual ~Referenced();
|
2004-07-20 13:34:02 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
mutable OpenThreads::Mutex* _refMutex;
|
2004-09-27 22:15:13 +08:00
|
|
|
|
2005-02-23 04:25:58 +08:00
|
|
|
mutable int _refCount;
|
2002-12-16 18:25:31 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
2002-12-16 18:25:31 +08:00
|
|
|
|
|
|
|
/** Class for override the default delete behavior so that users can implment their own object
|
|
|
|
* deletion schemes. This might be done to help implement protection of multiple threads from deleting
|
2002-12-17 05:39:26 +08:00
|
|
|
* objects unintentionally.
|
|
|
|
* Note, the DeleteHandler cannot itself be reference counted, otherwise it
|
|
|
|
* would be responsible for deleting itself!
|
|
|
|
* An static auto_ptr<> is used internally in Referenced.cpp to manage the
|
|
|
|
* DeleteHandler's memory.*/
|
2002-12-16 18:25:31 +08:00
|
|
|
class DeleteHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual ~DeleteHandler() {}
|
|
|
|
|
|
|
|
/** flush any cache of objects that need to be deleted by doing an actual delete.*/
|
|
|
|
virtual void flush() {}
|
|
|
|
|
|
|
|
inline void doDelete(const Referenced* object) { delete object; }
|
|
|
|
|
|
|
|
/** Request the deletion of an object.
|
|
|
|
* Depending on users implementation of DeleteHandler, the delete of the object may occur
|
|
|
|
* straight away or be delayed until doDelete is called.
|
|
|
|
* The default implementation does a delete straight away.*/
|
|
|
|
virtual void requestDelete(const Referenced* object) { doDelete(object); }
|
|
|
|
};
|
2005-02-23 20:50:10 +08:00
|
|
|
|
|
|
|
inline void Referenced::ref() const
|
|
|
|
{
|
|
|
|
if (_refMutex)
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
|
|
|
|
++_refCount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++_refCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Referenced::unref() const
|
|
|
|
{
|
|
|
|
bool needDelete = false;
|
|
|
|
if (_refMutex)
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
|
|
|
|
--_refCount;
|
|
|
|
needDelete = _refCount<=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--_refCount;
|
|
|
|
needDelete = _refCount<=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needDelete)
|
|
|
|
{
|
|
|
|
if (getDeleteHandler()) getDeleteHandler()->requestDelete(this);
|
|
|
|
else delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-02 00:14:53 +08:00
|
|
|
#else
|
|
|
|
/** Java wrappers use the CBridgable base-class for referencing
|
|
|
|
* and garbage collection.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Referenced : public NoodleGlue::CBridgable
|
2004-11-02 00:14:53 +08:00
|
|
|
{
|
|
|
|
public:
|
2004-11-15 21:02:43 +08:00
|
|
|
/** Method not used in NoodleGlue referencing
|
2004-11-02 00:14:53 +08:00
|
|
|
*/
|
|
|
|
inline void unref_nodelete() const { --_refCount; }
|
|
|
|
inline int referenceCount() const { return _refCount; }
|
|
|
|
protected:
|
|
|
|
virtual ~Referenced() {}
|
|
|
|
};
|
|
|
|
#endif //OSG_JAVA_BUILD
|
2002-12-16 18:25:31 +08:00
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|