MyGUI  3.2.0
MyGUI_Allocator.h
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #ifndef __MYGUI_ALLOCATOR_H__
00023 #define __MYGUI_ALLOCATOR_H__
00024 
00025 #include <memory>
00026 #include <limits>
00027 
00028 namespace MyGUI
00029 {
00030 
00031     template<typename T>
00032     class Allocator
00033     {
00034     public:
00035         //    typedefs
00036         typedef T value_type;
00037         typedef value_type* pointer;
00038         typedef const value_type* const_pointer;
00039         typedef value_type& reference;
00040         typedef const value_type& const_reference;
00041         typedef std::size_t size_type;
00042         typedef std::ptrdiff_t difference_type;
00043 
00044     public:
00045         //    convert an allocator<T> to allocator<U>
00046         template<typename U>
00047         struct rebind
00048         {
00049             typedef Allocator<U> other;
00050         };
00051 
00052     public:
00053         inline explicit Allocator() { }
00054         inline ~Allocator() { }
00055         template<typename U>
00056         inline explicit Allocator(Allocator<U> const&) { }
00057 
00058         //    address
00059         inline pointer address(reference r)
00060         {
00061             return &r;
00062         }
00063         inline const_pointer address(const_reference r)
00064         {
00065             return &r;
00066         }
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)
00086         {
00087             new (p) T(t);
00088         }
00089         inline void destroy(pointer p)
00090         {
00091             p->~T();
00092         }
00093 
00094         inline bool operator==(Allocator const&)
00095         {
00096             return true;
00097         }
00098         inline bool operator!=(Allocator const& a)
00099         {
00100             return !operator==(a);
00101         }
00102     };
00103 
00104 } // namespace MyGUI
00105 
00106 #endif // __MYGUI_ALLOCATOR_H__