00001 00007 /* 00008 This file is part of MyGUI. 00009 00010 MyGUI is free software: you can redistribute it and/or modify 00011 it under the terms of the GNU Lesser General Public License as published by 00012 the Free Software Foundation, either version 3 of the License, or 00013 (at your option) any later version. 00014 00015 MyGUI is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public License 00021 along with MyGUI. If not, see <http://www.gnu.org/licenses/>. 00022 */ 00023 #ifndef __MYGUI_ALLOCATOR_H__ 00024 #define __MYGUI_ALLOCATOR_H__ 00025 00026 #include <memory> 00027 #include <limits> 00028 00029 #ifdef max 00030 #undef max 00031 #undef min 00032 #endif 00033 00034 namespace MyGUI 00035 { 00036 00037 template<typename T> 00038 class Allocator 00039 { 00040 public : 00041 // typedefs 00042 typedef T value_type; 00043 typedef value_type* pointer; 00044 typedef const value_type* const_pointer; 00045 typedef value_type& reference; 00046 typedef const value_type& const_reference; 00047 typedef std::size_t size_type; 00048 typedef std::ptrdiff_t difference_type; 00049 00050 public : 00051 // convert an allocator<T> to allocator<U> 00052 template<typename U> 00053 struct rebind 00054 { 00055 typedef Allocator<U> other; 00056 }; 00057 00058 public : 00059 inline explicit Allocator() { } 00060 inline ~Allocator() { } 00061 template<typename U> 00062 inline explicit Allocator(Allocator<U> const&) { } 00063 00064 // address 00065 inline pointer address(reference r) { return &r; } 00066 inline const_pointer address(const_reference r) { return &r; } 00067 00068 // memory allocation 00069 inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) 00070 { 00071 return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T))); 00072 } 00073 inline void deallocate(pointer p, size_type) 00074 { 00075 ::operator delete(p); 00076 } 00077 00078 // size 00079 inline size_type max_size() const 00080 { 00081 return std::numeric_limits<size_type>::max() / sizeof(T); 00082 } 00083 00084 // construction/destruction 00085 inline void construct(pointer p, const T& t) { new(p) T(t); } 00086 inline void destroy(pointer p) { p->~T(); } 00087 00088 inline bool operator==(Allocator const&) { return true; } 00089 inline bool operator!=(Allocator const& a) { return !operator==(a); } 00090 }; 00091 00092 } // namespace MyGUI 00093 00094 #endif // __MYGUI_ALLOCATOR_H__