claw::memory::smart_ptr< T > Class Template Reference

#include <smart_ptr.hpp>

List of all members.


Detailed Description

template<typename T>
class claw::memory::smart_ptr< T >

A pointer with a reference counter.

Smart pointers allow the user to stop caring about the release of dynamically allocated memory. When no more pointers point to the allocated memory, this memory is released.

Template parameters:

Author:
Julien Jorge

Definition at line 51 of file smart_ptr.hpp.


Public Types

typedef T value_type
 The type of the pointed data.
typedef smart_ptr< value_typeself_type
 The type of the current class.
typedef T & reference
 Reference on the type of the stored data.
typedef T * pointer
 Pointer on the type of the stored data.
typedef const T & const_reference
 Constant reference on the type of the stored data.
typedef const T *const const_pointer
 Constant pointer on the type of the stored data.

Public Member Functions

 smart_ptr ()
 Default constructor.
 smart_ptr (pointer data)
 Constructor from a pointer.
 smart_ptr (const self_type &that)
 Copy constructor.
 ~smart_ptr ()
 Destructor. The memory is freed only if no more smart_ptr point on it.
self_typeoperator= (const self_type &that)
 Assignment operator.
bool operator== (const self_type &that) const
 Equality operator.
bool operator!= (const self_type &that) const
 Disequality operator.
pointer operator-> ()
 Dereference operator.
const_pointer operator-> () const
 Dereference operator.
reference operator* ()
 Dereference operator.
const_reference operator* () const
 Dereference operator.

Private Member Functions

void copy (const self_type &that)
 Copy a smart_ptr.
void release ()
 Release the allocated memory.

Private Attributes

unsigned int * m_ref_count
 Number of smart_ptr pointing on this memory area.
pointer m_ptr
 The pointed item.

Member Typedef Documentation

template<typename T>
typedef T claw::memory::smart_ptr< T >::value_type

The type of the pointed data.

Definition at line 55 of file smart_ptr.hpp.

template<typename T>
typedef smart_ptr<value_type> claw::memory::smart_ptr< T >::self_type

The type of the current class.

Definition at line 58 of file smart_ptr.hpp.

template<typename T>
typedef T& claw::memory::smart_ptr< T >::reference

Reference on the type of the stored data.

Definition at line 61 of file smart_ptr.hpp.

template<typename T>
typedef T* claw::memory::smart_ptr< T >::pointer

Pointer on the type of the stored data.

Definition at line 64 of file smart_ptr.hpp.

template<typename T>
typedef const T& claw::memory::smart_ptr< T >::const_reference

Constant reference on the type of the stored data.

Definition at line 67 of file smart_ptr.hpp.

template<typename T>
typedef const T* const claw::memory::smart_ptr< T >::const_pointer

Constant pointer on the type of the stored data.

Definition at line 70 of file smart_ptr.hpp.


Constructor & Destructor Documentation

template<typename T>
claw::memory::smart_ptr< T >::smart_ptr (  )  [inline]

Default constructor.

Definition at line 38 of file smart_ptr.tpp.

00039   : m_ref_count(NULL), m_ptr(NULL)
00040 {
00041 
00042 } // smart_ptr::smart_ptr()

template<typename T>
claw::memory::smart_ptr< T >::smart_ptr ( pointer  data  )  [inline]

Constructor from a pointer.

Parameters:
data Pointer on the data.
Warning: this constructor allows expressions like

int a; smart_ptr<int> p(&a);

Nevertheless, you should never fo that.

Definition at line 56 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr, and claw::memory::smart_ptr< T >::m_ref_count.

00057   : m_ref_count(NULL), m_ptr(NULL)
00058 {
00059   if (data)
00060     {
00061       m_ref_count = new unsigned int(1);
00062       m_ptr = data;
00063     }
00064 } // smart_ptr::smart_ptr() [pointer]

template<typename T>
claw::memory::smart_ptr< T >::smart_ptr ( const self_type that  )  [inline]

Copy constructor.

Parameters:
that The smart_pointer to copy.

Definition at line 72 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::copy().

00073 {
00074   copy( that );
00075 } // smart_ptr::smart_ptr() [copy]

template<typename T>
claw::memory::smart_ptr< T >::~smart_ptr (  )  [inline]

