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 |
|
|||||||||
|
Constructs an empty pointer.
|
|
||||||||||||||
|
Constructs a new smart pointer. The parameter must be assignable to T (e.g., T2 is a subclass of T). |
|
||||||||||
|
Copies a smart pointer, incrementing its reference count.
|
|
||||||||||||||
|
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). |
|
|||||||||
|
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. |
|
|||||||||
|
Returns a string representation of the pointer.
|
|
|||||||||
|
Returns a represntation useful for debugging.
|
|
|||||||||||||
|
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. |
|
|||||||||
|
Returns a pointer to the underlying object.
|
|
||||||||||||||||||||
|
|
|
|||||||||
|
Returns a reference to the underlying object. Fails via a run-time assertion if this smart pointer is null. |
|
|||||||||
|
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) { ... }
|
|
|||||||||
|
Returns a pointer to the underlying object. Fails via a run-time assertion if this smart pointer is null. |
|
||||||||||||||
|
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). |
|
||||||||||
|
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.
|
|
|||||||||
|
Clears this pointer. Equivalent to *this = ptr<T>().
|
|
||||||||||
|
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). |
|
|||||||||||||
|
Statically casts the ptr to type T2, returning the resultant smart pointer (sharing and incrementing this object's reference count).
|
|
|||||||||
|
Returns true if this smart pointer contains the only reference to the underlying pointer. Equivalent to use_count() == 1. |
|
||||||||||
|
Creates an unowned pointer with no reference count.
|
|
|||||||||
|
Returns the reference count of this pointer, as described in the overview.
|
|
|||||
|
|