Contents Functions Classes Topics User Forum CVS

G3D::ReferenceCountedObject Class Reference

#include <ReferenceCount.h>

Inherited by G3D::Conduit, G3D::Framebuffer, G3D::GFont, G3D::GModule, G3D::GPUProgram, G3D::IFSModel, G3D::Lighting, G3D::MD2Model, G3D::Milestone, G3D::NetListener, G3D::ObjectShader, G3D::PosedModel, G3D::PosedModel2D, G3D::Renderbuffer, G3D::Shader, G3D::Shape, G3D::Sky, G3D::Texture, G3D::VARArea, and G3D::VertexAndPixelShader.

List of all members.

Public Member Functions

void ReferenceCountedObject_zeroWeakPointers ()
virtual ~ReferenceCountedObject ()
 ReferenceCountedObject (const ReferenceCountedObject &notUsed)
ReferenceCountedObjectoperator= (const ReferenceCountedObject &other)

Public Attributes

AtomicInt32 ReferenceCountedObject_refCount
_WeakPtrLinkedListReferenceCountedObject_weakPointer

Protected Member Functions

 ReferenceCountedObject ()


Detailed Description

Objects that are reference counted inherit from this. Subclasses must have a public destructor (the default destructor is fine) and publicly inherit ReferenceCountedObject.

Multiple inheritance from a reference counted object is dangerous-- use at your own risk.

ReferenceCountedPointer and ReferenceCountedObject are threadsafe. You can create and drop references on multiple threads without violating integrity. WeakReferenceCountedPointer is not threadsafe. Introducing a weak pointer destroys all thread safety, even for strong pointers to the same object (this is inherent in the design of the class; we cannot fix it without slowing down the performance of reference counted objects.)

Usage Example

class Foo : public G3D::ReferenceCountedObject {
public:
    int x;
};

class Bar : public Foo {};

typedef G3D::ReferenceCountedPointer<Foo> FooRef;
typedef G3D::WeakReferenceCountedPointer<Foo> WeakFooRef;
typedef G3D::ReferenceCountedPointer<Bar> BarRef;

int main(int argc, char *argv[]) {

    WeakFooRef x;

    {
        FooRef a = new Foo();

Reference count == 1

        x = a;
Weak references do not increase count

        {
            FooRef b = a;
Reference count == 2
        }

Reference count == 1
    }
No more strong references; object automatically deleted.
x is set to NULL automatically.

Example of using dynamic cast on reference counted objects
    BarRef b = new Bar();

No cast needed to go down the heirarchy.
    FooRef f = b;

We can't cast the reference object because it is a class.
Instead we must extract the pointer and cast that:
    b = dynamic_cast<Bar*>(&*f);

    return 0;
}

Deprecated:
To be replaced by boost::shared_ptr in 7.0


Constructor & Destructor Documentation

G3D::ReferenceCountedObject::ReferenceCountedObject  )  [inline, protected]
 

virtual G3D::ReferenceCountedObject::~ReferenceCountedObject  )  [inline, virtual]
 

G3D::ReferenceCountedObject::ReferenceCountedObject const ReferenceCountedObject notUsed  )  [inline]
 

Note: copies will initially start out with 0 references and 0 weak references like any other object.


Member Function Documentation

ReferenceCountedObject& G3D::ReferenceCountedObject::operator= const ReferenceCountedObject other  )  [inline]
 

void G3D::ReferenceCountedObject::ReferenceCountedObject_zeroWeakPointers  )  [inline]
 

Automatically called immediately before the object is deleted. This is not called from the destructor because it needs to be invoked before the subclass destructor.


Member Data Documentation

AtomicInt32 G3D::ReferenceCountedObject::ReferenceCountedObject_refCount
 

The long name is to keep this from accidentally conflicting with a subclass's variable name. Do not use or explicitly manipulate this value--its type may change in the future and is not part of the supported API.

_WeakPtrLinkedList* G3D::ReferenceCountedObject::ReferenceCountedObject_weakPointer
 

Linked list of all weak pointers that reference this (some may be on the stack!). Do not use or explicitly manipulate this value.


The documentation for this class was generated from the following file:
Generated on Mon Jul 17 11:50:46 2006 for G3D by doxygen 1.4.5
Hosted by SourceForge.net Logo