Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
MemoryAllocatedObject.h
1 /***********************************************************************
2  filename: CEGUIMemoryAllocatedObject.h
3  created: 28/10/2010
4  author: Martin Preisler (inspired by Ogre3D)
5 
6  purpose: Overrides new an delete operators and uses given Allocator
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIMemoryAllocatedObject_h_
31 #define _CEGUIMemoryAllocatedObject_h_
32 
33 #ifndef _CEGUIMemoryAllocation_h_
34 # error Dont include this directly! Include CEGUIBase.h instead.
35 #endif
36 
37 namespace CEGUI
38 {
39 
40 #ifdef CEGUI_CUSTOM_ALLOCATORS
41 
50 template <typename Class>
51 class AllocatedObject
52 {
53 public:
54  typedef typename AllocatorConfig<Class>::Allocator Allocator;
55 
56  inline explicit AllocatedObject()
57  {}
58 
59 #ifndef CEGUI_CUSTOM_ALLOCATORS_DEBUG
60  inline void* operator new(size_t size)
61  {
62  return Allocator::allocateBytes(size);
63  }
64 #else
65  inline void* operator new(size_t size, const char* file, int line, const char* func)
66  {
67  return Allocator::allocateBytes(size, file, line, func);
68  }
69 #endif
70 
71  inline void operator delete(void* ptr)
72  {
73  Allocator::deallocateBytes(ptr);
74  }
75 
76 #ifndef CEGUI_CUSTOM_ALLOCATORS_DEBUG
77  inline void* operator new[] (size_t size)
78  {
79  return Allocator::allocateBytes(size);
80  }
81 #else
82  inline void* operator new[] (size_t size, const char* file, int line, const char* func)
83  {
84  return Allocator::allocateBytes(size, file, line, func);
85  }
86 #endif
87 
88  inline void operator delete[] (void* ptr)
89  {
90  Allocator::deallocateBytes(ptr);
91  }
92 
93  // todo: does debug variant even make sense with placement new?
94  inline void* operator new(size_t size, void* ptr)
95  {
96  (void) size;
97  return ptr;
98  }
99 
100  inline void operator delete(void* ptr, void*)
101  {
102  Allocator::deallocateBytes(ptr);
103  }
104 };
105 
106 #else
107 
108 // allocated object is just a stub template class if custom memory allocators aren't used
109 template<typename Allocator>
111 {
112 public:
113  inline explicit AllocatedObject()
114  {}
115 };
116 
117 #endif
118 
119 }
120 
121 #endif // end of guard _CEGUIMemoryAllocatedObject_h_