array_allocator.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _ARRAY_ALLOCATOR_H
00035 #define _ARRAY_ALLOCATOR_H 1
00036
00037 #include <cstddef>
00038 #include <new>
00039 #include <bits/functexcept.h>
00040 #include <tr1/array>
00041 #include <bits/stl_move.h>
00042
00043 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00044
00045 using std::size_t;
00046 using std::ptrdiff_t;
00047
00048
00049 template<typename _Tp>
00050 class array_allocator_base
00051 {
00052 public:
00053 typedef size_t size_type;
00054 typedef ptrdiff_t difference_type;
00055 typedef _Tp* pointer;
00056 typedef const _Tp* const_pointer;
00057 typedef _Tp& reference;
00058 typedef const _Tp& const_reference;
00059 typedef _Tp value_type;
00060
00061 pointer
00062 address(reference __x) const { return &__x; }
00063
00064 const_pointer
00065 address(const_reference __x) const { return &__x; }
00066
00067 void
00068 deallocate(pointer, size_type)
00069 {
00070
00071 }
00072
00073 size_type
00074 max_size() const throw()
00075 { return size_t(-1) / sizeof(_Tp); }
00076
00077
00078
00079 void
00080 construct(pointer __p, const _Tp& __val)
00081 { ::new((void *)__p) value_type(__val); }
00082
00083 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00084 template<typename... _Args>
00085 void
00086 construct(pointer __p, _Args&&... __args)
00087 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
00088 #endif
00089
00090 void
00091 destroy(pointer __p) { __p->~_Tp(); }
00092 };
00093
00094
00095
00096
00097
00098 template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
00099 class array_allocator : public array_allocator_base<_Tp>
00100 {
00101 public:
00102 typedef size_t size_type;
00103 typedef ptrdiff_t difference_type;
00104 typedef _Tp* pointer;
00105 typedef const _Tp* const_pointer;
00106 typedef _Tp& reference;
00107 typedef const _Tp& const_reference;
00108 typedef _Tp value_type;
00109 typedef _Array array_type;
00110
00111 private:
00112 array_type* _M_array;
00113 size_type _M_used;
00114
00115 public:
00116 template<typename _Tp1, typename _Array1 = _Array>
00117 struct rebind
00118 { typedef array_allocator<_Tp1, _Array1> other; };
00119
00120 array_allocator(array_type* __array = NULL) throw()
00121 : _M_array(__array), _M_used(size_type()) { }
00122
00123 array_allocator(const array_allocator& __o) throw()
00124 : _M_array(__o._M_array), _M_used(__o._M_used) { }
00125
00126 template<typename _Tp1, typename _Array1>
00127 array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
00128 : _M_array(NULL), _M_used(size_type()) { }
00129
00130 ~array_allocator() throw() { }
00131
00132 pointer
00133 allocate(size_type __n, const void* = 0)
00134 {
00135 if (_M_array == 0 || _M_used + __n > _M_array->size())
00136 std::__throw_bad_alloc();
00137 pointer __ret = _M_array->begin() + _M_used;
00138 _M_used += __n;
00139 return __ret;
00140 }
00141 };
00142
00143 template<typename _Tp, typename _Array>
00144 inline bool
00145 operator==(const array_allocator<_Tp, _Array>&,
00146 const array_allocator<_Tp, _Array>&)
00147 { return true; }
00148
00149 template<typename _Tp, typename _Array>
00150 inline bool
00151 operator!=(const array_allocator<_Tp, _Array>&,
00152 const array_allocator<_Tp, _Array>&)
00153 { return false; }
00154
00155 _GLIBCXX_END_NAMESPACE
00156
00157 #endif