Crazy Eddie's GUI System  0.8.3
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
MemorySTLWrapper.h
1 /***********************************************************************
2  filename: CEGUIMemorySTLWrapper.h
3  created: 28/10/2010
4  author: Martin Preisler (inspired by Ogre3D)
5 
6  purpose: Wraps CEGUI Allocators to std::allocator class
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 _CEGUIMemorySTLWrapper_h_
31 #define _CEGUIMemorySTLWrapper_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 
42 template<typename T>
43 struct STLAllocatorWrapperBase
44 {
45  typedef T value_type;
46 };
47 // getting rid of const trick taken from Ogre :-)
48 template<typename T>
49 struct STLAllocatorWrapperBase<const T>
50 {
51  typedef T value_type;
52 };
53 
54 template <typename T, typename Allocator>
55 class STLAllocatorWrapper : public STLAllocatorWrapperBase<T>
56 {
57 public:
58  typedef STLAllocatorWrapperBase<T> Base;
59  typedef typename Base::value_type value_type;
60 
61  typedef value_type* pointer;
62  typedef const value_type* const_pointer;
63  typedef value_type& reference;
64  typedef const value_type& const_reference;
65 
66  typedef std::size_t size_type;
67  typedef std::ptrdiff_t difference_type;
68 
69  template<typename U>
70  struct rebind
71  {
72  typedef STLAllocatorWrapper<U, Allocator> other;
73  };
74 
75  inline explicit STLAllocatorWrapper()
76  {}
77 
78  inline STLAllocatorWrapper(const STLAllocatorWrapper&)
79  {}
80 
81  template <typename U, typename P>
82  inline STLAllocatorWrapper(const STLAllocatorWrapper<U, P>&)
83  {}
84 
85  inline pointer address(reference x) const
86  {
87  return &x;
88  }
89 
90  inline const_pointer address(const_reference x) const
91  {
92  return &x;
93  }
94 
95  inline size_type max_size() const throw()
96  {
97  return Allocator::getMaxAllocationSize();
98  }
99 
100  inline pointer allocate(size_type count, typename std::allocator<void>::const_pointer ptr = 0)
101  {
102  (void)ptr;
103  return static_cast<pointer>(Allocator::allocateBytes(count * sizeof(T)));
104  }
105 
106  inline void deallocate(pointer ptr, size_type /*size*/ )
107  {
108  Allocator::deallocateBytes(ptr);
109  }
110 
111  inline void construct(pointer p, const T& val)
112  {
113  new(static_cast<void*>(p)) T(val);
114  }
115 
116  inline void destroy(pointer p)
117  {
118  p->~T();
119  }
120 };
121 
122 template<typename T, typename T2, typename P>
123 inline bool operator==(const STLAllocatorWrapper<T, P>&, const STLAllocatorWrapper<T2, P>&)
124 {
125  // same allocator, return true
126  return true;
127 }
128 
129 template<typename T, typename P, typename OtherAllocator>
130 inline bool operator==(const STLAllocatorWrapper<T, P>&, const OtherAllocator&)
131 {
132  // if the template abose doesn't get matched, return false (allocators differ)
133  return false;
134 }
135 
136 template<typename T, typename T2, typename P>
137 inline bool operator!=(const STLAllocatorWrapper<T, P>&, const STLAllocatorWrapper<T2,P>&)
138 {
139  // same allocator, return false (they are not different)
140  return false;
141 }
142 
143 template<typename T, typename P, typename OtherAllocator>
144 inline bool operator!=(const STLAllocatorWrapper<T, P>&, const OtherAllocator&)
145 {
146  // the above didn't get matched, that means the allocators differ...
147  return true;
148 }
149 
150 // STL allocator helper macros
151 #define CEGUI_VECTOR_ALLOC(T) , ::CEGUI::STLAllocatorWrapper<T, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
152 #define CEGUI_SET_ALLOC(T) , ::CEGUI::STLAllocatorWrapper<T, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
153 #define CEGUI_MAP_ALLOC(K, V) , ::CEGUI::STLAllocatorWrapper<std::pair<K, V>, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
154 #define CEGUI_MULTIMAP_ALLOC(K, V) , ::CEGUI::STLAllocatorWrapper<std::pair<K, V>, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
155 
156 #else
157 
158 // STL allocator helper macros
159 #define CEGUI_VECTOR_ALLOC(T)
160 #define CEGUI_SET_ALLOC(T)
161 #define CEGUI_MAP_ALLOC(K, V)
162 #define CEGUI_MULTIMAP_ALLOC(K, V)
163 
164 #endif
165 
166 }
167 
168 #endif // end of guard _CEGUIMemorySTLWrapper_h_