00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2008 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien_jorge@yahoo.fr 00024 */ 00030 #include <cassert> 00031 #include <cstdlib> 00032 00033 /*----------------------------------------------------------------------------*/ 00037 template<typename T> 00038 claw::memory::smart_ptr<T>::smart_ptr() 00039 : m_ref_count(NULL), m_ptr(NULL) 00040 { 00041 00042 } // smart_ptr::smart_ptr() 00043 00044 /*----------------------------------------------------------------------------*/ 00055 template<typename T> 00056 claw::memory::smart_ptr<T>::smart_ptr( pointer data ) 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] 00065 00066 /*----------------------------------------------------------------------------*/ 00071 template<typename T> 00072 claw::memory::smart_ptr<T>::smart_ptr( const self_type& that ) 00073 { 00074 copy( that ); 00075 } // smart_ptr::smart_ptr() [copy] 00076 00077 /*----------------------------------------------------------------------------*/ 00081 template<typename T> 00082 claw::memory::smart_ptr<T>::~smart_ptr() 00083 { 00084 release(); 00085 } // smart_ptr::~smart_ptr() 00086 00087 /*----------------------------------------------------------------------------*/ 00092 template<typename T> 00093 typename claw::memory::smart_ptr<T>::self_type& 00094 claw::memory::smart_ptr<T>::operator=( const self_type& that ) 00095 { 00096 if ( &that != this ) 00097 { 00098 release(); 00099 copy( that ); 00100 } 00101 00102 return *this; 00103 } // smart_ptr::operator=() 00104 00105 /*----------------------------------------------------------------------------*/ 00111 template<typename T> 00112 bool claw::memory::smart_ptr<T>::operator==( const self_type& that ) const 00113 { 00114 return !(*this < that) && !(that < *this); 00115 } // smart_ptr::operator==() 00116 00117 /*----------------------------------------------------------------------------*/ 00123 template<typename T> 00124 bool claw::memory::smart_ptr<T>::operator!=( const self_type& that ) const 00125 { 00126 return (*this < that) || (that < *this); 00127 } // smart_ptr::operator!=() 00128 00129 /*----------------------------------------------------------------------------*/ 00136 template<typename T> 00137 bool claw::memory::smart_ptr<T>::operator<( const self_type& that ) const 00138 { 00139 return m_ptr < that.m_ptr; 00140 } // smart_ptr::operator<() 00141 00142 /*----------------------------------------------------------------------------*/ 00148 template<typename T> 00149 bool claw::memory::smart_ptr<T>::operator<=( const self_type& that ) const 00150 { 00151 return !(that < *this); 00152 } // smart_ptr::operator<=() 00153 00154 /*----------------------------------------------------------------------------*/ 00160 template<typename T> 00161 bool claw::memory::smart_ptr<T>::operator>( const self_type& that ) const 00162 { 00163 return that < *this; 00164 } // smart_ptr::operator>() 00165 00166 /*----------------------------------------------------------------------------*/ 00172 template<typename T> 00173 bool claw::memory::smart_ptr<T>::operator>=( const self_type& that ) const 00174 { 00175 return !(*this < that); 00176 } // smart_ptr::operator>=() 00177 00178 /*----------------------------------------------------------------------------*/ 00182 template<typename T> 00183 typename claw::memory::smart_ptr<T>::pointer 00184 claw::memory::smart_ptr<T>::operator->() 00185 { 00186 return m_ptr; 00187 } // smart_ptr::operator->() 00188 00189 /*----------------------------------------------------------------------------*/ 00193 template<typename T> 00194 typename claw::memory::smart_ptr<T>::const_pointer 00195 claw::memory::smart_ptr<T>::operator->() const 00196 { 00197 return m_ptr; 00198 } // smart_ptr::operator->() 00199 00200 /*----------------------------------------------------------------------------*/ 00204 template<typename T> 00205 typename claw::memory::smart_ptr<T>::reference 00206 claw::memory::smart_ptr<T>::operator*() 00207 { 00208 return *m_ptr; 00209 } // smart_ptr::operator*() 00210 00211 /*----------------------------------------------------------------------------*/ 00215 template<typename T> 00216 typename claw::memory::smart_ptr<T>::const_reference 00217 claw::memory::smart_ptr<T>::operator*() const 00218 { 00219 return *m_ptr; 00220 } // smart_ptr::operator*() 00221 00222 /*----------------------------------------------------------------------------*/ 00227 template<typename T> 00228 void claw::memory::smart_ptr<T>::copy( const self_type& that ) 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() 00238 00239 /*----------------------------------------------------------------------------*/ 00245 template<typename T> 00246 void claw::memory::smart_ptr<T>::release() 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()