#include <smart_ptr.hpp>
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.
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. | |
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. |
typedef T claw::memory::smart_ptr< T >::value_type |
typedef smart_ptr<value_type> claw::memory::smart_ptr< T >::self_type |
typedef T& claw::memory::smart_ptr< T >::reference |
typedef T* claw::memory::smart_ptr< T >::pointer |
typedef const T& claw::memory::smart_ptr< T >::const_reference |
typedef const T* const claw::memory::smart_ptr< T >::const_pointer |
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()
claw::memory::smart_ptr< T >::smart_ptr | ( | pointer | data | ) | [inline] |
Constructor from a pointer.
data | Pointer on the data. |
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]
claw::memory::smart_ptr< T >::smart_ptr | ( | const self_type & | that | ) | [inline] |
Copy constructor.
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]
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()
claw::memory::smart_ptr< T >::self_type & claw::memory::smart_ptr< T >::operator= | ( | const self_type & | that | ) | [inline] |
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().
00095 { 00096 if ( &that != this ) 00097 { 00098 release(); 00099 copy( that ); 00100 } 00101 00102 return *this; 00103 } // smart_ptr::operator=()
bool claw::memory::smart_ptr< T >::operator== | ( | const self_type & | that | ) | const [inline] |
Equality operator.
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==()
bool claw::memory::smart_ptr< T >::operator!= | ( | const self_type & | that | ) | const [inline] |
Disequality operator.
Definition at line 122 of file smart_ptr.tpp.
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->()
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->()
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*()
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*()
void claw::memory::smart_ptr< T >::copy | ( | const self_type & | that | ) | [inline, private] |
Copy a smart_ptr.
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()
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()
unsigned int* claw::memory::smart_ptr< T >::m_ref_count [private] |
Number of smart_ptr pointing on this memory area.
Definition at line 92 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().
pointer claw::memory::smart_ptr< T >::m_ptr [private] |
The pointed item.
Definition at line 95 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().