KeosSmartPtr.h

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #ifndef KEOS_SMARTPTR_H
00023 #define KEOS_SMARTPTR_H
00024 
00025 #include "KeosSmartPtrPolicies.h"
00026 #include "KeosException.h"
00027 
00028 namespace Keos
00029 {
00030   //-----------------------------------------------------------------------
00034   template <class T, template <class> class Ownership = CRefCount >
00035   class CSmartPtr : public Ownership<T>
00036   {
00037   public :
00038 
00042     CSmartPtr();
00043 
00047     CSmartPtr(const CSmartPtr& Copy);
00048 
00052     CSmartPtr(T* pPointer);
00053 
00057     ~CSmartPtr();
00058 
00062     T& operator * () const;
00063 
00067     T* operator ->() const;
00068 
00073     const CSmartPtr& operator =(const CSmartPtr& Pointer);
00074 
00078     operator T*() const;
00079 
00080   private :
00081 
00085     void Swap(CSmartPtr& Ptr);
00086 
00088     T* m_pData;
00089   };
00090 
00091 
00092   //=======================================================================
00093   // CSmartPtr inline functions
00094   //=======================================================================
00095 
00096   //-----------------------------------------------------------------------
00097   template <class T, template <class> class Ownership >
00098   inline CSmartPtr<T, Ownership>::CSmartPtr() :
00099       m_pData(NULL)
00100   {}
00101 
00102   //-----------------------------------------------------------------------
00103   template <class T, template <class> class Ownership >
00104   inline CSmartPtr<T, Ownership>::CSmartPtr(const CSmartPtr<T, Ownership>& Copy) :
00105       Ownership<T>(Copy),
00106       m_pData      (Clone(Copy.m_pData))
00107   {}
00108 
00109   //-----------------------------------------------------------------------
00110   template <class T, template <class> class Ownership >
00111   inline CSmartPtr<T, Ownership>::CSmartPtr(T* pPointer) :
00112       m_pData(pPointer)
00113   {}
00114 
00115   //-----------------------------------------------------------------------
00116   template <class T, template <class> class Ownership >
00117   inline CSmartPtr<T, Ownership>::~CSmartPtr()
00118   {
00119     Release(m_pData);
00120   }
00121 
00122   //-----------------------------------------------------------------------
00123   template <class T, template <class> class Ownership >
00124   inline void CSmartPtr<T, Ownership>::Swap(CSmartPtr<T, Ownership>& Ptr)
00125   {
00126     Ownership<T>::Swap(Ptr);
00127     std::swap(m_pData, Ptr.m_pData);
00128   }
00129 
00130   //-----------------------------------------------------------------------
00131   template <class T, template <class> class Ownership >
00132   inline T& CSmartPtr<T, Ownership>::operator *() const
00133   {
00134     Assert(m_pData != NULL);
00135 
00136     return *m_pData;
00137   }
00138 
00139   //-----------------------------------------------------------------------
00140   template <class T, template <class> class Ownership >
00141   inline T* CSmartPtr<T, Ownership>::operator ->() const
00142   {
00143     Assert(m_pData != NULL);
00144 
00145     return m_pData;
00146   }
00147 
00148   //-----------------------------------------------------------------------
00149   template <class T, template <class> class Ownership >
00150   inline const CSmartPtr<T, Ownership>& CSmartPtr<T, Ownership>::operator =(const CSmartPtr<T, Ownership>& Pointer)
00151   {
00152     CSmartPtr<T, Ownership>(Pointer).Swap(*this);
00153 
00154     return *this;
00155   }
00156 
00157   //-----------------------------------------------------------------------
00158   template <class T, template <class> class Ownership >
00159   inline CSmartPtr<T, Ownership>::operator T*() const
00160   {
00161     return m_pData;
00162   }
00163 
00164 } // namespace Keos
00165 
00166 #endif // KEOS_SMARTPTR_H

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1