Version
Wwise SDK 2019.2.15
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: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> 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  #if defined(__has_warning)
51  #if __has_warning("-Walign-mismatch")
52  #define __IGNORE_RECENT_CLANG_WARNINGS
53  #pragma clang diagnostic push
54  #pragma clang diagnostic ignored "-Walign-mismatch"
55  #endif
56  #endif
57 
58  typedef T __attribute__((aligned(1))) UnalignedT;
59  *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
60 
61  #ifdef __IGNORE_RECENT_CLANG_WARNINGS
62  #undef __IGNORE_RECENT_CLANG_WARNINGS
63  #pragma clang diagnostic pop
64  #endif
65 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
66  *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
67 #else
68  *reinterpret_cast<T *>(out_pVal) = in_val;
69 #endif
70  }
71 
72  /// Read data from bank and advance pointer.
73  template< typename T >
74  inline T ReadBankData(
75  AkUInt8*& in_rptr
76 #ifdef _DEBUG
77  , AkUInt32& in_rSize
78 #endif
79  )
80  {
81  T l_Value = ReadUnaligned<T>(in_rptr);
82 
83  in_rptr += sizeof(T);
84 #ifdef _DEBUG
85  in_rSize -= sizeof(T);
86 #endif
87  return l_Value;
88  }
89 
90  template< typename T >
92  AkUInt8*& in_rptr
93 #ifdef _DEBUG
94  , AkUInt32& in_rSize
95 #endif
96  )
97  {
98  AkUInt32 l_Value = 0;
99 
100  AkUInt8 currentByte = *in_rptr;
101  ++in_rptr;
102 #ifdef _DEBUG
103  --in_rSize;
104 #endif
105  l_Value = (currentByte & 0x7F);
106  while (0x80 & currentByte)
107  {
108  currentByte = *in_rptr;
109  ++in_rptr;
110 #ifdef _DEBUG
111  --in_rSize;
112 #endif
113  l_Value = l_Value << 7;
114  l_Value |= (currentByte & 0x7F);
115  }
116 
117  return (T)l_Value;
118  }
119 
120  inline char * ReadBankStringUtf8(
121  AkUInt8*& in_rptr
122 #ifdef _DEBUG
123  , AkUInt32& in_rSize
124 #endif
125  , AkUInt32& out_uStringSize)
126  {
127  out_uStringSize = ReadBankData<AkUInt32>(in_rptr
128 #ifdef _DEBUG
129  , in_rSize
130 #endif
131  );
132 
133  char * pString = 0;
134  if (out_uStringSize > 0)
135  {
136  pString = reinterpret_cast<char*>(in_rptr);
137  in_rptr += out_uStringSize;
138 #ifdef _DEBUG
139  in_rSize -= out_uStringSize;
140 #endif
141  }
142  return pString;
143  }
144 }
145 
146 
147 #ifdef _DEBUG
148 
149 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
150 #define READBANKDATA( _Type, _Ptr, _Size ) \
151  AK::ReadBankData<_Type>( _Ptr, _Size )
152 
153 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
154  AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
155 
156 /// Read and return non-null-terminatd UTF-8 string stored in bank, and its size.
157 #define READBANKSTRING_UTF8( _Ptr, _Size, _out_StringSize ) \
158  AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
159 
160 /// Read and return non-null-terminatd string stored in bank, and its size.
161 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
162  AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize ) //same as UTF-8 for now.
163 
164 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
165 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
166  ( _Ptr ) += sizeof( _Type ); \
167  ( _Size ) -= sizeof( _Type )
168 
169 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
170 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
171  ( _Ptr ) += _NumBytes; \
172  ( _Size ) -= _NumBytes
173 
174 #else
175 
176 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
177 #define READBANKDATA( _Type, _Ptr, _Size ) \
178  AK::ReadBankData<_Type>( _Ptr )
179 
180 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
181  AK::ReadVariableSizeBankData<_Type>( _Ptr )
182 
183 #define READBANKSTRING_UTF8( _Ptr, _Size, _out_StringSize ) \
184  AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
185 
186 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
187  AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
188 
189 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
190 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
191  ( _Ptr ) += sizeof( _Type )
192 
193 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
194 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
195  ( _Ptr ) += _NumBytes;
196 
197 #endif
198 
199 #define GETBANKDATABIT( _Data, _Shift ) \
200  (((_Data) >> (_Shift)) & 0x1)
201 
202 /// Helper macro to determine whether the full content of a block of memory was properly parsed
203 #ifdef _DEBUG
204  #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
205 #else
206  #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
207 #endif
208 
209 #endif //_AK_BANKREADHELPERS_H_
Audiokinetic namespace.
void WriteUnaligned(AkUInt8 *out_pVal, const T in_val)
T ReadBankData(AkUInt8 *&in_rptr)
Read data from bank and advance pointer.
T ReadVariableSizeBankData(AkUInt8 *&in_rptr)
uint8_t AkUInt8
Unsigned 8-bit integer.
Definition: AkTypes.h:83
T ReadUnaligned(const AkUInt8 *in_pVal)
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:85
char * ReadBankStringUtf8(AkUInt8 *&in_rptr, AkUInt32 &out_uStringSize)