Module: Threads Package: Smart Pointer
RWTOnlyPointer<Body> RWTPointer<Body>
BodyType operator Body*() operator*() |
operator->() operator=() orphan() |
RWTOnlyPointer() transfer() ~RWTOnlyPointer() |
#include <rw/pointer/RWTOnlyPointer.h>
RWTOnlyPointer<Body> is an abstraction that simplifies the use of pointers referring to objects (that is, bodies) on the heap. It doesn't do reference counting on the body; rather, it is based on the idea of strict ownership semantics. Strict ownership means that if the assignment operator or the copy constructor is called, then the right-hand instance is invalidated (its body pointer is set to rwnil), and the left-hand instance assumes responsibility for deleting the body.
This class is useful for ensuring the recovery of dynamically allocated memory, specially in the presence of exceptions. This class provides functionality equivalent to that provided by the Standard C++ Library module's std::auto_ptr<> class.
#include <rw/pointer/RWTOnlyPointer.h> Class A { ... }; Class B {public: // The instance of A allocated here is automatically // Deleted if an exception occurs in the initialize() // function. B() {p_ = new A; initialize(); } ~B() {} private: RWTOnlyPointer<A> p_; void initialize(void); };
typedef Body BodyType;
The type of the body.
RWTOnlyPointer(Body* bodyP=rwnil);
Assumes responsibility for a new body. This pointer manages an instance parameter bodyP. Throws no exceptions.
RWTOnlyPointer(RWStaticCtor);
Special constructor that performs no initialization. Throws no exceptions.
RWTOnlyPointer(RWTOnlyPointer<Body>& second);
Assumes responsibility for another handle's body.
~RWTOnlyPointer();
Deletes the current body.
RWTOnlyPointer<Body>& operator=(Body* ptr);
Assigns a new pointer value to the handle (required for handle conversions). Throws no exceptions.
RWTOnlyPointer<Body>& operator=(RWTOnlyPointer<Body>& second);
Assumes responsibility for another handle's body.
operator Body*();
Body pointer conversion operator for the handle (required for handle conversions). Gives up responsibility for deleting body.
Body& operator*() const;
Pointer dereference operator. Dereferences the handle to get a reference to the body. Throws the exception RWTHRInvalidPointer if it is not valid.
Body* operator->() const;
Pointer dereference operator. This operator should always be used to dereference the handle as it will validate the pointer. If it is not valid, it will throw an RWTHRInvalidPointer exception.
Body* orphan();
Relinquishes control over the current body (if any).
void transfer(Body* bodyP=rwnil);
Deletes the current body (if any), and assumes control of another body (if any).
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.