Crazy Eddie's GUI System  0.8.4
MemoryAllocatedObject.h
00001 /***********************************************************************
00002         created:        28/10/2010
00003         author:         Martin Preisler (inspired by Ogre3D)
00004 
00005         purpose:        Overrides new an delete operators and uses given Allocator
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUIMemoryAllocatedObject_h_
00030 #define _CEGUIMemoryAllocatedObject_h_
00031 
00032 #ifndef _CEGUIMemoryAllocation_h_
00033 #   error Dont include this directly! Include CEGUIBase.h instead.
00034 #endif
00035 
00036 namespace CEGUI
00037 {
00038  
00039 #ifdef CEGUI_CUSTOM_ALLOCATORS
00040 
00049 template <typename Class>
00050 class AllocatedObject
00051 {
00052 public:
00053     typedef typename AllocatorConfig<Class>::Allocator Allocator;
00054 
00055         inline explicit AllocatedObject()
00056         {}
00057 
00058 #ifndef CEGUI_CUSTOM_ALLOCATORS_DEBUG
00059         inline void* operator new(size_t size)
00060         {
00061                 return Allocator::allocateBytes(size);
00062         }
00063 #else
00064         inline void* operator new(size_t size, const char* file, int line, const char* func)
00065         {
00066                 return Allocator::allocateBytes(size, file, line, func);
00067         }
00068 #endif
00069 
00070         inline void operator delete(void* ptr)
00071         {
00072                 Allocator::deallocateBytes(ptr);
00073         }
00074 
00075 #ifndef CEGUI_CUSTOM_ALLOCATORS_DEBUG
00076         inline void* operator new[] (size_t size)
00077         {
00078                 return Allocator::allocateBytes(size);
00079         }
00080 #else
00081         inline void* operator new[] (size_t size, const char* file, int line, const char* func)
00082         {
00083                 return Allocator::allocateBytes(size, file, line, func);
00084         }
00085 #endif
00086 
00087         inline void operator delete[] (void* ptr)
00088         {
00089                 Allocator::deallocateBytes(ptr);
00090         }
00091 
00092     // todo: does debug variant even make sense with placement new?
00093         inline void* operator new(size_t size, void* ptr)
00094         {
00095                 (void) size;
00096                 return ptr;
00097         }
00098 
00099     inline void operator delete(void* ptr, void*)
00100         {
00101                 Allocator::deallocateBytes(ptr);
00102         }
00103 };
00104 
00105 #else
00106 
00107 // allocated object is just a stub template class if custom memory allocators aren't used
00108 template<typename Allocator>
00109 class AllocatedObject
00110 {
00111 public:
00112     inline explicit AllocatedObject()
00113         {}
00114 };
00115 
00116 #endif
00117 
00118 }
00119 
00120 #endif  // end of guard _CEGUIMemoryAllocatedObject_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends