STLAllocator.h

Go to the documentation of this file.
00001 #ifndef __LDK_STLALLOCATOR_H__
00002 #define __LDK_STLALLOCATOR_H__
00003 
00005 //   Copyright (C) 2003-2006 Lorien Dunn.
00006 //
00007 //   Contact: loriendunn AT users DOT sourceforge DOT net
00008 //
00009 //   This software is provided 'as-is', without any express or implied warranty.
00010 //   In no event will the authors be held liable for any damages arising from
00011 //   the use of this software.
00012 //
00013 //   Permission is granted to anyone to use this software for any purpose,
00014 //   including commercial applications, and to alter it and redistribute it
00015 //   freely, subject to the following restrictions:
00016 //
00017 //   1. The origin of this software must not be misrepresented; you must not
00018 //   claim that you wrote the original software. If you use this software in a
00019 //   product, an acknowledgment in the product documentation would be
00020 //   appreciated but is not required.
00021 //
00022 //   2. Altered source versions must be plainly marked as such, and must not be
00023 //   misrepresented as being the original software.
00024 //
00025 //   3. This notice may not be removed or altered from any source distribution.
00026 //
00028 
00035 
00036 namespace LDK
00037 {
00047 template<class T>
00048 class Allocator
00049 {
00050 public:
00051     typedef size_t      size_type;
00052     typedef ptrdiff_t   difference_type;
00053     typedef T*          pointer;
00054     typedef const T*    const_pointer;
00055     typedef T&          reference;
00056     typedef const T&    const_reference;
00057     typedef T           value_type;
00058 
00059     template<typename T1>
00060     struct rebind
00061     {
00062         typedef Allocator<T1> other;
00063     };
00064 
00065     inline Allocator() throw() {}
00066     inline Allocator(const Allocator&) {}
00067     template<typename T1>
00068     inline Allocator(const Allocator<T1>&) {}
00069     inline ~Allocator() {}
00070 
00071     inline pointer address(reference r) const { return &r; }
00072 
00073     inline const_pointer address(const_reference r) const { return &r; }
00074 
00075 
00076     inline pointer allocate(size_type n, const void* = 0)
00077     {
00078        assert(n);
00079        return reinterpret_cast<pointer>(LDK_RTALLOC(n*sizeof(value_type)));
00080     }
00081 
00082     //LDK extensions
00083     template <size_type n>
00084     inline pointer allocate()
00085     {
00086         CTASSERT(n>0,allocatorInvalidObjectCount);
00087         return reinterpret_cast<pointer>(LDK_CTALLOC(n*sizeof(value_type)));
00088     }
00089 
00090     inline pointer reallocate(pointer mem, size_type newNo)
00091     {
00092         assert(mem);
00093         assert(newNo);
00094         return reinterpret_cast<pointer>(LDK_REALLOC(mem,newNo*sizeof(value_type)));
00095     }
00096 
00097     //extension for the MSVC STL
00098     inline char* _Charalloc(size_type n, const void* = 0)
00099     {
00100         assert(n);
00101         return reinterpret_cast<char*>(LDK_RTALLOC(n));
00102     }
00103 
00104     inline void deallocate(void* p, size_type n)
00105     {
00106         LDK::dealloc(p);
00107     }
00108 
00109     inline size_type max_size() const { return size_type(-1) / sizeof(value_type); }
00110 
00111     inline void construct(pointer p, const value_type& val) { new(p) value_type(val); }
00112     inline void destroy(pointer p) { p->~T(); }
00113 };
00114 template<typename T1, typename T2>
00115 inline bool operator==(const Allocator<T1>&, const Allocator<T2>&) { return true; }
00116 
00117 template<typename T1, typename T2>
00118 inline bool operator!=(const Allocator<T1>&, const Allocator<T2>&) { return false; }
00119 
00120 }//namespace LDK
00121 
00122 #endif //__LDK_STLALLOCATOR_H__
00123 

Generated on Fri Aug 17 18:32:26 2007 for LDK by  doxygen 1.5.1