Table of Contents

include/AK/Tools/Common/AkBankReadHelpers.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 #ifndef _AK_BANKREADHELPERS_H_
00029 #define _AK_BANKREADHELPERS_H_
00030 
00031 namespace AK
00032 {
00033     template< typename T >
00034     inline T ReadUnaligned(const AkUInt8* in_pVal)
00035     {
00036 #if defined(__GNUC__)
00037         typedef T __attribute__((aligned(1))) UnalignedT;
00038         return *reinterpret_cast<const UnalignedT *>(in_pVal);
00039 #elif defined(_MSC_VER)  && !defined(AK_CPU_X86)
00040         return *reinterpret_cast<const T __unaligned *>(in_pVal);  // __unaligned not supported on 32-bit x86
00041 #else
00042         return *reinterpret_cast<const T *>(in_pVal);
00043 #endif
00044     }
00045 
00046     template< typename T >
00047     inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
00048     {
00049 #if defined(__GNUC__)
00050         typedef T __attribute__((aligned(1))) UnalignedT;
00051         *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
00052 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
00053         *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
00054 #else
00055         *reinterpret_cast<T *>(out_pVal) = in_val;
00056 #endif
00057     }
00058 
00059     /// Read data from bank and advance pointer.
00060     template< typename T >
00061     inline T ReadBankData(
00062         AkUInt8*& in_rptr
00063 #ifdef _DEBUG
00064         , AkUInt32& in_rSize
00065 #endif
00066     )
00067     {
00068         T l_Value = ReadUnaligned<T>(in_rptr);
00069 
00070         in_rptr += sizeof(T);
00071 #ifdef _DEBUG
00072         in_rSize -= sizeof(T);
00073 #endif
00074         return l_Value;
00075     }
00076 
00077     template< typename T >
00078     inline T ReadVariableSizeBankData(
00079         AkUInt8*& in_rptr
00080 #ifdef _DEBUG
00081         , AkUInt32& in_rSize
00082 #endif
00083     )
00084     {
00085         AkUInt32 l_Value = 0;
00086 
00087         AkUInt8 currentByte = *in_rptr;
00088         ++in_rptr;
00089 #ifdef _DEBUG
00090         --in_rSize;
00091 #endif
00092         l_Value = (currentByte & 0x7F);
00093         while (0x80 & currentByte)
00094         {
00095             currentByte = *in_rptr;
00096             ++in_rptr;
00097 #ifdef _DEBUG
00098             --in_rSize;
00099 #endif
00100             l_Value = l_Value << 7;
00101             l_Value |= (currentByte & 0x7F);
00102         }
00103 
00104         return (T)l_Value;
00105     }
00106 
00107     inline char * ReadBankStringUtf8(
00108         AkUInt8*& in_rptr
00109 #ifdef _DEBUG
00110         , AkUInt32& in_rSize
00111 #endif
00112         , AkUInt32& out_uStringSize)
00113     {
00114         out_uStringSize = ReadBankData<AkUInt32>(in_rptr
00115 #ifdef _DEBUG
00116             , in_rSize
00117 #endif
00118             );
00119 
00120         char * pString = 0;
00121         if (out_uStringSize > 0)
00122         {
00123             pString = reinterpret_cast<char*>(in_rptr);
00124             in_rptr += out_uStringSize;
00125 #ifdef _DEBUG
00126             in_rSize -= out_uStringSize;
00127 #endif
00128         }
00129         return pString;
00130     }
00131 }
00132 
00133 
00134 #ifdef _DEBUG
00135 
00136 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
00137 #define READBANKDATA( _Type, _Ptr, _Size )      \
00138         AK::ReadBankData<_Type>( _Ptr, _Size )
00139 
00140 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size )      \
00141         AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
00142 
00143 /// Read and return non-null-terminatd UTF-8 string stored in bank, and its size.
00144 #define READBANKSTRING_UTF8( _Ptr, _Size, _out_StringSize )     \
00145         AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
00146 
00147 /// Read and return non-null-terminatd string stored in bank, and its size.
00148 #define READBANKSTRING( _Ptr, _Size, _out_StringSize )      \
00149         AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize ) //same as UTF-8 for now.
00150 
00151 /// Skip over some bank data  of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
00152 #define SKIPBANKDATA( _Type, _Ptr, _Size )      \
00153         ( _Ptr ) += sizeof( _Type );    \
00154         ( _Size ) -= sizeof( _Type )
00155 
00156 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
00157 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
00158         ( _Ptr ) += _NumBytes;      \
00159         ( _Size ) -= _NumBytes
00160 
00161 #else
00162 
00163 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
00164 #define READBANKDATA( _Type, _Ptr, _Size )      \
00165         AK::ReadBankData<_Type>( _Ptr )
00166 
00167 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size )      \
00168         AK::ReadVariableSizeBankData<_Type>( _Ptr )
00169 
00170 #define READBANKSTRING_UTF8( _Ptr, _Size, _out_StringSize )     \
00171         AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
00172 
00173 #define READBANKSTRING( _Ptr, _Size, _out_StringSize )      \
00174         AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
00175 
00176 /// Skip over some bank data  of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
00177 #define SKIPBANKDATA( _Type, _Ptr, _Size )      \
00178         ( _Ptr ) += sizeof( _Type )
00179 
00180 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
00181 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
00182         ( _Ptr ) += _NumBytes;
00183 
00184 #endif
00185 
00186 #define GETBANKDATABIT( _Data, _Shift ) \
00187     (((_Data) >> (_Shift)) & 0x1)
00188 
00189 /// Helper macro to determine whether the full content of a block of memory was properly parsed
00190 #ifdef _DEBUG
00191     #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
00192 #else
00193     #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
00194 #endif
00195 
00196 #endif //_AK_BANKREADHELPERS_H_