2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_MEMORY_ALLOCATOR_H )
23 #define INCLUDED_MEMORY_ALLOCATOR_H
30 #define DefaultAllocator std::allocator
34 /// \brief An allocator that uses c++ new/delete.
35 /// Compliant with the std::allocator interface.
36 template<typename Type>
37 class DefaultAllocator
41 typedef Type value_type;
42 typedef value_type* pointer;
43 typedef const Type* const_pointer;
44 typedef Type& reference;
45 typedef const Type& const_reference;
46 typedef size_t size_type;
47 typedef ptrdiff_t difference_type;
49 template<typename Other>
52 typedef DefaultAllocator<Other> other;
57 DefaultAllocator( const DefaultAllocator<Type>& ){
59 template<typename Other> DefaultAllocator( const DefaultAllocator<Other>& ){
64 pointer address( reference instance ) const {
67 const_pointer address( const_reference instance ) const {
70 Type* allocate( size_type size, const void* = 0 ){
71 return static_cast<Type*>( ::operator new( size * sizeof( Type ) ) );
73 void deallocate( pointer p, size_type ){
74 ::operator delete( p );
76 size_type max_size() const {
77 return std::size_t( -1 ) / sizeof( Type );
79 void construct( pointer p, const Type& value ){
82 void destroy( pointer p ){
87 template<typename Type, typename Other>
88 inline bool operator==( const DefaultAllocator<Type>&, const DefaultAllocator<Other>& ){
91 template<typename Type, typename OtherAllocator>
92 inline bool operator==( const DefaultAllocator<Type>&, const OtherAllocator& ){
99 template<typename Type>
100 class NamedAllocator : public DefaultAllocator<Type>
102 typedef DefaultAllocator<Type> allocator_type;
107 typedef Type value_type;
108 typedef value_type* pointer;
109 typedef const Type* const_pointer;
110 typedef Type& reference;
111 typedef const Type& const_reference;
112 typedef size_t size_type;
113 typedef ptrdiff_t difference_type;
115 template<typename Other>
118 typedef NamedAllocator<Other> other;
121 explicit NamedAllocator( const char* name ) : m_name( name ){
123 NamedAllocator( const NamedAllocator<Type>& other ) : m_name( other.m_name ){
125 template<typename Other> NamedAllocator( const NamedAllocator<Other>& other ) : m_name( other.m_name ){
130 pointer address( reference instance ) const {
131 return allocator_type::address( instance );
133 const_pointer address( const_reference instance ) const {
134 return allocator_type::address( instance );
136 Type* allocate( size_type size, const void* = 0 ){
137 return allocator_type::allocate( size );
139 void deallocate( pointer p, size_type size ){
140 allocator_type::deallocate( p, size );
142 size_type max_size() const {
143 return allocator_type::max_size();
145 void construct( pointer p, const Type& value ){
146 allocator_type::construct( p, value );
148 void destroy( pointer p ){
149 allocator_type::destroy( p );
152 template<typename Other>
153 bool operator==( const NamedAllocator<Other>& other ){
157 // returns true if the allocators are not interchangeable
158 template<typename Other>
159 bool operator!=( const NamedAllocator<Other>& other ){
167 #include "generic/object.h"
171 template<typename Type>
172 class DefaultConstruct
175 void operator()( Type& t ){
180 template<typename Type, typename T1>
185 Construct( const T1& other_ ) : other( other_ ){
187 void operator()( Type& t ){
188 constructor( t, other );
192 template<typename Type>
196 void operator()( Type& t ){
201 template<typename Type, typename Allocator = DefaultAllocator<Type> >
202 class New : public Allocator
207 explicit New( const Allocator& allocator ) : Allocator( allocator ){
211 return new( Allocator::allocate( 1 ) )Type();
213 template<typename T1>
214 Type* scalar( const T1& t1 ){
215 return new( Allocator::allocate( 1 ) )Type( t1 );
217 template<typename T1, typename T2>
218 Type* scalar( const T1& t1, const T2& t2 ){
219 return new( Allocator::allocate( 1 ) )Type( t1, t2 );
221 template<typename T1, typename T2, typename T3>
222 Type* scalar( const T1& t1, const T2& t2, const T3& t3 ){
223 return new( Allocator::allocate( 1 ) )Type( t1, t2, t3 );
225 template<typename T1, typename T2, typename T3, typename T4>
226 Type* scalar( const T1& t1, const T2& t2, const T3& t3, const T4& t4 ){
227 return new( Allocator::allocate( 1 ) )Type( t1, t2, t3, t4 );
229 template<typename T1, typename T2, typename T3, typename T4, typename T5>
230 Type* scalar( const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5 ){
231 return new( Allocator::allocate( 1 ) )Type( t1, t2, t3, t4, t5 );
233 Type* vector( std::size_t size ){
235 Type* p = Allocator::allocate( size );
236 std::for_each( p, p + size, DefaultConstruct<Type>() );
239 // this does not work with msvc71 runtime
240 return new( Allocator::allocate( size ) )Type[size];
243 template<typename T1>
244 Type* vector( std::size_t size, const T1& t1 ){
245 Type* p = Allocator::allocate( size );
246 std::for_each( p, p + size, Construct<Type, T1>( t1 ) );
251 template<typename Type, typename Allocator = DefaultAllocator<Type> >
252 class Delete : public Allocator
257 explicit Delete( const Allocator& allocator ) : Allocator( allocator ){
260 void scalar( Type* p ){
263 Allocator::deallocate( p, 1 );
266 void vector( Type* p, std::size_t size ){
267 // '::operator delete' handles null
268 // 'std::allocator::deallocate' requires non-null
270 std::for_each( p, p + size, Destroy<Type>() );
271 Allocator::deallocate( p, size );
277 template<typename Type>
281 typedef New<Type, NamedAllocator<Type> > type;
284 template<typename Type>
288 typedef Delete<Type, NamedAllocator<Type> > type;