RWTCountedPointer<Body> RWTPointer<Body>
#include <rw/pointer/RWTCountedPointer.h>
A smart pointer class that is intended for use as a handle to a reference-counting body. Each time an instance of this class is bound to a body instance, it increments the reference count maintained by that body. Each time it detaches from a body instance, it decrements the body's reference count; and if the reference count reaches zero, it deletes the body instance. The reference counting relieves clients of the burden of having to keep track of when it is safe to delete a body instance. The class specified as a template parameter must provide member functions for incrementing and decrementing the reference count. These functions must be declared as void addReference() and unsigned removeReference(). The easiest way to achieve this is by deriving from class RWTCountingBody.
#include <rw/pointer/RWTCountedPointer.h> #include <rw/pointer/RWTCountingBody.h> #include <rw/sync/RWMutexLock.h> #include <iostream.h> class Foo : public RWTCountingBody<RWMutexLock> { public: void bar(void) { cout << "I'm a foo example" << endl; } }; typedef RWTCountedPointer<Foo> FooPointer; int main(void) { try { FooPointer p1(new Foo);p1->bar(); } catch(RWxmsg& msg) { cout << msg.why() << endl; } return 0; }
RWTCountedPointer(Body* bodyP=rwnil);
Attaches to and increments the reference count on a body.
RWTCountedPointer(RWStaticCtor);
Special constructor that performs no initialization. Throws no exceptions.
RWTCountedPointer(const RWTCountedPointer<Body>& second);
Attaches to and increments the reference count on second's body.
~RWTCountedPointer(void);
Decrements the body's reference count and deletes it if there are no other references.
typedef Body BodyType;
The type of the Body.
Body& operator*(void) const;
Dereferences the handle to get a reference to the body. Throws an RWTHRInvalidPointer exception.
Body* operator->(void) const;
This operator should always be used to dereference the handle, since it will validate the pointer, throwing an RWTHRInvalidPointer exception if it is not valid (==rwnil).
RWTCountedPointer<Body>& operator=(Body* ptr);
Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references. It then attaches to pointer body, and increments its reference count.
RWTCountedPointer<Body>& operator=(const RWTCountedPointer<Body>& second);
Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references. It then attaches to second's body, and increments its reference count.
void orphan(void);
Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references.
void transfer(Body* bodyP=rwnil);
Detaches from the current body (if any), decrements its reference count and deletes it if there are no other references. Then it attaches to the new body, incrementing its reference count.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.