Destructor. The memory is freed only if no more smart_ptr point on it.

Definition at line 82 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::release().

00083 {
00084   release();
00085 } // smart_ptr::~smart_ptr()


Member Function Documentation

template<typename T>
claw::memory::smart_ptr< T >::self_type & claw::memory::smart_ptr< T >::operator= ( const self_type that  )  [inline]

Assignment operator.

Parameters:
that The smart_ptr to copy.

Definition at line 94 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::copy(), and claw::memory::smart_ptr< T >::release().

00095 {
00096   if ( &that != this )
00097     {
00098       release();
00099       copy( that );
00100     }
00101 
00102   return *this;
00103 } // smart_ptr::operator=()

template<typename T>
bool claw::memory::smart_ptr< T >::operator== ( const self_type that  )  const [inline]

Equality operator.

Returns:
True if the two smart_ptr point on the same address.

Definition at line 111 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr.

00112 {
00113   return m_ptr == that.m_ptr;
00114 } // smart_ptr::operator==()

template<typename T>
bool claw::memory::smart_ptr< T >::operator!= ( const self_type that  )  const [inline]

Disequality operator.

Returns:
True if the two smart_ptr point on different address.

Definition at line 122 of file smart_ptr.tpp.

00123 {
00124   return !( (*this) == that );
00125 } // smart_ptr::operator!=()

template<typename T>
claw::memory::smart_ptr< T >::pointer claw::memory::smart_ptr< T >::operator-> (  )  [inline]

Dereference operator.

Definition at line 133 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr.

00134 {
00135   return m_ptr;
00136 } // smart_ptr::operator->()

template<typename T>
claw::memory::smart_ptr< T >::const_pointer claw::memory::smart_ptr< T >::operator-> (  )  const [inline]

Dereference operator.

Definition at line 144 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr.

00145 {
00146   return m_ptr;
00147 } // smart_ptr::operator->()

template<typename T>
claw::memory::smart_ptr< T >::reference claw::memory::smart_ptr< T >::operator* (  )  [inline]

Dereference operator.

Definition at line 155 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr.

00156 {
00157   return *m_ptr;
00158 } // smart_ptr::operator*()

template<typename T>
claw::memory::smart_ptr< T >::const_reference claw::memory::smart_ptr< T >::operator* (  )  const [inline]

Dereference operator.

Definition at line 166 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr.

00167 {
00168   return *m_ptr;
00169 } // smart_ptr::operator*()

template<typename T>
void claw::memory::smart_ptr< T >::copy ( const self_type that  )  [inline, private]

Copy a smart_ptr.

Parameters:
that The smart_pointer to copy.

Definition at line 177 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr, and claw::memory::smart_ptr< T >::m_ref_count.

Referenced by claw::memory::smart_ptr< T >::operator=(), and claw::memory::smart_ptr< T >::smart_ptr().

00178 {
00179   assert( this != &that );
00180 
00181   m_ref_count = that.m_ref_count;
00182   m_ptr = that.m_ptr;
00183 
00184   if (m_ref_count)
00185     ++(*m_ref_count);
00186 } // smart_ptr::copy()

template<typename T>
void claw::memory::smart_ptr< T >::release (  )  [inline, private]

Release the allocated memory.

The memory is release only if no more smart_ptr point on it.

Definition at line 195 of file smart_ptr.tpp.

References claw::memory::smart_ptr< T >::m_ptr, and claw::memory::smart_ptr< T >::m_ref_count.

Referenced by claw::memory::smart_ptr< T >::operator=(), and claw::memory::smart_ptr< T >::~smart_ptr().

00196 {
00197   if (m_ref_count)
00198     if ( *m_ref_count )
00199       {
00200   --(*m_ref_count);
00201 
00202   if ( !(*m_ref_count) )
00203     {
00204       delete m_ptr;
00205       delete m_ref_count;
00206         
00207       m_ref_count = NULL;
00208     }
00209     
00210   m_ptr = NULL;
00211       }
00212 } // smart_ptr::release()


Member Data Documentation

template<typename T>
unsigned int* claw::memory::smart_ptr< T >::m_ref_count [private]

template<typename T>
pointer claw::memory::smart_ptr< T >::m_ptr [private]


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

Generated on Thu Jun 26 09:35:07 2008 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.5.6