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 | ( | ) | [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. |
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]
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()
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 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()
bool claw::memory::smart_ptr< T >::operator!= | ( | const self_type & | that | ) | const [inline] |
Disequality operator.
that | The pointer to compare to. |
Definition at line 124 of file smart_ptr.tpp.
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*()
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*()
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->()
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->()
bool claw::memory::smart_ptr< T >::operator< | ( | const self_type & | that | ) | const [inline] |
"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.
00138 { 00139 return m_ptr < that.m_ptr; 00140 } // smart_ptr::operator<()
bool claw::memory::smart_ptr< T >::operator<= | ( | const self_type & | that | ) | const [inline] |
"Less or equal" operator.
that | The pointer to compare to. |
Definition at line 149 of file smart_ptr.tpp.
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().
bool claw::memory::smart_ptr< T >::operator== | ( | const self_type & | that | ) | const [inline] |
Equality operator.
that | The pointer to compare to. |
Definition at line 112 of file smart_ptr.tpp.
bool claw::memory::smart_ptr< T >::operator> | ( | const self_type & | that | ) | const [inline] |
"Greater than" operator.
that | The pointer to compare to. |
Definition at line 161 of file smart_ptr.tpp.
bool claw::memory::smart_ptr< T >::operator>= | ( | const self_type & | that | ) | const [inline] |
"Greater or equal" operator.
that | The pointer to compare to. |
Definition at line 173 of file smart_ptr.tpp.
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()
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().