allocator.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2001-2006, William Joseph.
00003 All Rights Reserved.
00004 
00005 This file is part of GtkRadiant.
00006 
00007 GtkRadiant is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 GtkRadiant is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GtkRadiant; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 
00022 #if !defined(INCLUDED_MEMORY_ALLOCATOR_H)
00023 #define INCLUDED_MEMORY_ALLOCATOR_H
00024 
00025 #include <memory>
00026 
00029 template<typename Type>
00030 class DefaultAllocator {
00031 public:
00032 
00033     typedef Type value_type;
00034     typedef value_type* pointer;
00035     typedef const Type* const_pointer;
00036     typedef Type& reference;
00037     typedef const Type& const_reference;
00038     typedef size_t size_type;
00039     typedef ptrdiff_t difference_type;
00040 
00041     template<typename Other>
00042     struct rebind {
00043         typedef DefaultAllocator<Other> other;
00044     };
00045 
00046     DefaultAllocator() {
00047     }
00048     DefaultAllocator(const DefaultAllocator<Type>&) {
00049     }
00050     template<typename Other> DefaultAllocator(const DefaultAllocator<Other>&) {
00051     }
00052     ~DefaultAllocator() {
00053     }
00054 
00055     pointer address(reference instance) const {
00056         return &instance;
00057     }
00058     const_pointer address(const_reference instance) const {
00059         return &instance;
00060     }
00061     Type* allocate(size_type size, const void* = 0) {
00062         return static_cast<Type*>(::operator new(size * sizeof(Type)));
00063     }
00064     void deallocate(pointer p, size_type) {
00065         ::operator delete(p);
00066     }
00067     size_type max_size() const {
00068         return std::size_t(-1) / sizeof (Type);
00069     }
00070     void construct(pointer p, const Type& value) {
00071         new(p) Type(value);
00072     }
00073     void destroy(pointer p) {
00074         p->~Type();
00075     }
00076 };
00077 
00078 template<typename Type, typename Other>
00079 inline bool operator==(const DefaultAllocator<Type>&, const DefaultAllocator<Other>&) {
00080     return true;
00081 }
00082 template<typename Type, typename OtherAllocator>
00083 inline bool operator==(const DefaultAllocator<Type>&, const OtherAllocator&) {
00084     return false;
00085 }
00086 
00087 
00088 
00089 #include <algorithm>
00090 #include "generic/object.h"
00091 
00092 
00093 
00094 template<typename Type>
00095 class DefaultConstruct {
00096 public:
00097     void operator()(Type& t) {
00098         constructor(t);
00099     }
00100 };
00101 
00102 template<typename Type, typename T1>
00103 class Construct {
00104     const T1& other;
00105 public:
00106     Construct(const T1& other_) : other(other_) {
00107     }
00108     void operator()(Type& t) {
00109         constructor(t, other);
00110     }
00111 };
00112 
00113 template<typename Type>
00114 class Destroy {
00115 public:
00116     void operator()(Type& t) {
00117         destructor(t);
00118     }
00119 };
00120 
00121 template < typename Type, typename Allocator = DefaultAllocator<Type> >
00122 class New : public Allocator {
00123 public:
00124     New() {
00125     }
00126     explicit New(const Allocator& allocator) : Allocator(allocator) {
00127     }
00128 
00129     Type* scalar() {
00130         return new(Allocator::allocate(1)) Type();
00131     }
00132     template<typename T1>
00133     Type* scalar(const T1& t1) {
00134         return new(Allocator::allocate(1)) Type(t1);
00135     }
00136     template<typename T1, typename T2>
00137     Type* scalar(const T1& t1, const T2& t2) {
00138         return new(Allocator::allocate(1)) Type(t1, t2);
00139     }
00140     template<typename T1, typename T2, typename T3>
00141     Type* scalar(const T1& t1, const T2& t2, const T3& t3) {
00142         return new(Allocator::allocate(1)) Type(t1, t2, t3);
00143     }
00144     template<typename T1, typename T2, typename T3, typename T4>
00145     Type* scalar(const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
00146         return new(Allocator::allocate(1)) Type(t1, t2, t3, t4);
00147     }
00148     template<typename T1, typename T2, typename T3, typename T4, typename T5>
00149     Type* scalar(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) {
00150         return new(Allocator::allocate(1)) Type(t1, t2, t3, t4, t5);
00151     }
00152     Type* vector(std::size_t size) {
00153         Type* p = Allocator::allocate(size);
00154         std::for_each(p, p + size, DefaultConstruct<Type>());
00155         return p;
00156     }
00157     template<typename T1>
00158     Type* vector(std::size_t size, const T1& t1) {
00159         Type* p = Allocator::allocate(size);
00160         std::for_each(p, p + size, Construct<Type, T1>(t1));
00161         return p;
00162     }
00163 };
00164 
00165 template < typename Type, typename Allocator = DefaultAllocator<Type> >
00166 class Delete : public Allocator {
00167 public:
00168     Delete() {
00169     }
00170     explicit Delete(const Allocator& allocator) : Allocator(allocator) {
00171     }
00172 
00173     void scalar(Type* p) {
00174         if (p != 0) {
00175             p->~Type();
00176             Allocator::deallocate(p, 1);
00177         }
00178     }
00179     void vector(Type* p, std::size_t size) {
00180         // '::operator delete' handles null
00181         // 'std::allocator::deallocate' requires non-null
00182         if (p != 0) {
00183             std::for_each(p, p + size, Destroy<Type>());
00184             Allocator::deallocate(p, size);
00185         }
00186     }
00187 };
00188 
00189 #endif

Generated by  doxygen 1.6.2