MyGUI  3.2.1
MyGUI_CustomAllocator.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_CUSTOM_ALLOCATOR_H__
00008 #define __MYGUI_CUSTOM_ALLOCATOR_H__
00009 
00010 #include <memory>
00011 #include <limits>
00012 
00013 // for Ogre version
00014 #include <OgrePrerequisites.h>
00015 
00016 #if OGRE_VERSION < MYGUI_DEFINE_VERSION(1, 6, 0)
00017 #include <OgreMemoryManager.h>
00018 #include <OgreNoMemoryMacros.h>
00019 #endif
00020 
00021 namespace MyGUI
00022 {
00023 
00024     template<typename T>
00025     class Allocator
00026     {
00027     public:
00028         //    typedefs
00029         typedef T value_type;
00030         typedef value_type* pointer;
00031         typedef const value_type* const_pointer;
00032         typedef value_type& reference;
00033         typedef const value_type& const_reference;
00034         typedef std::size_t size_type;
00035         typedef std::ptrdiff_t difference_type;
00036 
00037     public:
00038         //    convert an allocator<T> to allocator<U>
00039         template<typename U>
00040         struct rebind
00041         {
00042             typedef Allocator<U> other;
00043         };
00044 
00045     public:
00046         inline explicit Allocator() { }
00047         inline ~Allocator() { }
00048         template<typename U>
00049         inline explicit Allocator(Allocator<U> const&) { }
00050 
00051         //    address
00052         inline pointer address(reference r)
00053         {
00054             return &r;
00055         }
00056         inline const_pointer address(const_reference r)
00057         {
00058             return &r;
00059         }
00060 
00061         //    memory allocation
00062         inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
00063         {
00064             return reinterpret_cast<pointer>(::operator new (cnt * sizeof (T)));
00065         }
00066         inline void deallocate(pointer p, size_type)
00067         {
00068             ::operator delete (p);
00069         }
00070 
00071         //    size
00072         inline size_type max_size() const
00073         {
00074             return (std::numeric_limits<size_type>::max)() / sizeof(T);
00075         }
00076 
00077         //    construction/destruction
00078         inline void construct(pointer p, const T& t)
00079         {
00080             new (p) T(t);
00081         }
00082         inline void destroy(pointer p)
00083         {
00084             p->~T();
00085         }
00086 
00087         inline bool operator==(Allocator const&)
00088         {
00089             return true;
00090         }
00091         inline bool operator!=(Allocator const& a)
00092         {
00093             return !operator==(a);
00094         }
00095     };
00096 
00097 } // namespace MyGUI
00098 
00099 #if OGRE_VERSION < MYGUI_DEFINE_VERSION(1, 6, 0)
00100 #include <OgreMemoryMacros.h>
00101 #endif
00102 
00103 #endif // __MYGUI_CUSTOM_ALLOCATOR_H__