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;
58 DefaultAllocator(const DefaultAllocator<Type>&)
61 template<typename Other> DefaultAllocator(const DefaultAllocator<Other>&)
68 pointer address(reference instance) const
72 const_pointer address(const_reference instance) const
76 Type* allocate(size_type size, const void* = 0)
78 return static_cast<Type*>(::operator new(size * sizeof(Type)));
80 void deallocate(pointer p, size_type)
84 size_type max_size() const
86 return std::size_t(-1) / sizeof (Type);
88 void construct(pointer p, const Type& value)
92 void destroy(pointer p)
98 template<typename Type, typename Other>
99 inline bool operator==(const DefaultAllocator<Type>&, const DefaultAllocator<Other>&)
103 template<typename Type, typename OtherAllocator>
104 inline bool operator==(const DefaultAllocator<Type>&, const OtherAllocator&)
112 template<typename Type>
113 class NamedAllocator : public DefaultAllocator<Type>
115 typedef DefaultAllocator<Type> allocator_type;
120 typedef Type value_type;
121 typedef value_type* pointer;
122 typedef const Type* const_pointer;
123 typedef Type& reference;
124 typedef const Type& const_reference;
125 typedef size_t size_type;
126 typedef ptrdiff_t difference_type;
128 template<typename Other>
131 typedef NamedAllocator<Other> other;
134 explicit NamedAllocator(const char* name) : m_name(name)
137 NamedAllocator(const NamedAllocator<Type>& other) : m_name(other.m_name)
140 template<typename Other> NamedAllocator(const NamedAllocator<Other>& other) : m_name(other.m_name)
147 pointer address(reference instance) const
149 return allocator_type::address(instance);
151 const_pointer address(const_reference instance) const
153 return allocator_type::address(instance);
155 Type* allocate(size_type size, const void* = 0)
157 return allocator_type::allocate(size);
159 void deallocate(pointer p, size_type size)
161 allocator_type::deallocate(p, size);
163 size_type max_size() const
165 return allocator_type::max_size();
167 void construct(pointer p, const Type& value)
169 allocator_type::construct(p, value);
171 void destroy(pointer p)
173 allocator_type::destroy(p);
176 template<typename Other>
177 bool operator==(const NamedAllocator<Other>& other)
182 // returns true if the allocators are not interchangeable
183 template<typename Other>
184 bool operator!=(const NamedAllocator<Other>& other)
193 #include "generic/object.h"
197 template<typename Type>
198 class DefaultConstruct
201 void operator()(Type& t)
207 template<typename Type, typename T1>
212 Construct(const T1& other_) : other(other_)
215 void operator()(Type& t)
217 constructor(t, other);
221 template<typename Type>
225 void operator()(Type& t)
231 template<typename Type, typename Allocator = DefaultAllocator<Type> >
232 class New : public Allocator
238 explicit New(const Allocator& allocator) : Allocator(allocator)
244 return new(Allocator::allocate(1)) Type();
246 template<typename T1>
247 Type* scalar(const T1& t1)
249 return new(Allocator::allocate(1)) Type(t1);
251 template<typename T1, typename T2>
252 Type* scalar(const T1& t1, const T2& t2)
254 return new(Allocator::allocate(1)) Type(t1, t2);
256 template<typename T1, typename T2, typename T3>
257 Type* scalar(const T1& t1, const T2& t2, const T3& t3)
259 return new(Allocator::allocate(1)) Type(t1, t2, t3);
261 template<typename T1, typename T2, typename T3, typename T4>
262 Type* scalar(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
264 return new(Allocator::allocate(1)) Type(t1, t2, t3, t4);
266 template<typename T1, typename T2, typename T3, typename T4, typename T5>
267 Type* scalar(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
269 return new(Allocator::allocate(1)) Type(t1, t2, t3, t4, t5);
271 Type* vector(std::size_t size)
274 Type* p = Allocator::allocate(size);
275 std::for_each(p, p + size, DefaultConstruct<Type>());
278 // this does not work with msvc71 runtime
279 return new(Allocator::allocate(size)) Type[size];
282 template<typename T1>
283 Type* vector(std::size_t size, const T1& t1)
285 Type* p = Allocator::allocate(size);
286 std::for_each(p, p + size, Construct<Type, T1>(t1));
291 template<typename Type, typename Allocator = DefaultAllocator<Type> >
292 class Delete : public Allocator
298 explicit Delete(const Allocator& allocator) : Allocator(allocator)
307 Allocator::deallocate(p, 1);
310 void vector(Type* p, std::size_t size)
312 // '::operator delete' handles null
313 // 'std::allocator::deallocate' requires non-null
316 std::for_each(p, p + size, Destroy<Type>());
317 Allocator::deallocate(p, size);
323 template<typename Type>
327 typedef New<Type, NamedAllocator<Type> > type;
330 template<typename Type>
334 typedef Delete<Type, NamedAllocator<Type> > type;