SourcePro® API Reference Guide

 
List of all members | Public Types | Public Member Functions | Protected Member Functions
RWTOnlyPointer< Body > Class Template Reference

Simplifies the use of pointers that refer to objects on the heap, automatically destroying the object when the pointer object is itself destroyed. More...

#include <rw/pointer/RWTOnlyPointer.h>

Inheritance diagram for RWTOnlyPointer< Body >:
RWTPointer< Body >

Public Types

typedef Body BodyType
 
- Public Types inherited from RWTPointer< Body >
typedef Body BodyType
 

Public Member Functions

 RWTOnlyPointer (RWStaticCtor)
 
 RWTOnlyPointer (Body *bodyP=rwnil)
 
 RWTOnlyPointer (const RWTOnlyPointer< Body > &second)
 
 ~RWTOnlyPointer (void)
 
 operator Body * (void)
 
Body & operator* (void) const
 
Body * operator-> (void) const
 
RWTOnlyPointer< Body > & operator= (const RWTOnlyPointer< Body > &second)
 
RWTOnlyPointer< Body > & operator= (Body *ptr)
 
- Public Member Functions inherited from RWTPointer< Body >
 ~RWTPointer (void)
 
bool isValid (void) const
 
bool operator!= (const RWTPointer< Body > &second) const
 
bool operator!= (const Body *ptr) const
 
bool operator== (const RWTPointer< Body > &second) const
 
bool operator== (const Body *ptr) const
 
void swapWith (RWTPointer< Body > &second)
 
void validate (void) const
 

Protected Member Functions

Body * orphan (void)
 
void transfer (Body *bodyP=rwnil)
 
- Protected Member Functions inherited from RWTPointer< Body >
 RWTPointer (void)
 
 RWTPointer (RWStaticCtor)
 
 RWTPointer (Body *bodyP)
 
 RWTPointer (const RWTPointer< Body > &second)
 
Body * get (void) const
 
RWTPointer< Body > & operator= (Body *ptr)
 

Detailed Description

template<class Body>
class RWTOnlyPointer< Body >

RWTOnlyPointer 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, especially in the presence of exceptions. This class provides functionality equivalent to that provided by the C++ Standard Library class std::auto_ptr.

Example
#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();
}
private:
void initialize(void);
};

Member Typedef Documentation

template<class Body>
typedef Body RWTOnlyPointer< Body >::BodyType

The type of the body.

Constructor & Destructor Documentation

template<class Body >
RWTOnlyPointer< Body >::RWTOnlyPointer ( RWStaticCtor  )
inline

Special constructor that performs no initialization. Throws no exceptions.

template<class Body>
RWTOnlyPointer< Body >::RWTOnlyPointer ( Body *  bodyP = rwnil)
inline

Assumes responsibility for a new body. This pointer manages an instance parameter bodyP. Throws no exceptions.

template<class Body>
RWTOnlyPointer< Body >::RWTOnlyPointer ( const RWTOnlyPointer< Body > &  second)
inline

Assumes responsibility for another handle's body.

template<class Body >
RWTOnlyPointer< Body >::~RWTOnlyPointer ( void  )
inline

Deletes the current body.

Member Function Documentation

template<class Body >
RWTOnlyPointer< Body >::operator Body * ( void  )
inline

Body pointer conversion operator for the handle (required for handle conversions). Gives up responsibility for deleting the body.

template<class Body >
Body & RWTOnlyPointer< Body >::operator* ( void  ) const
inline

Pointer dereference operator. Dereferences the handle to get a reference to the body. Throws the exception RWTHRInvalidPointer if the pointer is not valid.

template<class Body >
Body * RWTOnlyPointer< Body >::operator-> ( void  ) const
inline

Pointer dereference operator. This operator should always be used to dereference the handle as it first validates the pointer. If the pointer is not valid, throws an RWTHRInvalidPointer exception.

template<class Body>
RWTOnlyPointer< Body > & RWTOnlyPointer< Body >::operator= ( const RWTOnlyPointer< Body > &  second)
inline

Assumes responsibility for another handle's body.

template<class Body>
RWTOnlyPointer< Body > & RWTOnlyPointer< Body >::operator= ( Body *  ptr)
inline

Assigns a new pointer value to the handle (required for handle conversions). Throws no exceptions.

template<class Body >
Body * RWTOnlyPointer< Body >::orphan ( void  )
inlineprotected

Relinquishes control over the current body (if any).

template<class Body>
void RWTOnlyPointer< Body >::transfer ( Body *  bodyP = rwnil)
inlineprotected

Deletes the current body (if any), and assumes control of another body (if any).

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.