AkMixerInputMap.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef _AK_MIXERINPUTMAP_H_
  21. #define _AK_MIXERINPUTMAP_H_
  22. #include <AK/SoundEngine/Common/AkTypes.h>
  23. #include <AK/SoundEngine/Common/IAkPluginMemAlloc.h>
  24. #include <AK/Tools/Common/AkArray.h>
  25. #include <AK/SoundEngine/Common/IAkPlugin.h>
  26. /// Collection class to manage inputs in mixer plugins.
  27. /// The inputs are identified by their context. The type of data attached to it is the template argument USER_DATA.
  28. /// The collection performs allocation/deallocation of user data via AK_PLUGIN_NEW/DELETE().
  29. /// Usage
  30. ///
  31. /// // Init
  32. /// AkMixerInputMap<void*, MyStruct> m_mapInputs;
  33. /// m_mapInputs.Init( in_pAllocator ); // in_pAllocator passed at plugin init.
  34. ///
  35. /// // Add an input.
  36. /// m_mapInputs.AddInput( in_pInput ); // void * in_pInput
  37. ///
  38. /// // Find an input
  39. /// MyStruct * pInput = m_mapInputs.Exists( in_pInputContext ); // void * in_pInputContext passed to ConsumeInput()
  40. ///
  41. /// // Iterate through inputs.
  42. /// AkMixerInputMap<MyStruct>::Iterator it = m_mapInputs.End();
  43. /// while ( it != m_mapInputs.End() )
  44. /// {
  45. /// MyStruct * pInput = (*it).pUserData;
  46. /// ...
  47. /// ++it;
  48. /// }
  49. /// Structure of an entry in the AkMixerInputMap map.
  50. template <class KEY, class USER_DATA>
  51. struct AkInputMapSlot
  52. {
  53. KEY key; /// Key.
  54. USER_DATA * pUserData; /// User data. Here we have a buffer. Other relevant info would be the game object position and input parameters of the previous frame.
  55. AkInputMapSlot() : key( NULL ), pUserData( NULL ) {}
  56. bool operator ==(const AkInputMapSlot& in_Op) const { return (key == in_Op.key); }
  57. };
  58. /// AkMixerInputMap: Map of inputs (identified with AK::IAkMixerInputContext *) to user-defined blocks of data.
  59. template <class KEY, class USER_DATA>
  60. class AkMixerInputMap : public AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>
  61. {
  62. public:
  63. typedef AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator> BaseClass;
  64. /// Returns the user data associated with given input context. Returns NULL if none found.
  65. USER_DATA * Exists( KEY in_key )
  66. {
  67. typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator it = FindEx(in_key);
  68. return ( it != BaseClass::End() ) ? (*it).pUserData : NULL;
  69. }
  70. /// Adds an input with new user data.
  71. USER_DATA * AddInput(KEY in_key)
  72. {
  73. typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator it = FindEx(in_key);
  74. if ( it != BaseClass::End() )
  75. return (*it).pUserData;
  76. else
  77. {
  78. AkInputMapSlot<KEY, USER_DATA> * pSlot = AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::AddLast();
  79. if ( pSlot )
  80. {
  81. pSlot->pUserData = AK_PLUGIN_NEW( AkPluginArrayAllocator::GetAllocator(), USER_DATA );
  82. if ( pSlot->pUserData )
  83. {
  84. pSlot->key = in_key;
  85. return pSlot->pUserData;
  86. }
  87. BaseClass::RemoveLast();
  88. }
  89. }
  90. return NULL;
  91. }
  92. /// Removes an input and destroys its associated user data.
  93. bool RemoveInput(KEY in_key)
  94. {
  95. typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator it = FindEx( in_key );
  96. if ( it != BaseClass::End() )
  97. {
  98. AKASSERT( (*it).pUserData );
  99. AK_PLUGIN_DELETE( AkPluginArrayAllocator::GetAllocator(), (*it).pUserData );
  100. BaseClass::EraseSwap( it );
  101. return true;
  102. }
  103. return false;
  104. }
  105. /// Erase the specified iterator in the array. but it does not guarantee the ordering in the array.
  106. typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator EraseSwap(typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator& in_rIter)
  107. {
  108. if ((*in_rIter).pUserData)
  109. {
  110. AK_PLUGIN_DELETE(AkPluginArrayAllocator::GetAllocator(), (*in_rIter).pUserData);
  111. }
  112. return BaseClass::EraseSwap(in_rIter);
  113. }
  114. /// Terminate array.
  115. void Term()
  116. {
  117. if ( BaseClass::m_pItems )
  118. {
  119. RemoveAll();
  120. AkPluginArrayAllocator::Free( BaseClass::m_pItems );
  121. BaseClass::m_pItems = 0;
  122. BaseClass::m_ulReserved = 0;
  123. }
  124. }
  125. /// Finds an item in the array.
  126. typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator FindEx( KEY in_key ) const
  127. {
  128. AkInputMapSlot<KEY, USER_DATA> mapSlot;
  129. mapSlot.key = in_key;
  130. return BaseClass::FindEx( mapSlot );
  131. }
  132. /// Removes and destroys all items in the array.
  133. void RemoveAll()
  134. {
  135. for ( typename AkArray<AkInputMapSlot<KEY, USER_DATA>, const AkInputMapSlot<KEY, USER_DATA>&, AkPluginArrayAllocator>::Iterator it = BaseClass::Begin(), itEnd = BaseClass::End(); it != itEnd; ++it )
  136. {
  137. AKASSERT( (*it).pUserData );
  138. AK_PLUGIN_DELETE( AkPluginArrayAllocator::GetAllocator(), (*it).pUserData );
  139. (*it).~AkInputMapSlot();
  140. }
  141. BaseClass::m_uLength = 0;
  142. }
  143. };
  144. #endif // _AK_MIXERINPUTMAP_H_