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