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
29 #define DefaultAllocator std::allocator
33 /// \brief An allocator that uses c++ new/delete.
34 /// Compliant with the std::allocator interface.
35 template<typename Type>
36 class DefaultAllocator
40 typedef Type value_type;
41 typedef value_type* pointer;
42 typedef const Type* const_pointer;
43 typedef Type& reference;
44 typedef const Type& const_reference;
45 typedef size_t size_type;
46 typedef ptrdiff_t difference_type;
48 template<typename Other>
51 typedef DefaultAllocator<Other> other;
57 DefaultAllocator(const DefaultAllocator<Type>&)
60 template<typename Other> DefaultAllocator(const DefaultAllocator<Other>&)
67 pointer address(reference instance) const
71 const_pointer address(const_reference instance) const
75 Type* allocate(size_type size, const void* = 0)
77 return static_cast<Type*>(::operator new(size * sizeof(Type)));
79 void deallocate(pointer p, size_type)
83 size_type max_size() const
85 return std::size_t(-1) / sizeof (Type);
87 void construct(pointer p, const Type& value)
91 void destroy(pointer p)
97 template<typename Type, typename Other>
98 inline bool operator==(const DefaultAllocator<Type>&, const DefaultAllocator<Other>&)
102 template<typename Type, typename OtherAllocator>
103 inline bool operator==(const DefaultAllocator<Type>&, const OtherAllocator&)
111 template<typename Type>
112 class NamedAllocator : public DefaultAllocator<Type>
114 typedef DefaultAllocator<Type> allocator_type;
119 typedef Type value_type;
120 typedef value_type* pointer;
121 typedef const Type* const_pointer;
122 typedef Type& reference;
123 typedef const Type& const_reference;
124 typedef size_t size_type;
125 typedef ptrdiff_t difference_type;
127 template<typename Other>
130 typedef NamedAllocator<Other> other;
133 explicit NamedAllocator(const char* name) : m_name(name)
136 NamedAllocator(const NamedAllocator<Type>& other) : m_name(other.m_name)
139 template<typename Other> NamedAllocator(const NamedAllocator<Other>& other) : m_name(other.m_name)
146 pointer address(reference instance) const
148 return allocator_type::address(instance);
150 const_pointer address(const_reference instance) const
152 return allocator_type::address(instance);
154 Type* allocate(size_type size, const void* = 0)
156 return allocator_type::allocate(size);
158 void deallocate(pointer p, size_type size)
160 allocator_type::deallocate(p, size);
162 size_type max_size() const
164 return allocator_type::max_size();
166 void construct(pointer p, const Type& value)
168 allocator_type::construct(p, value);
170 void destroy(pointer p)
172 allocator_type::destroy(p);
175 template<typename Other>
176 bool operator==(const NamedAllocator<Other>& other)
181 // returns true if the allocators are not interchangeable
182 template<typename Other>
183 bool operator!=(const NamedAllocator<Other>& other)
192 #include "generic/object.h"
196 template<typename Type>
197 class DefaultConstruct
200 void operator()(Type& t)
206 template<typename Type, typename T1>
211 Construct(const T1& other_) : other(other_)
214 void operator()(Type& t)
216 constructor(t, other);
220 template<typename Type>
224 void operator()(Type& t)
230 template<typename Type, typename Allocator = DefaultAllocator<Type> >
231 class New : public Allocator
237 explicit New(const Allocator& allocator) : Allocator(allocator)
243 return new(Allocator::allocate(1)) Type();
245 template<typename T1>
246 Type* scalar(const T1& t1)
248 return new(Allocator::allocate(1)) Type(t1);
250 template<typename T1, typename T2>
251 Type* scalar(const T1& t1, const T2& t2)
253 return new(Allocator::allocate(1)) Type(t1, t2);
255 template<typename T1, typename T2, typename T3>
256 Type* scalar(const T1& t1, const T2& t2, const T3& t3)
258 return new(Allocator::allocate(1)) Type(t1, t2, t3);
260 template<typename T1, typename T2, typename T3, typename T4>
261 Type* scalar(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
263 return new(Allocator::allocate(1)) Type(t1, t2, t3, t4);
265 template<typename T1, typename T2, typename T3, typename T4, typename T5>
266 Type* scalar(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
268 return new(Allocator::allocate(1)) Type(t1, t2, t3, t4, t5);
270 Type* vector(std::size_t size)
273 Type* p = Allocator::allocate(size);
274 std::for_each(p, p + size, DefaultConstruct<Type>());
277 // this does not work with msvc71 runtime
278 return new(Allocator::allocate(size)) Type[size];
281 template<typename T1>
282 Type* vector(std::size_t size, const T1& t1)
284 Type* p = Allocator::allocate(size);
285 std::for_each(p, p + size, Construct<Type, T1>(t1));
290 template<typename Type, typename Allocator = DefaultAllocator<Type> >
291 class Delete : public Allocator
297 explicit Delete(const Allocator& allocator) : Allocator(allocator)
306 Allocator::deallocate(p, 1);
309 void vector(Type* p, std::size_t size)
311 // '::operator delete' handles null
312 // 'std::allocator::deallocate' requires non-null
315 std::for_each(p, p + size, Destroy<Type>());
316 Allocator::deallocate(p, size);
322 template<typename Type>
326 typedef New<Type, NamedAllocator<Type> > type;
329 template<typename Type>
333 typedef Delete<Type, NamedAllocator<Type> > type;