Wwise Version
Wwise SDK 2021.1.3
AkBankReadHelpers.h
Go to the documentation of this file.
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 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Version: v2021.1.3 Build: 7665
25  Copyright (c) 2006-2021 Audiokinetic Inc.
26 *******************************************************************************/
27 
28 #ifndef _AK_BANKREADHELPERS_H_
29 #define _AK_BANKREADHELPERS_H_
30 
31 namespace AK
32 {
33  template< typename T >
34  inline T ReadUnaligned(const AkUInt8* in_pVal)
35  {
36 #if defined(__GNUC__)
37  typedef T __attribute__((aligned(1))) UnalignedT;
38  return *reinterpret_cast<const UnalignedT *>(in_pVal);
39 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
40  return *reinterpret_cast<const T __unaligned *>(in_pVal); // __unaligned not supported on 32-bit x86
41 #else
42  return *reinterpret_cast<const T *>(in_pVal);
43 #endif
44  }
45 
46  template< typename T >
47  inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
48  {
49 #if defined(__GNUC__)
50  typedef T __attribute__((aligned(1))) UnalignedT;
51  *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
52 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
53  *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
54 #else
55  *reinterpret_cast<T *>(out_pVal) = in_val;
56 #endif
57  }
58 
59  /// Read data from bank and advance pointer.
60  template< typename T >
61  inline T ReadBankData(
62  AkUInt8*& in_rptr
63 #ifdef _DEBUG
64  , AkUInt32& in_rSize
65 #endif
66  )
67  {
68  T l_Value = ReadUnaligned<T>(in_rptr);
69 
70  in_rptr += sizeof(T);
71 #ifdef _DEBUG
72  in_rSize -= sizeof(T);
73 #endif
74  return l_Value;
75  }
76 
77  template< typename T >
79  AkUInt8*& in_rptr
80 #ifdef _DEBUG
81  , AkUInt32& in_rSize
82 #endif
83  )
84  {
85  AkUInt32 l_Value = 0;
86 
87  AkUInt8 currentByte = *in_rptr;
88  ++in_rptr;
89 #ifdef _DEBUG
90  --in_rSize;
91 #endif
92  l_Value = (currentByte & 0x7F);
93  while (0x80 & currentByte)
94  {
95  currentByte = *in_rptr;
96  ++in_rptr;
97 #ifdef _DEBUG
98  --in_rSize;
99 #endif
100  l_Value = l_Value << 7;
101  l_Value |= (currentByte & 0x7F);
102  }
103 
104  return (T)l_Value;
105  }
106 
107  inline char * ReadBankStringUtf8(
108  AkUInt8*& in_rptr
109 #ifdef _DEBUG
110  , AkUInt32& in_rSize
111 #endif
112  , AkUInt32& out_uStringSize)
113  {
114  char* pString = (char *)in_rptr;
115  out_uStringSize = 0;
116 
117  while (*in_rptr != 0)
118  {
119  ++in_rptr;
120 #ifdef _DEBUG
121  --in_rSize;
122 #endif
123  ++out_uStringSize;
124  }
125 
126  ++in_rptr;
127 #ifdef _DEBUG
128  --in_rSize;
129 #endif
130 
131  return pString;
132  }
133 }
134 
135 
136 #ifdef _DEBUG
137 
138 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
139 #define READBANKDATA( _Type, _Ptr, _Size ) \
140  AK::ReadBankData<_Type>( _Ptr, _Size )
141 
142 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
143  AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
144 
145 /// Read and return null-terminatd UTF-8 string stored in bank, and its size.
146 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
147  AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
148 
149 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
150 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
151  ( _Ptr ) += sizeof( _Type ); \
152  ( _Size ) -= sizeof( _Type )
153 
154 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
155 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
156  ( _Size ) -= _NumBytes; \
157  ( _Ptr ) += _NumBytes
158 
159 /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
160 #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
161  AKPLATFORM::SafeStrCpy( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
162  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
163 
164 /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
165 #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
166  AK_UTF8_TO_OSCHAR( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
167  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
168 
169 /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
170 #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxOutPtrSize ) \
171  AK_CHAR_TO_UTF16( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
172  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
173 
174 
175 #else
176 
177 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
178 #define READBANKDATA( _Type, _Ptr, _Size ) \
179  AK::ReadBankData<_Type>( _Ptr )
180 
181 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
182  AK::ReadVariableSizeBankData<_Type>( _Ptr )
183 
184 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
185  AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
186 
187 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
188 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
189  ( _Ptr ) += sizeof( _Type )
190 
191 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
192 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
193  ( _Ptr ) += _NumBytes;
194 
195 /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
196 #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
197  AKPLATFORM::SafeStrCpy( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
198  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
199 
200 /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
201 #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
202  AK_UTF8_TO_OSCHAR( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
203  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
204 
205 /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
206 #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxPtrSize ) \
207  AK_CHAR_TO_UTF16( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
208  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
209 
210 #endif
211 
212 #define GETBANKDATABIT( _Data, _Shift ) \
213  (((_Data) >> (_Shift)) & 0x1)
214 
215 /// Helper macro to determine whether the full content of a block of memory was properly parsed
216 #ifdef _DEBUG
217  #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
218 #else
219  #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
220 #endif
221 
222 #endif //_AK_BANKREADHELPERS_H_
Audiokinetic namespace.
void WriteUnaligned(AkUInt8 *out_pVal, const T in_val)
uint8_t AkUInt8
Unsigned 8-bit integer.
Definition: AkTypes.h:55
T ReadBankData(AkUInt8 *&in_rptr)
Read data from bank and advance pointer.
T ReadVariableSizeBankData(AkUInt8 *&in_rptr)
T ReadUnaligned(const AkUInt8 *in_pVal)
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:57
char * ReadBankStringUtf8(AkUInt8 *&in_rptr, AkUInt32 &out_uStringSize)