Table of Contents

include/AK/Tools/Common/AkAllocator.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 // AkAllocator.h
00029 
00030 #ifndef _AK_TOOLS_COMMON_AKALLOCATOR_H
00031 #define _AK_TOOLS_COMMON_AKALLOCATOR_H
00032 
00033 #include <AK/SoundEngine/Common/IAkPluginMemAlloc.h>
00034 
00035 namespace AK
00036 {
00037     // Audiokinetic Wwise namespace
00038     namespace Wwise
00039     {
00040         class Mallocator
00041             : public AK::IAkPluginMemAlloc
00042         {
00043         public:
00044             virtual void* Malloc(size_t in_uSize)
00045             {
00046                 return malloc(in_uSize);
00047             }
00048 
00049             virtual void Free(void* in_pMemAddress)
00050             {
00051                 free(in_pMemAddress);
00052             }
00053         };
00054 
00055         template<typename T>
00056         class SafeAllocator
00057         {
00058         public:
00059             SafeAllocator(AK::IAkPluginMemAlloc* in_pAlloc)
00060                 : m_pAlloc(in_pAlloc),
00061                   m_pPtr(nullptr)
00062             {
00063             }
00064 
00065             ~SafeAllocator()
00066             {
00067                 if (m_pPtr)
00068                 {
00069                     m_pAlloc->Free(m_pPtr);
00070                 }
00071             }
00072 
00073             T* operator->() { return m_pPtr; }
00074             T& operator*() { return *m_pPtr; }
00075 
00076             explicit operator bool() const { return m_pPtr != nullptr; }
00077             operator T*&() { return m_pPtr; }
00078 
00079         private:
00080             AK::IAkPluginMemAlloc* m_pAlloc;
00081             T* m_pPtr;
00082         };
00083     }
00084 }
00085 
00086 #endif // _AK_TOOLS_COMMON_AKALLOCATOR_H