libstdc++
|
00001 // <extptr_allocator.h> -*- C++ -*- 00002 00003 // Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** 00026 * @file ext/extptr_allocator.h 00027 * This file is a GNU extension to the Standard C++ Library. 00028 * 00029 * @author Bob Walters 00030 * 00031 * An example allocator which uses an alternative pointer type from 00032 * bits/pointer.h. Supports test cases which confirm container support 00033 * for alternative pointers. 00034 */ 00035 00036 #ifndef _EXTPTR_ALLOCATOR_H 00037 #define _EXTPTR_ALLOCATOR_H 1 00038 00039 #include <memory> 00040 #include <ext/numeric_traits.h> 00041 #include <ext/pointer.h> 00042 00043 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * @brief An example allocator which uses a non-standard pointer type. 00049 * @ingroup allocators 00050 * 00051 * This allocator specifies that containers use a 'relative pointer' as it's 00052 * pointer type. (See ext/pointer.h) Memory allocation in this example 00053 * is still performed using std::allocator. 00054 */ 00055 template<typename _Tp> 00056 class _ExtPtr_allocator 00057 { 00058 public: 00059 typedef std::size_t size_type; 00060 typedef std::ptrdiff_t difference_type; 00061 00062 // Note the non-standard pointer types. 00063 typedef _Pointer_adapter<_Relative_pointer_impl<_Tp> > pointer; 00064 typedef _Pointer_adapter<_Relative_pointer_impl<const _Tp> > 00065 const_pointer; 00066 00067 typedef _Tp& reference; 00068 typedef const _Tp& const_reference; 00069 typedef _Tp value_type; 00070 00071 template<typename _Up> 00072 struct rebind 00073 { typedef _ExtPtr_allocator<_Up> other; }; 00074 00075 _ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT 00076 : _M_real_alloc() { } 00077 00078 _ExtPtr_allocator(const _ExtPtr_allocator& __rarg) _GLIBCXX_USE_NOEXCEPT 00079 : _M_real_alloc(__rarg._M_real_alloc) { } 00080 00081 template<typename _Up> 00082 _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg) 00083 _GLIBCXX_USE_NOEXCEPT 00084 : _M_real_alloc(__rarg._M_getUnderlyingImp()) { } 00085 00086 ~_ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT 00087 { } 00088 00089 pointer address(reference __x) const _GLIBCXX_NOEXCEPT 00090 { return std::__addressof(__x); } 00091 00092 const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT 00093 { return std::__addressof(__x); } 00094 00095 pointer allocate(size_type __n, void* __hint = 0) 00096 { return _M_real_alloc.allocate(__n,__hint); } 00097 00098 void deallocate(pointer __p, size_type __n) 00099 { _M_real_alloc.deallocate(__p.get(), __n); } 00100 00101 size_type max_size() const _GLIBCXX_USE_NOEXCEPT 00102 { return __numeric_traits<size_type>::__max / sizeof(_Tp); } 00103 00104 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00105 template<typename _Up, typename... _Args> 00106 void 00107 construct(_Up* __p, _Args&&... __args) 00108 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00109 00110 template<typename... _Args> 00111 void 00112 construct(pointer __p, _Args&&... __args) 00113 { construct(__p.get(), std::forward<_Args>(__args)...); } 00114 00115 template<typename _Up> 00116 void 00117 destroy(_Up* __p) 00118 { __p->~_Up(); } 00119 00120 void destroy(pointer __p) 00121 { destroy(__p.get()); } 00122 00123 #else 00124 00125 void construct(pointer __p, const _Tp& __val) 00126 { ::new(__p.get()) _Tp(__val); } 00127 00128 void destroy(pointer __p) 00129 { __p->~_Tp(); } 00130 #endif 00131 00132 template<typename _Up> 00133 inline bool 00134 operator==(const _ExtPtr_allocator<_Up>& __rarg) 00135 { return _M_real_alloc == __rarg._M_getUnderlyingImp(); } 00136 00137 inline bool 00138 operator==(const _ExtPtr_allocator& __rarg) 00139 { return _M_real_alloc == __rarg._M_real_alloc; } 00140 00141 template<typename _Up> 00142 inline bool 00143 operator!=(const _ExtPtr_allocator<_Up>& __rarg) 00144 { return _M_real_alloc != __rarg._M_getUnderlyingImp(); } 00145 00146 inline bool 00147 operator!=(const _ExtPtr_allocator& __rarg) 00148 { return _M_real_alloc != __rarg._M_real_alloc; } 00149 00150 template<typename _Up> 00151 inline friend void 00152 swap(_ExtPtr_allocator<_Up>&, _ExtPtr_allocator<_Up>&); 00153 00154 // A method specific to this implementation. 00155 const std::allocator<_Tp>& 00156 _M_getUnderlyingImp() const 00157 { return _M_real_alloc; } 00158 00159 private: 00160 std::allocator<_Tp> _M_real_alloc; 00161 }; 00162 00163 // _ExtPtr_allocator<void> specialization. 00164 template<> 00165 class _ExtPtr_allocator<void> 00166 { 00167 public: 00168 typedef std::size_t size_type; 00169 typedef std::ptrdiff_t difference_type; 00170 typedef void value_type; 00171 00172 // Note the non-standard pointer types 00173 typedef _Pointer_adapter<_Relative_pointer_impl<void> > pointer; 00174 typedef _Pointer_adapter<_Relative_pointer_impl<const void> > 00175 const_pointer; 00176 00177 template<typename _Up> 00178 struct rebind 00179 { typedef _ExtPtr_allocator<_Up> other; }; 00180 00181 private: 00182 std::allocator<void> _M_real_alloc; 00183 }; 00184 00185 template<typename _Tp> 00186 inline void 00187 swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg) 00188 { 00189 std::allocator<_Tp> __tmp( __rarg._M_real_alloc ); 00190 __rarg._M_real_alloc = __larg._M_real_alloc; 00191 __larg._M_real_alloc = __tmp; 00192 } 00193 00194 _GLIBCXX_END_NAMESPACE_VERSION 00195 } // namespace 00196 00197 #endif /* _EXTPTR_ALLOCATOR_H */