Table of Contents

include/AK/Tools/Common/AkSmartPtr.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
00003 released in source code form as part of the SDK installer package.
00004 
00005 Commercial License Usage
00006 
00007 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
00008 may use this file in accordance with the end user license agreement provided 
00009 with the software or, alternatively, in accordance with the terms contained in a
00010 written agreement between you and Audiokinetic Inc.
00011 
00012 Apache License Usage
00013 
00014 Alternatively, this file may be used under the Apache License, Version 2.0 (the 
00015 "Apache License"); you may not use this file except in compliance with the 
00016 Apache License. You may obtain a copy of the Apache License at 
00017 http://www.apache.org/licenses/LICENSE-2.0.
00018 
00019 Unless required by applicable law or agreed to in writing, software distributed
00020 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
00021 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
00022 the specific language governing permissions and limitations under the License.
00023 
00024   Version: <VERSION>  Build: <BUILDNUMBER>
00025   Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
00026 *******************************************************************************/
00027 
00028 // AkSmartPtr.h
00029 
00030 /// \file 
00031 /// Helper file.
00032 
00033 #ifndef _AK_SMARTPTR_H
00034 #define _AK_SMARTPTR_H
00035 
00036 #include <AK/SoundEngine/Common/AkTypes.h>
00037 
00038 template <class T> class CAkSmartPtr
00039 {
00040 public:
00041     /// Smart pointer constructor
00042     AkForceInline CAkSmartPtr()
00043         : m_pT( NULL )
00044     {
00045     }
00046 
00047     /// Smart pointer constructor
00048     AkForceInline CAkSmartPtr( T* in_pT )
00049     {
00050         m_pT = in_pT;
00051         if (m_pT)
00052             m_pT->AddRef();
00053     }
00054 
00055     /// Smart pointer constructor
00056     AkForceInline CAkSmartPtr( const CAkSmartPtr<T>& in_rPtr )
00057     {
00058         m_pT = in_rPtr.m_pT;
00059         if (m_pT)
00060             m_pT->AddRef();
00061     }
00062 
00063     /// Smart pointer destructor
00064     ~CAkSmartPtr()
00065     {
00066         Release();
00067     }
00068 
00069     /// Release
00070     AkForceInline void Release()
00071     {
00072         if( m_pT )
00073         {
00074             m_pT->Release();
00075             m_pT = NULL;
00076         }
00077     }
00078 
00079     /// Assign with no Addref.
00080     AkForceInline void Attach( T* in_pObj )
00081     {
00082         _Assign( in_pObj, false );   
00083     }
00084 
00085     /// Give the pointer without releasing it.
00086     AkForceInline T* Detach()
00087     {
00088         T* pObj = m_pT;
00089         m_pT = NULL;
00090 
00091         return pObj;
00092     }
00093 
00094     /// Assignation operator</span>
00095     const CAkSmartPtr<T>& operator=( const CAkSmartPtr<T>& in_pObj )
00096     {
00097         _Assign( in_pObj.m_pT );
00098         return *this;
00099     }
00100 
00101     /// Assignation operator</span>
00102     const CAkSmartPtr<T>& operator=( T* in_pObj )
00103     {
00104         _Assign( in_pObj );
00105         return *this;
00106     }
00107 
00108     /// Operator *
00109     T& operator*() { return *m_pT; }
00110 
00111     /// Operator ->
00112     T* operator->() const { return m_pT; }
00113 
00114     /// Operator
00115     operator T*() const { return m_pT; }
00116 
00117     /// Operators to pass to functions like QueryInterface and other functions returning an addref'd pointer.
00118     T** operator &() { return &m_pT; }
00119 
00120     /// Operator *
00121     const T& operator*() const { return *m_pT; }
00122 
00123     /// Cast
00124     T* Cast() { return m_pT; }
00125 
00126     /// Cast
00127     const T* Cast() const { return m_pT; }
00128 
00129 protected:
00130 
00131     /// internal use only
00132     void _Assign( T* in_pObj, bool in_bAddRef = true )
00133     {
00134         if (in_pObj != NULL && in_bAddRef)
00135             in_pObj->AddRef();
00136 
00137         // Must use a local pointer since we cannot call Release(); without having set the m_pT to its final value.
00138         T* l_Ptr = m_pT;
00139         m_pT = in_pObj;
00140         if (l_Ptr)
00141             l_Ptr->Release();
00142     }
00143 
00144     /// internal use only
00145     bool _Compare( const T* in_pObj ) const
00146     {
00147         return m_pT == in_pObj;
00148     }
00149 
00150     /// internal use only
00151     T* m_pT; 
00152 };
00153 
00154 #endif // _AK_SMARTPTR_H
00155