OpenSceneGraph/include/osg/Referenced

52 lines
1.8 KiB
Plaintext
Raw Normal View History

2002-07-17 04:07:32 +08:00
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
//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 {
/** Base class from providing referencing counted objects.*/
class SG_EXPORT Referenced
2001-01-11 00:32:10 +08:00
{
public:
Referenced() { _refCount=0; }
Referenced(const Referenced&) { _refCount=0; }
2001-01-11 00:32:10 +08:00
inline Referenced& operator = (Referenced&) { return *this; }
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.*/
inline void ref() const { ++_refCount; }
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
reference count goes to zero, it is assumed that this object
is no longer referenced and is automatically deleted.*/
inline void unref() const { --_refCount; if (_refCount<=0) delete this; }
/** 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. */
inline const int referenceCount() const { return _refCount; }
2001-01-11 00:32:10 +08:00
protected:
virtual ~Referenced();
mutable int _refCount;
2001-01-11 00:32:10 +08:00
};
}
2001-01-11 00:32:10 +08:00
#endif