SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
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 (Body *bodyP=rwnil)
 
 RWTOnlyPointer (const RWTOnlyPointer< Body > &second)
 
 RWTOnlyPointer (RWStaticCtor)
 
 ~RWTOnlyPointer (void)
 
 operator Body * (void)
 
Body & operator* (void) const
 
Body * operator-> (void) const
 
RWTOnlyPointer< Body > & operator= (Body *ptr)
 
RWTOnlyPointer< Body > & operator= (const RWTOnlyPointer< Body > &second)
 
- Public Member Functions inherited from RWTPointer< Body >
 ~RWTPointer (void)
 
bool isValid (void) const
 
bool operator!= (const Body *ptr) const
 
bool operator!= (const RWTPointer< Body > &second) const
 
bool operator== (const Body *ptr) const
 
bool operator== (const RWTPointer< Body > &second) 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 (Body *bodyP)
 
 RWTPointer (const RWTPointer< Body > &second)
 
 RWTPointer (RWStaticCtor)
 
 RWTPointer (void)
 
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);
};
Simplifies the use of pointers that refer to objects on the heap, automatically destroying the object...
Definition RWTOnlyPointer.h:66

Member Typedef Documentation

◆ BodyType

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

The type of the body.

Constructor & Destructor Documentation

◆ RWTOnlyPointer() [1/3]

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

Special constructor that performs no initialization. Throws no exceptions.

◆ RWTOnlyPointer() [2/3]

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.

◆ RWTOnlyPointer() [3/3]

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

Assumes responsibility for another handle's body.

◆ ~RWTOnlyPointer()

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

Deletes the current body.

Member Function Documentation

◆ operator Body *()

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.

◆ operator*()

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.

◆ operator->()

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.

◆ operator=() [1/2]

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.

◆ operator=() [2/2]

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

Assumes responsibility for another handle's body.

◆ orphan()

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

Relinquishes control over the current body (if any).

◆ transfer()

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 © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.