目录

include/AK/Tools/Common/AkSmartPtr.h

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright (c) 2006 Audiokinetic Inc. / All Rights Reserved
00004 //
00006 
00007 // AkSmartPtr.h
00008 
00011 
00012 #ifndef _AK_SMARTPTR_H
00013 #define _AK_SMARTPTR_H
00014 
00015 #include <AK/SoundEngine/Common/AkTypes.h>
00016 
00017 template <class T> class CAkSmartPtr
00018 {
00019 public:
00021     AkForceInline CAkSmartPtr()
00022         : m_pT( NULL )
00023     {
00024     }
00025 
00027     AkForceInline CAkSmartPtr( T* in_pT )
00028     {
00029         m_pT = in_pT;
00030         if (m_pT)
00031             m_pT->AddRef();
00032     }
00033 
00035     AkForceInline CAkSmartPtr( const CAkSmartPtr<T>& in_rPtr )
00036     {
00037         m_pT = in_rPtr.m_pT;
00038         if (m_pT)
00039             m_pT->AddRef();
00040     }
00041 
00043     ~CAkSmartPtr()
00044     {
00045         Release();
00046     }
00047 
00049     AkForceInline void Release()
00050     {
00051         if( m_pT )
00052         {
00053             m_pT->Release();
00054             m_pT = NULL;
00055         }
00056     }
00057 
00059     AkForceInline void Attach( T* in_pObj )
00060     {
00061         _Assign( in_pObj, false );   
00062     }
00063 
00065     AkForceInline T* Detach()
00066     {
00067         T* pObj = m_pT;
00068         m_pT = NULL;
00069 
00070         return pObj;
00071     }
00072 
00074     const CAkSmartPtr<T>& operator=( const CAkSmartPtr<T>& in_pObj )
00075     {
00076         _Assign( in_pObj.m_pT );
00077         return *this;
00078     }
00079 
00081     const CAkSmartPtr<T>& operator=( T* in_pObj )
00082     {
00083         _Assign( in_pObj );
00084         return *this;
00085     }
00086 
00088     T& operator*() { return *m_pT; }
00089 
00091     T* operator->() const { return m_pT; }
00092 
00094     operator T*() const { return m_pT; }
00095 
00097     T** operator &() { return &m_pT; }
00098 
00100     const T& operator*() const { return *m_pT; }
00101 
00103     T* Cast() { return m_pT; }
00104 
00106     const T* Cast() const { return m_pT; }
00107 
00108 protected:
00109 
00111     void _Assign( T* in_pObj, bool in_bAddRef = true )
00112     {
00113         if (in_pObj != NULL && in_bAddRef)
00114             in_pObj->AddRef();
00115 
00116         // Must use a local pointer since we cannot call Release(); without having set the m_pT to its final value.
00117         T* l_Ptr = m_pT;
00118         m_pT = in_pObj;
00119         if (l_Ptr)
00120             l_Ptr->Release();
00121     }
00122 
00124     bool _Compare( const T* in_pObj ) const
00125     {
00126         return m_pT == in_pObj;
00127     }
00128 
00130     T* m_pT; 
00131 };
00132 
00133 #endif // _AK_SMARTPTR_H
00134