# OgreMemorySTLAllocator.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2008 Torus Knot Software Ltd
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023
    00024 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd
    00027 ---------------------------------------------------------------------------
    00028 */
    00029
    00030 #ifndef _MemorySTLAllocator_H__
    00031 #define _MemorySTLAllocator_H__
    00032
    00033 #include "OgrePrerequisites.h"
    00034
    00035 namespace Ogre
    00036 {
    00037
    00038
    00055     template
    00056         <
    00057         typename T,
    00058         typename AllocPolicy
    00059         >
    00060     class STLAllocator
    00061     {
    00062     public :
    00064         typedef T                   value_type;
    00065         typedef value_type*         pointer;
    00066         typedef const value_type*   const_pointer;
    00067         typedef value_type&         reference;
    00068         typedef const value_type&   const_reference;
    00069         typedef std::size_t         size_type;
    00070         typedef std::ptrdiff_t      difference_type;
    00071
    00072
    00074         template<typename U>
    00075         struct rebind
    00076         {
    00077             typedef STLAllocator<U, AllocPolicy> other;
    00078         };
    00079
    00081         inline explicit STLAllocator()
    00082         { }
    00083
    00085         virtual ~STLAllocator()
    00086         { }
    00087
    00089         inline STLAllocator( STLAllocator const& rhs )
    00090         { }
    00091
    00093         template <typename U>
    00094         inline STLAllocator( STLAllocator<U, AllocPolicy> const& )
    00095         { }
    00096
    00098         template <typename U, typename P>
    00099         inline STLAllocator( STLAllocator<U, P> const& rhs )
    00100         { }
    00101
    00103         inline pointer allocate( size_type count,
    00104             typename std::allocator<void>::const_pointer ptr = 0 )
    00105         {
    00106             // convert request to bytes
    00107             register size_type sz = count*sizeof( T );
    00108             pointer p  = static_cast<pointer>(AllocPolicy::allocateBytes(sz));
    00109             return p;
    00110         }
    00111
    00113         inline void deallocate( pointer ptr, size_type count )
    00114         {
    00115             // convert request to bytes, but we can't use this?
    00116             register size_type sz = count*sizeof( T );
    00117             AllocPolicy::deallocateBytes(ptr);
    00118         }
    00119
    00120         pointer address(reference x) const
    00121         {
    00122             return &x;
    00123         }
    00124
    00125         const_pointer address(const_reference x) const
    00126         {
    00127             return &x;
    00128         }
    00129
    00130         size_type max_size() const throw()
    00131         {
    00132             // maximum size this can handle, delegate
    00133             return AllocPolicy::getMaxAllocationSize();
    00134         }
    00135
    00136         void construct(pointer p, const T& val)
    00137         {
    00138             // call placement new
    00139             new(static_cast<void*>(p)) T(val);
    00140         }
    00141
    00142         void destroy(pointer p)
    00143         {
    00144             // do we have to protect against non-classes here?
    00145             // some articles suggest yes, some no
    00146             p->~T();
    00147         }
    00148
    00149     };
    00150
    00153     template<typename T, typename T2, typename P>
    00154     inline bool operator==(STLAllocator<T,P> const&,
    00155         STLAllocator<T2,P> const&)
    00156     {
    00157         // same alloc policy (P), memory can be freed
    00158         return true;
    00159     }
    00160
    00163     template<typename T, typename P, typename OtherAllocator>
    00164     inline bool operator==(STLAllocator<T,P> const&,
    00165         OtherAllocator const&)
    00166     {
    00167         return false;
    00168     }
    00171     template<typename T, typename T2, typename P>
    00172     inline bool operator!=(STLAllocator<T,P> const&,
    00173         STLAllocator<T2,P> const&)
    00174     {
    00175         // same alloc policy (P), memory can be freed
    00176         return false;
    00177     }
    00178
    00181     template<typename T, typename P, typename OtherAllocator>
    00182     inline bool operator!=(STLAllocator<T,P> const&,
    00183         OtherAllocator const&)
    00184     {
    00185         return true;
    00186     }
    00187
    00188
    00189
    00190 }// namespace Ogre
    00191
    00192 #endif // _MemorySTLAllocator_H__
    00193

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sat Apr 11 14:26:42 2009