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

A pointer with a reference counter. More...

#include <smart_ptr.hpp>

List of all members.

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.
bool operator< (const self_type &that) const
 "Less than" operator.
bool operator<= (const self_type &that) const
 "Less or equal" operator.
bool operator> (const self_type &that) const
 "Greater than" operator.
bool operator>= (const self_type &that) const
 "Greater or equal" 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.

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.


Member Typedef Documentation

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.

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 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 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 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 >::value_type

The type of the pointed data.

Definition at line 55 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 >
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 228 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().

00229 {
00230   assert( this != &that );
00231 
00232   m_ref_count = that.m_ref_count;
00233   m_ptr = that.m_ptr;
00234 
00235   if (m_ref_count)
00236     ++(*m_ref_count);
00237 } // smart_ptr::copy()

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

Disequality operator.

Parameters:
that The pointer to compare to.
Returns:
(*this < that) || (that < *this).

Definition at line 124 of file smart_ptr.tpp.

00125 {
00126   return (*this < that) || (that < *this);
00127 } // 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 217 of file smart_ptr.tpp.

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

00218 {
00219   return *m_ptr;
00220 } // smart_ptr::operator*()

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

Dereference operator.

Definition at line 206 of file smart_ptr.tpp.

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

00207 {
00208   return *m_ptr;
00209 } // 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 195 of file smart_ptr.tpp.

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

00196 {
00197   return m_ptr;
00198 } // smart_ptr::operator->()

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

Dereference operator.

Definition at line 184 of file smart_ptr.tpp.

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

00185 {
00186   return m_ptr;
00187 } // smart_ptr::operator->()

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

"Less than" operator.

Parameters:
that The pointer to compare to.
Returns:
True if the address pointed by this is lower than the address pointed by that.

Definition at line 137 of file smart_ptr.tpp.

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

00138 {
00139   return m_ptr < that.m_ptr;
00140 } // smart_ptr::operator<()

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

"Less or equal" operator.

Parameters:
that The pointer to compare to.
Returns:
!(that < *this).

Definition at line 149 of file smart_ptr.tpp.

00150 {
00151   return !(that < *this);
00152 } // smart_ptr::operator<=()

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.

Parameters:
that The pointer to compare to.
Returns:
!(*this < that) && !(that < *this).

Definition at line 112 of file smart_ptr.tpp.

00113 {
00114   return !(*this < that) && !(that < *this);
00115 } // smart_ptr::operator==()

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

"Greater than" operator.

Parameters:
that The pointer to compare to.
Returns:
that < *this.

Definition at line 161 of file smart_ptr.tpp.

00162 {
00163   return that < *this;
00164 } // smart_ptr::operator>()

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

"Greater or equal" operator.

Parameters:
that The pointer to compare to.
Returns:
!(*this < that).

Definition at line 173 of file smart_ptr.tpp.

00174 {
00175   return !(*this < that);
00176 } // smart_ptr::operator>=()

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 246 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().

00247 {
00248   if (m_ref_count)
00249     if ( *m_ref_count )
00250       {
00251   --(*m_ref_count);
00252 
00253   if ( !(*m_ref_count) )
00254     {
00255       delete m_ptr;
00256       delete m_ref_count;
00257         
00258       m_ref_count = NULL;
00259     }
00260     
00261   m_ptr = NULL;
00262       }
00263 } // smart_ptr::release()


Member Data Documentation

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

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

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1