MyGUI  3.2.1
MyGUI_Allocator.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_ALLOCATOR_H__
00008 #define __MYGUI_ALLOCATOR_H__
00009 
00010 #include <memory>
00011 #include <limits>
00012 
00013 namespace MyGUI
00014 {
00015 
00016     template<typename T>
00017     class Allocator
00018     {
00019     public:
00020         //    typedefs
00021         typedef T value_type;
00022         typedef value_type* pointer;
00023         typedef const value_type* const_pointer;
00024         typedef value_type& reference;
00025         typedef const value_type& const_reference;
00026         typedef std::size_t size_type;
00027         typedef std::ptrdiff_t difference_type;
00028 
00029     public:
00030         //    convert an allocator<T> to allocator<U>
00031         template<typename U>
00032         struct rebind
00033         {
00034             typedef Allocator<U> other;
00035         };
00036 
00037     public:
00038         inline explicit Allocator() { }
00039         inline ~Allocator() { }
00040         template<typename U>
00041         inline explicit Allocator(Allocator<U> const&) { }
00042 
00043         //    address
00044         inline pointer address(reference r)
00045         {
00046             return &r;
00047         }
00048         inline const_pointer address(const_reference r)
00049         {
00050             return &r;
00051         }
00052 
00053         //    memory allocation
00054         inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
00055         {
00056             return reinterpret_cast<pointer>(::operator new (cnt * sizeof (T)));
00057         }
00058         inline void deallocate(pointer p, size_type)
00059         {
00060             ::operator delete (p);
00061         }
00062 
00063         //    size
00064         inline size_type max_size() const
00065         {
00066             return (std::numeric_limits<size_type>::max)() / sizeof(T);
00067         }
00068 
00069         //    construction/destruction
00070         inline void construct(pointer p, const T& t)
00071         {
00072             new (p) T(t);
00073         }
00074         inline void destroy(pointer p)
00075         {
00076             p->~T();
00077         }
00078 
00079         inline bool operator==(Allocator const&)
00080         {
00081             return true;
00082         }
00083         inline bool operator!=(Allocator const& a)
00084         {
00085             return !operator==(a);
00086         }
00087     };
00088 
00089 } // namespace MyGUI
00090 
00091 #endif // __MYGUI_ALLOCATOR_H__