Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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
    00104     //-----------------------------------------------------------------------
    00105     template <class T, template <class> class Ownership>
    00106     inline CSmartPtr<T, Ownership>::CSmartPtr(const CSmartPtr<T, Ownership>& Copy) :
    00107     Ownership<T>(Copy),
    00108     m_pData      (Clone(Copy.m_pData))
    00109     {
    00110
    00111     }
    00112
    00113     //-----------------------------------------------------------------------
    00114     template <class T, template <class> class Ownership>
    00115     inline CSmartPtr<T, Ownership>::CSmartPtr(T* pPointer) :
    00116     m_pData(pPointer)
    00117     {
    00118
    00119     }
    00120
    00121     //-----------------------------------------------------------------------
    00122     template <class T, template <class> class Ownership>
    00123     inline CSmartPtr<T, Ownership>::~CSmartPtr()
    00124     {
    00125         Release(m_pData);
    00126     }
    00127
    00128     //-----------------------------------------------------------------------
    00129     template <class T, template <class> class Ownership>
    00130     inline void CSmartPtr<T, Ownership>::Swap(CSmartPtr<T, Ownership>& Ptr)
    00131     {
    00132         Ownership<T>::Swap(Ptr);
    00133         std::swap(m_pData, Ptr.m_pData);
    00134     }
    00135
    00136     //-----------------------------------------------------------------------
    00137     template <class T, template <class> class Ownership>
    00138     inline T& CSmartPtr<T, Ownership>::operator *() const
    00139     {
    00140         Assert(m_pData != NULL);
    00141
    00142         return *m_pData;
    00143     }
    00144
    00145     //-----------------------------------------------------------------------
    00146     template <class T, template <class> class Ownership>
    00147     inline T* CSmartPtr<T, Ownership>::operator ->() const
    00148     {
    00149         Assert(m_pData != NULL);
    00150
    00151         return m_pData;
    00152     }
    00153
    00154     //-----------------------------------------------------------------------
    00155     template <class T, template <class> class Ownership>
    00156     inline const CSmartPtr<T, Ownership>& CSmartPtr<T, Ownership>::operator =(const CSmartPtr<T, Ownership>& Pointer)
    00157     {
    00158         CSmartPtr<T, Ownership>(Pointer).Swap(*this);
    00159
    00160         return *this;
    00161     }
    00162
    00163     //-----------------------------------------------------------------------
    00164     template <class T, template <class> class Ownership>
    00165     inline CSmartPtr<T, Ownership>::operator T*() const
    00166     {
    00167         return m_pData;
    00168     }
    00169
    00170 } // namespace Keos
    00171
    00172 #endif // KEOS_SMARTPTR_H