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