AkSmartPtr.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. // AkSmartPtr.h
  21. /// \file
  22. /// Helper file.
  23. #ifndef _AK_SMARTPTR_H
  24. #define _AK_SMARTPTR_H
  25. #include <AK/SoundEngine/Common/AkTypes.h>
  26. template <class T> class CAkSmartPtr
  27. {
  28. public:
  29. /// Smart pointer constructor
  30. AkForceInline CAkSmartPtr()
  31. : m_pT( NULL )
  32. {
  33. }
  34. /// Smart pointer constructor
  35. AkForceInline CAkSmartPtr( T* in_pT )
  36. {
  37. m_pT = in_pT;
  38. if (m_pT)
  39. m_pT->AddRef();
  40. }
  41. /// Smart pointer copy constructor
  42. AkForceInline CAkSmartPtr( const CAkSmartPtr<T>& in_rPtr )
  43. {
  44. m_pT = in_rPtr.m_pT;
  45. if (m_pT)
  46. m_pT->AddRef();
  47. }
  48. /// Smart pointer move constructor
  49. AkForceInline CAkSmartPtr( CAkSmartPtr<T>&& in_rPtr )
  50. {
  51. m_pT = in_rPtr.m_pT;
  52. in_rPtr.m_pT = NULL;
  53. }
  54. /// Smart pointer destructor
  55. ~CAkSmartPtr()
  56. {
  57. Release();
  58. }
  59. /// Release
  60. AkForceInline void Release()
  61. {
  62. if( m_pT )
  63. {
  64. m_pT->Release();
  65. m_pT = NULL;
  66. }
  67. }
  68. /// Assign with no Addref.
  69. AkForceInline void Attach( T* in_pObj )
  70. {
  71. _Assign( in_pObj, false );
  72. }
  73. /// Give the pointer without releasing it.
  74. AkForceInline T* Detach()
  75. {
  76. T* pObj = m_pT;
  77. m_pT = NULL;
  78. return pObj;
  79. }
  80. /// Copy Assignation operator
  81. const CAkSmartPtr<T>& operator=( const CAkSmartPtr<T>& in_pObj )
  82. {
  83. _Assign( in_pObj.m_pT );
  84. return *this;
  85. }
  86. /// Move Assignation operator
  87. CAkSmartPtr<T>& operator=( CAkSmartPtr<T>&& in_pObj )
  88. {
  89. _Assign( in_pObj.m_pT, false );
  90. in_pObj.m_pT = NULL;
  91. return *this;
  92. }
  93. /// Assignation operator
  94. const CAkSmartPtr<T>& operator=( T* in_pObj )
  95. {
  96. _Assign( in_pObj );
  97. return *this;
  98. }
  99. /// Operator *
  100. T& operator*() { return *m_pT; }
  101. /// Operator ->
  102. T* operator->() const { return m_pT; }
  103. /// Operator
  104. operator T*() const { return m_pT; }
  105. /// Operators to pass to functions like QueryInterface and other functions returning an addref'd pointer.
  106. T** operator &() { return &m_pT; }
  107. /// Operator *
  108. const T& operator*() const { return *m_pT; }
  109. /// Cast
  110. T* Cast() { return m_pT; }
  111. /// Cast
  112. const T* Cast() const { return m_pT; }
  113. protected:
  114. /// internal use only
  115. void _Assign( T* in_pObj, bool in_bAddRef = true )
  116. {
  117. if (in_pObj != NULL && in_bAddRef)
  118. in_pObj->AddRef();
  119. // Must use a local pointer since we cannot call Release(); without having set the m_pT to its final value.
  120. T* l_Ptr = m_pT;
  121. m_pT = in_pObj;
  122. if (l_Ptr)
  123. l_Ptr->Release();
  124. }
  125. /// internal use only
  126. bool _Compare( const T* in_pObj ) const
  127. {
  128. return m_pT == in_pObj;
  129. }
  130. /// internal use only
  131. T* m_pT;
  132. };
  133. #endif // _AK_SMARTPTR_H