Table of Contents

include/AK/SoundEngine/Common/AkBytesMem.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 // AkBytesMem.h
00029 
00030 /// \file 
00031 /// IReadBytes / IWriteBytes implementation on a growing memory buffer. This
00032 /// version uses the AK::MemoryMgr memory pools.
00033 
00034 #pragma once
00035 
00036 #include <AK/IBytes.h>
00037 #include <AK/SoundEngine/Common/AkSoundEngineExport.h>
00038 #include <AK/Tools/Common/AkBankReadHelpers.h>
00039 
00040 namespace AK
00041 {
00042     class ReadBytesMem
00043         : public AK::IReadBytes
00044     {
00045     public:
00046 
00047         AKSOUNDENGINE_API ReadBytesMem();
00048         AKSOUNDENGINE_API ReadBytesMem(
00049             const void * in_pBytes, 
00050             AkInt32 in_cBytes
00051             );
00052         AKSOUNDENGINE_API virtual ~ReadBytesMem();
00053 
00054         // IReadBytes implementation
00055 
00056         AKSOUNDENGINE_API virtual bool ReadBytes(
00057             void * in_pData, 
00058             AkInt32 in_cBytes, 
00059             AkInt32 & out_cRead
00060             );
00061 
00062         // Public methods
00063 
00064         AKSOUNDENGINE_API void Attach(
00065             const void * in_pBytes, 
00066             AkInt32 in_cBytes
00067             );
00068 
00069     private:
00070         AkInt32   m_cBytes;
00071         const AkUInt8 * m_pBytes;
00072 
00073         AkInt32   m_cPos;
00074     };
00075 
00076     class WriteBytesMem
00077         : public AK::IWriteBuffer
00078     {
00079     public:
00080     
00081         AKSOUNDENGINE_API WriteBytesMem();
00082         AKSOUNDENGINE_API virtual ~WriteBytesMem();
00083     
00084         // IWriteBytes implementation
00085 
00086         AKSOUNDENGINE_API virtual bool WriteBytes(
00087             const void * in_pData, 
00088             AkInt32 in_cBytes, 
00089             AkInt32& out_cWritten);
00090     
00091         // IWriteBuffer implementation
00092 
00093         AKSOUNDENGINE_API virtual bool Reserve(
00094             AkInt32 in_cBytes
00095             );
00096 
00097         AKSOUNDENGINE_API virtual AkInt32 Count() const;
00098 
00099         AKSOUNDENGINE_API virtual void SetCount(
00100             AkInt32 in_cBytes
00101             );
00102 
00103         AKSOUNDENGINE_API virtual AkUInt8 * Bytes() const;
00104 
00105         AKSOUNDENGINE_API virtual AkUInt8 * Detach();
00106     
00107         AKSOUNDENGINE_API virtual void Clear();
00108     
00109         // Public methods
00110 
00111         AKSOUNDENGINE_API void SetMemPool( AkMemPoolId in_pool );
00112 
00113         template<class T>
00114         bool Write(const T & in_data)
00115         {
00116             AkInt32 cPos = m_cPos;
00117             AkInt32 cNewPos = cPos + sizeof(T);
00118 
00119             if ((m_cBytes >= cNewPos) || Grow(cNewPos))
00120             {
00121                 AK::WriteUnaligned<T>(m_pBytes + cPos, in_data);
00122                 m_cPos = cNewPos;
00123                 return true;
00124             }
00125             else
00126                 return false;
00127         }
00128     
00129     private:
00130         bool Grow(
00131             AkInt32 in_cBytes
00132         );
00133 
00134         AkInt32     m_cBytes;
00135         AkUInt8 *   m_pBytes;
00136     
00137         AkInt32 m_cPos;
00138     
00139         AkMemPoolId m_pool;
00140     };
00141 }