Crazy Eddie's GUI System  0.8.4
MemorySTLWrapper.h
00001 /***********************************************************************
00002         created:        28/10/2010
00003         author:         Martin Preisler (inspired by Ogre3D)
00004 
00005         purpose:        Wraps CEGUI Allocators to std::allocator class
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 _CEGUIMemorySTLWrapper_h_
00030 #define _CEGUIMemorySTLWrapper_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 
00041 template<typename T>
00042 struct STLAllocatorWrapperBase
00043 {
00044         typedef T value_type;
00045 };
00046 // getting rid of const trick taken from Ogre :-)
00047 template<typename T>
00048 struct STLAllocatorWrapperBase<const T>
00049 {
00050         typedef T value_type;
00051 };
00052 
00053 template <typename T, typename Allocator>
00054 class STLAllocatorWrapper : public STLAllocatorWrapperBase<T>
00055 {
00056 public:
00057     typedef STLAllocatorWrapperBase<T> Base;
00058     typedef typename Base::value_type value_type;
00059 
00060         typedef value_type* pointer;
00061         typedef const value_type* const_pointer;
00062         typedef value_type& reference;
00063         typedef const value_type& const_reference;
00064 
00065         typedef std::size_t size_type;
00066         typedef std::ptrdiff_t difference_type;
00067 
00068         template<typename U>
00069         struct rebind
00070         {
00071                 typedef STLAllocatorWrapper<U, Allocator> other;
00072         };
00073 
00074         inline explicit STLAllocatorWrapper()
00075         {}
00076 
00077         inline STLAllocatorWrapper(const STLAllocatorWrapper&)
00078         {}
00079 
00080         template <typename U, typename P>
00081         inline STLAllocatorWrapper(const STLAllocatorWrapper<U, P>&)
00082         {}
00083 
00084     inline pointer address(reference x) const
00085         {
00086                 return &x;
00087         }
00088 
00089         inline const_pointer address(const_reference x) const
00090         {
00091                 return &x;
00092         }
00093 
00094         inline size_type max_size() const throw()
00095         {
00096                 return Allocator::getMaxAllocationSize();
00097         }
00098 
00099         inline pointer allocate(size_type count, typename std::allocator<void>::const_pointer ptr = 0)
00100         {
00101         (void)ptr;
00102                 return static_cast<pointer>(Allocator::allocateBytes(count * sizeof(T)));
00103         }
00104 
00105         inline void deallocate(pointer ptr, size_type /*size*/ )
00106         {
00107                 Allocator::deallocateBytes(ptr);
00108         }
00109 
00110         inline void construct(pointer p, const T& val)
00111         {
00112                 new(static_cast<void*>(p)) T(val);
00113         }
00114 
00115         inline void destroy(pointer p)
00116         {
00117                 p->~T();
00118         }
00119 };
00120 
00121 template<typename T, typename T2, typename P>
00122 inline bool operator==(const STLAllocatorWrapper<T, P>&, const STLAllocatorWrapper<T2, P>&)
00123 {
00124         // same allocator, return true
00125         return true;
00126 }
00127 
00128 template<typename T, typename P, typename OtherAllocator>
00129 inline bool operator==(const STLAllocatorWrapper<T, P>&, const OtherAllocator&)
00130 {
00131     // if the template abose doesn't get matched, return false (allocators differ)
00132         return false;
00133 }
00134 
00135 template<typename T, typename T2, typename P>
00136 inline bool operator!=(const STLAllocatorWrapper<T, P>&, const STLAllocatorWrapper<T2,P>&)
00137 {
00138         // same allocator, return false (they are not different)
00139         return false;
00140 }
00141 
00142 template<typename T, typename P, typename OtherAllocator>
00143 inline bool operator!=(const STLAllocatorWrapper<T, P>&, const OtherAllocator&)
00144 {
00145     // the above didn't get matched, that means the allocators differ...
00146         return true;
00147 }
00148 
00149 // STL allocator helper macros
00150 #define CEGUI_VECTOR_ALLOC(T) , ::CEGUI::STLAllocatorWrapper<T, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
00151 #define CEGUI_SET_ALLOC(T) , ::CEGUI::STLAllocatorWrapper<T, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
00152 #define CEGUI_MAP_ALLOC(K, V) , ::CEGUI::STLAllocatorWrapper<std::pair<K, V>, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
00153 #define CEGUI_MULTIMAP_ALLOC(K, V) , ::CEGUI::STLAllocatorWrapper<std::pair<K, V>, ::CEGUI::AllocatorConfig< ::CEGUI::STLAllocator >::Allocator>
00154 
00155 #else
00156 
00157 // STL allocator helper macros
00158 #define CEGUI_VECTOR_ALLOC(T)
00159 #define CEGUI_SET_ALLOC(T)
00160 #define CEGUI_MAP_ALLOC(K, V)
00161 #define CEGUI_MULTIMAP_ALLOC(K, V)
00162 
00163 #endif
00164 
00165 }
00166 
00167 #endif  // end of guard _CEGUIMemorySTLWrapper_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends