Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

ptr< T > Class Template Reference

List of all members.

Detailed Description

template<typename T>
class ptr< T >

A threadsafe reference-counting smart pointer class.

When a ptr<T> object is first created from a pointer of type T*, a new reference count is created and initialized to one. Whenever a copy of this smart pointer is made, the reference count is shared with the new copy and incremented. Whenever a copy is deleted, the reference count is decremented; when it reaches zero, the underlying object is deleted.

Using the static ptr<T>::unowned() method, a ptr can also be created with a reference count of 0, in which case the underlying pointer is considered unowned and is never implicitly deallocated and the reference count is never changed.


Public Methods

 ptr ()
 Constructs an empty pointer.

template<typename T2>  ptr (T2 *p)
 Constructs a new smart pointer.

 ptr (const ptr &p)
 Copies a smart pointer, incrementing its reference count.

template<typename T2>  ptr (const ptr< T2 > &p)
 Copies a smart pointer, incrementing its reference count.

ptr< T > & operator= (const ptr< T > &p)
 Clears this smart pointer (decreasing the reference count and deleting the underlying pointer if necessary) and assigns to it a new pointer, incrementing its reference count.

 ~ptr ()
 Destructor.

template<typename T2> ptr< T > & operator= (const ptr< T2 > &p)
 Clears this smart pointer (decreasing the reference count and deleting the underlying pointer if necessary) and assigns to it a new pointer, incrementing its reference count.

void set (T *p)
 Clears this smart pointer (decreasing the reference count and deleting the underlying pointer if necessary) and assigns to it a new pointer, incrementing its reference count.

template<typename T2> ptr< T2 > static_cast_to ()
 Statically casts the ptr to type T2, returning the resultant smart pointer (sharing and incrementing this object's reference count).

template<typename T2> ptr< T2 > dynamic_cast_to ()
 Dynamically casts the ptr to type T2, returning the resultant smart pointer (sharing this object's reference count).

 NMSTL_ARITH_OPS_T1 (T2, ptr< T2 >, rep)
 operator const void * () const
 Returns a null pointer if this smart pointer is null, or a non-null pointer if non-null.

T & operator * () const
 Returns a reference to the underlying object.

T * operator-> () const
 Returns a pointer to the underlying object.

T * get () const
 Returns a pointer to the underlying object.

string debug_repr () const
 Returns a represntation useful for debugging.

void reset ()
 Clears this pointer. Equivalent to *this = ptr<T>().

long use_count ()
 Returns the reference count of this pointer, as described in the overview.

bool unique ()
 Returns true if this smart pointer contains the only reference to the underlying pointer.

string as_string () const
 Returns a string representation of the pointer.


Static Public Methods

ptr< T > unowned (T *t)
 Creates an unowned pointer with no reference count.


Friends

class ptr


Constructor & Destructor Documentation

template<typename T>
ptr< T >::ptr  
 

Constructs an empty pointer.

template<typename T>
template<typename T2>
ptr< T >::ptr T2 *    p [explicit]
 

Constructs a new smart pointer.

The parameter must be assignable to T (e.g., T2 is a subclass of T).

template<typename T>
ptr< T >::ptr const ptr< T > &    p
 

Copies a smart pointer, incrementing its reference count.

template<typename T>
template<typename T2>
ptr< T >::ptr const ptr< T2 > &    p
 

Copies a smart pointer, incrementing its reference count.

The parameter's pointer type must be assignable to T (e.g., T2 is a subclass of T).

template<typename T>
ptr< T >::~ptr  
 

Destructor.

Clears this smart pointer, decreasing the reference count (unless the pointer is unowned) and deleting the underlying pointer if the reference count reaches 0.


Member Function Documentation

template<typename T>
string ptr< T >::as_string   const
 

Returns a string representation of the pointer.

template<typename T>
string ptr< T >::debug_repr   const
 

Returns a represntation useful for debugging.

template<typename T>
template<typename T2>
ptr<T2> ptr< T >::dynamic_cast_to  
 

Dynamically casts the ptr to type T2, returning the resultant smart pointer (sharing this object's reference count).

If a T* cannot be cast to a T2*, then a null smart pointer is returned and this object's reference count is not incremented.

template<typename T>
T* ptr< T >::get   const
 

Returns a pointer to the underlying object.

template<typename T>
ptr< T >::NMSTL_ARITH_OPS_T1 T2   ,
ptr< T2 >   ,
rep   
 

template<typename T>
T& ptr< T >::operator *   const
 

Returns a reference to the underlying object.

Fails via a run-time assertion if this smart pointer is null.

template<typename T>
ptr< T >::operator const void *   const
 

Returns a null pointer if this smart pointer is null, or a non-null pointer if non-null.

Useful primarily for implicitly casting to a boolean, e.g.:

        ptr<foo> p = ...;
        if (p) { ... }
        

template<typename T>
T* ptr< T >::operator->   const
 

Returns a pointer to the underlying object.

Fails via a run-time assertion if this smart pointer is null.

template<typename T>
template<typename T2>
ptr<T>& ptr< T >::operator= const ptr< T2 > &    p
 

Clears this smart pointer (decreasing the reference count and deleting the underlying pointer if necessary) and assigns to it a new pointer, incrementing its reference count.

The parameter's pointer type must be assignable to T (e.g., T2 is a subclass of T).

template<typename T>
ptr<T>& ptr< T >::operator= const ptr< T > &    p
 

Clears this smart pointer (decreasing the reference count and deleting the underlying pointer if necessary) and assigns to it a new pointer, incrementing its reference count.

template<typename T>
void ptr< T >::reset  
 

Clears this pointer. Equivalent to *this = ptr<T>().

template<typename T>
void ptr< T >::set T *    p
 

Clears this smart pointer (decreasing the reference count and deleting the underlying pointer if necessary) and assigns to it a new pointer, incrementing its reference count.

The parameter's pointer type must be assignable to T (e.g., T2 is a subclass of T).

template<typename T>
template<typename T2>
ptr<T2> ptr< T >::static_cast_to  
 

Statically casts the ptr to type T2, returning the resultant smart pointer (sharing and incrementing this object's reference count).

template<typename T>
bool ptr< T >::unique  
 

Returns true if this smart pointer contains the only reference to the underlying pointer.

Equivalent to use_count() == 1.

template<typename T>
ptr<T> ptr< T >::unowned T *    t [static]
 

Creates an unowned pointer with no reference count.

template<typename T>
long ptr< T >::use_count  
 

Returns the reference count of this pointer, as described in the overview.


Friends And Related Function Documentation

template<typename T>
friend class ptr [friend]
 


The documentation for this class was generated from the following file:
Generated on Fri Dec 20 13:35:11 2002 for NMSTL