smart_ptr.tpp

Go to the documentation of this file.
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 /*----------------------------------------------------------------------------*/
00110 template<typename T>
00111 bool claw::memory::smart_ptr<T>::operator==( const self_type& that ) const
00112 {
00113   return m_ptr == that.m_ptr;
00114 } // smart_ptr::operator==()
00115 
00116 /*----------------------------------------------------------------------------*/
00121 template<typename T>
00122 bool claw::memory::smart_ptr<T>::operator!=( const self_type& that ) const
00123 {
00124   return !( (*this) == that );
00125 } // smart_ptr::operator!=()
00126 
00127 /*----------------------------------------------------------------------------*/
00131 template<typename T>
00132 typename claw::memory::smart_ptr<T>::pointer
00133 claw::memory::smart_ptr<T>::operator->()
00134 {
00135   return m_ptr;
00136 } // smart_ptr::operator->()
00137 
00138 /*----------------------------------------------------------------------------*/
00142 template<typename T>
00143 typename claw::memory::smart_ptr<T>::const_pointer
00144 claw::memory::smart_ptr<T>::operator->() const
00145 {
00146   return m_ptr;
00147 } // smart_ptr::operator->()
00148 
00149 /*----------------------------------------------------------------------------*/
00153 template<typename T>
00154 typename claw::memory::smart_ptr<T>::reference
00155 claw::memory::smart_ptr<T>::operator*()
00156 {
00157   return *m_ptr;
00158 } // smart_ptr::operator*()
00159 
00160 /*----------------------------------------------------------------------------*/
00164 template<typename T>
00165 typename claw::memory::smart_ptr<T>::const_reference
00166 claw::memory::smart_ptr<T>::operator*() const
00167 {
00168   return *m_ptr;
00169 } // smart_ptr::operator*()
00170 
00171 /*----------------------------------------------------------------------------*/
00176 template<typename T>
00177 void claw::memory::smart_ptr<T>::copy( const self_type& that )
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()
00187 
00188 /*----------------------------------------------------------------------------*/
00194 template<typename T>
00195 void claw::memory::smart_ptr<T>::release()
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()

Generated on Thu Jun 26 09:35:04 2008 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.5.6