A pointer with a reference counter. More...
#include <smart_ptr.hpp>
Public Types | |
typedef T | value_type |
The type of the pointed data. | |
typedef smart_ptr< value_type > | self_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_type & | operator= (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. |
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:
Definition at line 51 of file smart_ptr.hpp.
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.
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.
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.
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.
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.
typedef T claw::memory::smart_ptr< T >::value_type |
The type of the pointed data.
Definition at line 55 of file smart_ptr.hpp.
claw::memory::smart_ptr< T >::smart_ptr | ( | ) |
Default constructor.
Definition at line 38 of file smart_ptr.tpp.
: m_ref_count(NULL), m_ptr(NULL) { } // smart_ptr::smart_ptr()
claw::memory::smart_ptr< T >::smart_ptr | ( | pointer | data | ) |
Constructor from a pointer.
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.
: m_ref_count(NULL), m_ptr(NULL) { if (data) { m_ref_count = new unsigned int(1); m_ptr = data; } } // smart_ptr::smart_ptr() [pointer]
claw::memory::smart_ptr< T >::smart_ptr | ( | const self_type & | that | ) |
Copy constructor.
that | The smart_pointer to copy. |
Definition at line 72 of file smart_ptr.tpp.
References claw::memory::smart_ptr< T >::copy().
{ copy( that ); } // smart_ptr::smart_ptr() [copy]
claw::memory::smart_ptr< T >::~smart_ptr | ( | ) |
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().
{ release(); } // smart_ptr::~smart_ptr()
void claw::memory::smart_ptr< T >::copy | ( | const self_type & | that | ) | [private] |
Copy a smart_ptr.
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().
{ assert( this != &that ); m_ref_count = that.m_ref_count; m_ptr = that.m_ptr; if (m_ref_count) ++(*m_ref_count); } // smart_ptr::copy()
bool claw::memory::smart_ptr< T >::operator!= | ( | const self_type & | that | ) | const |
Disequality operator.
that | The pointer to compare to. |
Definition at line 124 of file smart_ptr.tpp.
{ return (*this < that) || (that < *this); } // smart_ptr::operator!=()
claw::memory::smart_ptr< T >::reference claw::memory::smart_ptr< T >::operator* | ( | ) |
Dereference operator.
Definition at line 206 of file smart_ptr.tpp.
References claw::memory::smart_ptr< T >::m_ptr.
{ return *m_ptr; } // smart_ptr::operator*()
claw::memory::smart_ptr< T >::const_reference claw::memory::smart_ptr< T >::operator* | ( | ) | const |
Dereference operator.
Definition at line 217 of file smart_ptr.tpp.
References claw::memory::smart_ptr< T >::m_ptr.
{ return *m_ptr; } // smart_ptr::operator*()
claw::memory::smart_ptr< T >::const_pointer claw::memory::smart_ptr< T >::operator-> | ( | ) | const |
Dereference operator.
Definition at line 195 of file smart_ptr.tpp.
References claw::memory::smart_ptr< T >::m_ptr.
{ return m_ptr; } // smart_ptr::operator->()
claw::memory::smart_ptr< T >::pointer claw::memory::smart_ptr< T >::operator-> | ( | ) |
Dereference operator.
Definition at line 184 of file smart_ptr.tpp.
References claw::memory::smart_ptr< T >::m_ptr.
{ return m_ptr; } // smart_ptr::operator->()
bool claw::memory::smart_ptr< T >::operator< | ( | const self_type & | that | ) | const |
"Less than" operator.
that | The pointer to compare to. |
Definition at line 137 of file smart_ptr.tpp.
References claw::memory::smart_ptr< T >::m_ptr.
{ return m_ptr < that.m_ptr; } // smart_ptr::operator<()
bool claw::memory::smart_ptr< T >::operator<= | ( | const self_type & | that | ) | const |
"Less or equal" operator.
that | The pointer to compare to. |
Definition at line 149 of file smart_ptr.tpp.
{ return !(that < *this); } // smart_ptr::operator<=()
claw::memory::smart_ptr< T >::self_type & claw::memory::smart_ptr< T >::operator= | ( | const self_type & | that | ) |
Assignment operator.
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().
bool claw::memory::smart_ptr< T >::operator== | ( | const self_type & | that | ) | const |
Equality operator.
that | The pointer to compare to. |
Definition at line 112 of file smart_ptr.tpp.
{ return !(*this < that) && !(that < *this); } // smart_ptr::operator==()
bool claw::memory::smart_ptr< T >::operator> | ( | const self_type & | that | ) | const |
"Greater than" operator.
that | The pointer to compare to. |
Definition at line 161 of file smart_ptr.tpp.
{ return that < *this; } // smart_ptr::operator>()
bool claw::memory::smart_ptr< T >::operator>= | ( | const self_type & | that | ) | const |
"Greater or equal" operator.
that | The pointer to compare to. |
Definition at line 173 of file smart_ptr.tpp.
{ return !(*this < that); } // smart_ptr::operator>=()
void claw::memory::smart_ptr< T >::release | ( | ) | [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().
{ if (m_ref_count) if ( *m_ref_count ) { --(*m_ref_count); if ( !(*m_ref_count) ) { delete m_ptr; delete m_ref_count; m_ref_count = NULL; } m_ptr = NULL; } } // smart_ptr::release()
pointer claw::memory::smart_ptr< T >::m_ptr [private] |
The pointed item.
Definition at line 101 of file smart_ptr.hpp.
Referenced by claw::memory::smart_ptr< T >::copy(), claw::memory::smart_ptr< T >::operator*(), claw::memory::smart_ptr< T >::operator->(), claw::memory::smart_ptr< T >::operator<(), claw::memory::smart_ptr< T >::release(), and claw::memory::smart_ptr< T >::smart_ptr().
unsigned int* claw::memory::smart_ptr< T >::m_ref_count [private] |
Number of smart_ptr pointing on this memory area.
Definition at line 98 of file smart_ptr.hpp.
Referenced by claw::memory::smart_ptr< T >::copy(), claw::memory::smart_ptr< T >::release(), and claw::memory::smart_ptr< T >::smart_ptr().