Public Member Functions | Private Member Functions | Private Attributes

smart_ptr< T > Class Template Reference
[Threadsafe Collection API]

Auto-destructing pointer class. More...

#include <smart_ptr.h>

Collaboration diagram for smart_ptr< T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 smart_ptr (T *pT=NULL) throw ()
template<class X >
 smart_ptr (const smart_ptr< X > &from) throw ()
 smart_ptr (const smart_ptr< T > &from) throw ()
 ~smart_ptr (void) throw ()
T * operator-> (void) throw ()
const T * operator-> (void) const throw ()
 operator T * (void) throw ()
 operator const T * (void) const throw ()
bool operator! (void) const throw ()
const smart_ptr< T > & operator= (T *pT) throw ()
template<class X >
const smart_ptr< T > & operator= (const smart_ptr< X > &from) throw ()
const smart_ptr< T > & operator= (const smart_ptr< T > &from) throw ()
long get_ref_count (void) const throw ()
T * disown (void) throw ()

Private Member Functions

void assign (T *pT) throw ()
template<class X >
void assign (const smart_ptr< X > &from) throw ()
void release (void) throw ()
long increment (void) throw ()
long decrement (void) throw ()

Private Attributes

threadsafe_counterm_prefcount
T * m_pT

Detailed Description

template<class T>
class smart_ptr< T >

Auto-destructing pointer class.

This is a ref-counting class. It keeps the ref struct as a separate struct on the heap, and all smart pointers share it.

WARNING: you will really hose yourself if you have two or more smart_ptr's wrapping the same underlying pointer if they use different ref structs. The way to avoid this is to use smart_ptr<T>'s everywhere (never convert to a raw T* and then pass to another smart_ptr<T>, for instance).

This class is PARTLY threadsafe! That is, there is a mutex protecting the ref count. This is a slight overhead for single-threaded applications, but is probably worth it overall. General smart_ptr<T> accesses such as

p->doSomething()  

don't pay any synchronization overhead. Only operations that impact the ref count (assignment, destruction, etc.) will synchronize.

Put another way, accessing the pointer is NOT synchronized (and therefore not generally threadsafe). If your class needs that, you'll need to make the class itself threadsafe.

Definition at line 69 of file smart_ptr.h.


Constructor & Destructor Documentation

template<class T>
smart_ptr< T >::smart_ptr ( T *  pT = NULL  )  throw () [inline]

Definition at line 45 of file smart_ptr.h.

template<class T>
template<class X >
smart_ptr< T >::smart_ptr ( const smart_ptr< X > &  from  )  throw () [inline]

Definition at line 50 of file smart_ptr.h.

template<class T>
smart_ptr< T >::smart_ptr ( const smart_ptr< T > &  from  )  throw () [inline]

Definition at line 55 of file smart_ptr.h.

template<class T>
smart_ptr< T >::~smart_ptr ( void   )  throw () [inline]

Definition at line 61 of file smart_ptr.h.


Member Function Documentation

template<class T>
T* smart_ptr< T >::operator-> ( void   )  throw () [inline]

Definition at line 64 of file smart_ptr.h.

template<class T>
const T* smart_ptr< T >::operator-> ( void   )  const throw () [inline]

Definition at line 69 of file smart_ptr.h.

template<class T>
smart_ptr< T >::operator T * ( void   )  throw () [inline]

Definition at line 74 of file smart_ptr.h.

template<class T>
smart_ptr< T >::operator const T * ( void   )  const throw () [inline]

Definition at line 78 of file smart_ptr.h.

template<class T>
bool smart_ptr< T >::operator! ( void   )  const throw () [inline]

Definition at line 82 of file smart_ptr.h.

template<class T>
const smart_ptr<T>& smart_ptr< T >::operator= ( T *  pT  )  throw () [inline]

Definition at line 86 of file smart_ptr.h.

template<class T>
template<class X >
const smart_ptr<T>& smart_ptr< T >::operator= ( const smart_ptr< X > &  from  )  throw () [inline]

Definition at line 92 of file smart_ptr.h.

template<class T>
const smart_ptr<T>& smart_ptr< T >::operator= ( const smart_ptr< T > &  from  )  throw () [inline]

Definition at line 97 of file smart_ptr.h.

template<class T>
long smart_ptr< T >::get_ref_count ( void   )  const throw () [inline]

Definition at line 102 of file smart_ptr.h.

template<class T>
T* smart_ptr< T >::disown ( void   )  throw () [inline]

Definition at line 106 of file smart_ptr.h.

template<class T>
void smart_ptr< T >::assign ( T *  pT  )  throw () [inline, private]

Definition at line 124 of file smart_ptr.h.

template<class T>
template<class X >
void smart_ptr< T >::assign ( const smart_ptr< X > &  from  )  throw () [inline, private]

Definition at line 135 of file smart_ptr.h.

template<class T>
void smart_ptr< T >::release ( void   )  throw () [inline, private]

Definition at line 179 of file smart_ptr.h.

template<class T>
long smart_ptr< T >::increment ( void   )  throw () [inline, private]

Definition at line 197 of file smart_ptr.h.

template<class T>
long smart_ptr< T >::decrement ( void   )  throw () [inline, private]

Definition at line 204 of file smart_ptr.h.


Member Data Documentation

template<class T>
threadsafe_counter* smart_ptr< T >::m_prefcount [private]

Definition at line 212 of file smart_ptr.h.

template<class T>
T* smart_ptr< T >::m_pT [private]

Definition at line 213 of file smart_ptr.h.


The documentation for this class was generated from the following file: