OGRE: OgreMemorySTLAllocator.h Source File - OGRE Documentation

# 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-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028
    00029 #ifndef _MemorySTLAllocator_H__
    00030 #define _MemorySTLAllocator_H__
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreHeaderPrefix.h"
    00034
    00035 namespace Ogre
    00036 {
    00037
    00038
    00062     // Base STL allocator class.
    00063     template<typename T>
    00064     struct STLAllocatorBase
    00065     {   // base class for generic allocators
    00066         typedef T value_type;
    00067     };
    00068
    00069     // Base STL allocator class. (const T version).
    00070     template<typename T>
    00071     struct STLAllocatorBase<const T>
    00072     {   // base class for generic allocators for const T
    00073         typedef T value_type;
    00074     };
    00075
    00076     template
    00077         <
    00078         typename T,
    00079         typename AllocPolicy
    00080         >
    00081     class STLAllocator : public STLAllocatorBase<T>
    00082     {
    00083     public :
    00085         typedef STLAllocatorBase<T>         Base;
    00086         typedef typename Base::value_type   value_type;
    00087         typedef value_type*                 pointer;
    00088         typedef const value_type*           const_pointer;
    00089         typedef value_type&                 reference;
    00090         typedef const value_type&           const_reference;
    00091         typedef std::size_t                 size_type;
    00092         typedef std::ptrdiff_t              difference_type;
    00093
    00094
    00096         template<typename U>
    00097         struct rebind
    00098         {
    00099             typedef STLAllocator<U, AllocPolicy> other;
    00100         };
    00101
    00103         inline explicit STLAllocator()
    00104         { }
    00105
    00107         virtual ~STLAllocator()
    00108         { }
    00109
    00111         inline STLAllocator( STLAllocator const& )
    00112         { }
    00113
    00115         template <typename U>
    00116         inline STLAllocator( STLAllocator<U, AllocPolicy> const& )
    00117         { }
    00118
    00120         template <typename U, typename P>
    00121         inline STLAllocator( STLAllocator<U, P> const& )
    00122         { }
    00123
    00125         inline pointer allocate( size_type count,
    00126             typename std::allocator<void>::const_pointer ptr = 0 )
    00127         {
    00128                         (void)ptr;
    00129             // convert request to bytes
    00130             register size_type sz = count*sizeof( T );
    00131             pointer p  = static_cast<pointer>(AllocPolicy::allocateBytes(sz));
    00132             return p;
    00133         }
    00134
    00136         inline void deallocate( pointer ptr, size_type )
    00137         {
    00138             // convert request to bytes, but we can't use this?
    00139             // register size_type sz = count*sizeof( T );
    00140             AllocPolicy::deallocateBytes(ptr);
    00141         }
    00142
    00143         pointer address(reference x) const
    00144         {
    00145             return &x;
    00146         }
    00147
    00148         const_pointer address(const_reference x) const
    00149         {
    00150             return &x;
    00151         }
    00152
    00153         size_type max_size() const throw()
    00154         {
    00155             // maximum size this can handle, delegate
    00156             return AllocPolicy::getMaxAllocationSize();
    00157         }
    00158
    00159         void construct(pointer p, const T& val)
    00160         {
    00161             // call placement new
    00162             new(static_cast<void*>(p)) T(val);
    00163         }
    00164
    00165         void destroy(pointer p)
    00166         {
    00167             // do we have to protect against non-classes here?
    00168             // some articles suggest yes, some no
    00169             p->~T();
    00170         }
    00171
    00172     };
    00173
    00176     template<typename T, typename T2, typename P>
    00177     inline bool operator==(STLAllocator<T,P> const&,
    00178         STLAllocator<T2,P> const&)
    00179     {
    00180         // same alloc policy (P), memory can be freed
    00181         return true;
    00182     }
    00183
    00186     template<typename T, typename P, typename OtherAllocator>
    00187     inline bool operator==(STLAllocator<T,P> const&,
    00188         OtherAllocator const&)
    00189     {
    00190         return false;
    00191     }
    00194     template<typename T, typename T2, typename P>
    00195     inline bool operator!=(STLAllocator<T,P> const&,
    00196         STLAllocator<T2,P> const&)
    00197     {
    00198         // same alloc policy (P), memory can be freed
    00199         return false;
    00200     }
    00201
    00204     template<typename T, typename P, typename OtherAllocator>
    00205     inline bool operator!=(STLAllocator<T,P> const&,
    00206         OtherAllocator const&)
    00207     {
    00208         return true;
    00209     }
    00210
    00211
    00215 }// namespace Ogre
    00216
    00217 #include "OgreHeaderSuffix.h"
    00218 #endif // _MemorySTLAllocator_H__
    00219

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52