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 
00061     template
00062         <
00063         typename T,
00064         typename AllocPolicy
00065         >
00066     class STLAllocator 
00067     {
00068     public :
00070         typedef T                   value_type;
00071         typedef value_type*         pointer;
00072         typedef const value_type*   const_pointer;
00073         typedef value_type&         reference;
00074         typedef const value_type&   const_reference;
00075         typedef std::size_t         size_type;
00076         typedef std::ptrdiff_t      difference_type;
00077 
00078 
00080         template<typename U>
00081         struct rebind
00082         {
00083             typedef STLAllocator<U, AllocPolicy> other;
00084         };
00085 
00087         inline explicit STLAllocator()
00088         { }
00089 
00091         virtual ~STLAllocator()
00092         { }
00093 
00095         inline STLAllocator( STLAllocator const& )
00096         { }
00097 
00099         template <typename U>
00100         inline STLAllocator( STLAllocator<U, AllocPolicy> const& )
00101         { }
00102 
00104         template <typename U, typename P>
00105         inline STLAllocator( STLAllocator<U, P> const& )
00106         { }
00107 
00109         inline pointer allocate( size_type count,
00110             typename std::allocator<void>::const_pointer ptr = 0 )
00111         {
00112                         (void)ptr;
00113             // convert request to bytes
00114             register size_type sz = count*sizeof( T );
00115             pointer p  = static_cast<pointer>(AllocPolicy::allocateBytes(sz));
00116             return p;
00117         }
00118 
00120         inline void deallocate( pointer ptr, size_type )
00121         {
00122             // convert request to bytes, but we can't use this?
00123             // register size_type sz = count*sizeof( T );
00124             AllocPolicy::deallocateBytes(ptr);
00125         }
00126 
00127         pointer address(reference x) const
00128         {
00129             return &x;
00130         }
00131 
00132         const_pointer address(const_reference x) const
00133         {
00134             return &x;
00135         }
00136 
00137         size_type max_size() const throw()
00138         {
00139             // maximum size this can handle, delegate
00140             return AllocPolicy::getMaxAllocationSize();
00141         }
00142 
00143         void construct(pointer p, const T& val)
00144         {
00145             // call placement new
00146             new(static_cast<void*>(p)) T(val);
00147         }
00148 
00149         void destroy(pointer p)
00150         {
00151             // do we have to protect against non-classes here?
00152             // some articles suggest yes, some no
00153             p->~T();
00154         }
00155 
00156     };
00157 
00160     template<typename T, typename T2, typename P>
00161     inline bool operator==(STLAllocator<T,P> const&,
00162         STLAllocator<T2,P> const&)
00163     {
00164         // same alloc policy (P), memory can be freed
00165         return true;
00166     }
00167 
00170     template<typename T, typename P, typename OtherAllocator>
00171     inline bool operator==(STLAllocator<T,P> const&,
00172         OtherAllocator const&)
00173     {
00174         return false;
00175     }
00178     template<typename T, typename T2, typename P>
00179     inline bool operator!=(STLAllocator<T,P> const&,
00180         STLAllocator<T2,P> const&)
00181     {
00182         // same alloc policy (P), memory can be freed
00183         return false;
00184     }
00185 
00188     template<typename T, typename P, typename OtherAllocator>
00189     inline bool operator!=(STLAllocator<T,P> const&,
00190         OtherAllocator const&)
00191     {
00192         return true;
00193     }
00194 
00195 
00199 }// namespace Ogre
00200 
00201 #include "OgreHeaderSuffix.h"
00202 #endif // _MemorySTLAllocator_H__
00203 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Thu Dec 31 16:27:11 2009