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