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