MyGUI  3.0.3
MyGUI_Allocator.h
Go to the documentation of this file.
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 namespace MyGUI
00030 {
00031 
00032     template<typename T>
00033     class Allocator
00034     {
00035     public:
00036         //    typedefs
00037         typedef T value_type;
00038         typedef value_type* pointer;
00039         typedef const value_type* const_pointer;
00040         typedef value_type& reference;
00041         typedef const value_type& const_reference;
00042         typedef std::size_t size_type;
00043         typedef std::ptrdiff_t difference_type;
00044 
00045     public:
00046         //    convert an allocator<T> to allocator<U>
00047         template<typename U>
00048         struct rebind
00049         {
00050             typedef Allocator<U> other;
00051         };
00052 
00053     public:
00054         inline explicit Allocator() { }
00055         inline ~Allocator() { }
00056         template<typename U>
00057         inline explicit Allocator(Allocator<U> const&) { }
00058 
00059         //    address
00060         inline pointer address(reference r) { return &r; }
00061         inline const_pointer address(const_reference r) { return &r; }
00062 
00063         //    memory allocation
00064         inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
00065         {
00066             return reinterpret_cast<pointer>(::operator new (cnt * sizeof (T)));
00067         }
00068         inline void deallocate(pointer p, size_type)
00069         {
00070             ::operator delete (p);
00071         }
00072 
00073         //    size
00074         inline size_type max_size() const
00075         {
00076             return std::numeric_limits<size_type>::max() / sizeof(T);
00077         }
00078 
00079         //    construction/destruction
00080         inline void construct(pointer p, const T& t) { new (p) T(t); }
00081         inline void destroy(pointer p) { p->~T(); }
00082 
00083         inline bool operator==(Allocator const&) { return true; }
00084         inline bool operator!=(Allocator const& a) { return !operator==(a); }
00085     };
00086 
00087 } // namespace MyGUI
00088 
00089 #endif // __MYGUI_ALLOCATOR_H__