2002-07-17 04:07:32 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
|
2001-10-04 23:12:57 +08:00
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_REFERENCED
|
|
|
|
#define OSG_REFERENCED 1
|
|
|
|
|
|
|
|
#include <osg/Export>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2002-12-16 18:25:31 +08:00
|
|
|
|
|
|
|
// forward declar, declared after Refenced below.
|
|
|
|
class DeleteHandler;
|
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Base class from providing referencing counted objects.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
class SG_EXPORT Referenced
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2002-09-20 23:55:50 +08:00
|
|
|
Referenced()
|
|
|
|
{
|
|
|
|
|
|
|
|
_refCount=0;
|
|
|
|
#ifdef OSG_COMPILE_UNIT_TESTS
|
|
|
|
_createdCount ++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
Referenced(const Referenced&) {
|
|
|
|
_refCount=0;
|
|
|
|
#ifdef OSG_COMPILE_UNIT_TESTS
|
|
|
|
_createdCount ++;
|
|
|
|
#endif
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
inline Referenced& operator = (Referenced&) { return *this; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-12-16 18:25:31 +08:00
|
|
|
friend DeleteHandler;
|
|
|
|
|
|
|
|
static void setDeleteHandler(DeleteHandler* handler);
|
|
|
|
|
|
|
|
static DeleteHandler* getDeleteHandler();
|
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** increment the reference count by one, indicating that
|
|
|
|
this object has another pointer which is referencing it.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void ref() const { ++_refCount; }
|
2002-03-20 22:03:30 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** decrement the reference count by one, indicating that
|
|
|
|
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.*/
|
2002-12-16 18:25:31 +08:00
|
|
|
inline void unref() const;
|
2002-03-20 22:03:30 +08:00
|
|
|
|
|
|
|
/** decrement the reference count by one, indicating that
|
|
|
|
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.*/
|
|
|
|
inline void unref_nodelete() const { --_refCount; }
|
|
|
|
|
2001-01-11 00:32:10 +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
|
|
|
|
2002-09-20 23:55:50 +08:00
|
|
|
#ifdef OSG_COMPILE_UNIT_TESTS
|
|
|
|
/** return the total number of created referenced objects */
|
|
|
|
inline static int createdCount() { return _createdCount; }
|
|
|
|
|
|
|
|
/** return the total number of deleted referenced objects */
|
|
|
|
inline static int deletedCount() { return _deletedCount; }
|
|
|
|
#endif
|
|
|
|
|
2002-12-16 18:25:31 +08:00
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
protected:
|
2002-02-07 09:11:20 +08:00
|
|
|
virtual ~Referenced();
|
2001-09-20 05:08:56 +08:00
|
|
|
mutable int _refCount;
|
2002-12-16 18:25:31 +08:00
|
|
|
|
|
|
|
|
2002-09-20 23:55:50 +08:00
|
|
|
#ifdef OSG_COMPILE_UNIT_TESTS
|
|
|
|
static int _createdCount; // incremented in the constructor
|
|
|
|
static int _deletedCount; // incremented in the destructor
|
|
|
|
#endif
|
2001-09-20 05:08:56 +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
|
|
|
|
* objects unintentionally.*/
|
|
|
|
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); }
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void Referenced::unref() const
|
|
|
|
{
|
|
|
|
--_refCount;
|
|
|
|
if (_refCount<=0)
|
|
|
|
{
|
|
|
|
if (getDeleteHandler()) getDeleteHandler()->requestDelete(this);
|
|
|
|
else delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|