1 // Allocator traits -*- C++ -*- 2 3 // Copyright (C) 2011-2014 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file ext/alloc_traits.h 26 * This file is a GNU extension to the Standard C++ Library. 27 */ 28 29 #ifndef _EXT_ALLOC_TRAITS_H 30 #define _EXT_ALLOC_TRAITS_H 1 31 32 #pragma GCC system_header 33 34 #if __cplusplus >= 201103L 35 # include <bits/move.h> 36 # include <bits/alloc_traits.h> 37 #else 38 # include <bits/allocator.h> // for __alloc_swap 39 #endif 40 41 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 42 { 43 _GLIBCXX_BEGIN_NAMESPACE_VERSION 44 45 #if __cplusplus >= 201103L 46 template<typename _Alloc> 47 struct __allocator_always_compares_equal : std::false_type { }; 48 49 template<typename _Tp> 50 struct __allocator_always_compares_equal<std::allocator<_Tp>> 51 : std::true_type { }; 52 53 template<typename, typename> struct array_allocator; 54 55 template<typename _Tp, typename _Array> 56 struct __allocator_always_compares_equal<array_allocator<_Tp, _Array>> 57 : std::true_type { }; 58 59 template<typename> struct bitmap_allocator; 60 61 template<typename _Tp> 62 struct __allocator_always_compares_equal<bitmap_allocator<_Tp>> 63 : std::true_type { }; 64 65 template<typename> struct malloc_allocator; 66 67 template<typename _Tp> 68 struct __allocator_always_compares_equal<malloc_allocator<_Tp>> 69 : std::true_type { }; 70 71 template<typename> struct mt_allocator; 72 73 template<typename _Tp> 74 struct __allocator_always_compares_equal<mt_allocator<_Tp>> 75 : std::true_type { }; 76 77 template<typename> struct new_allocator; 78 79 template<typename _Tp> 80 struct __allocator_always_compares_equal<new_allocator<_Tp>> 81 : std::true_type { }; 82 83 template<typename> struct pool_allocator; 84 85 template<typename _Tp> 86 struct __allocator_always_compares_equal<pool_allocator<_Tp>> 87 : std::true_type { }; 88 #endif 89 90 /** 91 * @brief Uniform interface to C++98 and C++0x allocators. 92 * @ingroup allocators 93 */ 94 template<typename _Alloc> 95 struct __alloc_traits 96 #if __cplusplus >= 201103L 97 : std::allocator_traits<_Alloc> 98 #endif 99 { 100 typedef _Alloc allocator_type; 101 #if __cplusplus >= 201103L 102 typedef std::allocator_traits<_Alloc> _Base_type; 103 typedef typename _Base_type::value_type value_type; 104 typedef typename _Base_type::pointer pointer; 105 typedef typename _Base_type::const_pointer const_pointer; 106 typedef typename _Base_type::size_type size_type; 107 typedef typename _Base_type::difference_type difference_type; 108 // C++11 allocators do not define reference or const_reference 109 typedef value_type& reference; 110 typedef const value_type& const_reference; 111 using _Base_type::allocate; 112 using _Base_type::deallocate; 113 using _Base_type::construct; 114 using _Base_type::destroy; 115 using _Base_type::max_size; 116 117 private: 118 template<typename _Ptr> 119 using __is_custom_pointer 120 = std::__and_<std::is_same<pointer, _Ptr>, 121 std::__not_<std::is_pointer<_Ptr>>>; 122 123 public: 124 // overload construct for non-standard pointer types 125 template<typename _Ptr, typename... _Args> 126 static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type 127 construct(_Alloc& __a, _Ptr __p, _Args&&... __args) 128 { 129 _Base_type::construct(__a, std::addressof(*__p), 130 std::forward<_Args>(__args)...); 131 } 132 133 // overload destroy for non-standard pointer types 134 template<typename _Ptr> 135 static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type 136 destroy(_Alloc& __a, _Ptr __p) 137 { _Base_type::destroy(__a, std::addressof(*__p)); } 138 139 static _Alloc _S_select_on_copy(const _Alloc& __a) 140 { return _Base_type::select_on_container_copy_construction(__a); } 141 142 static void _S_on_swap(_Alloc& __a, _Alloc& __b) 143 { std::__alloc_on_swap(__a, __b); } 144 145 static constexpr bool _S_propagate_on_copy_assign() 146 { return _Base_type::propagate_on_container_copy_assignment::value; } 147 148 static constexpr bool _S_propagate_on_move_assign() 149 { return _Base_type::propagate_on_container_move_assignment::value; } 150 151 static constexpr bool _S_propagate_on_swap() 152 { return _Base_type::propagate_on_container_swap::value; } 153 154 static constexpr bool _S_always_equal() 155 { return __allocator_always_compares_equal<_Alloc>::value; } 156 157 static constexpr bool _S_nothrow_move() 158 { return _S_propagate_on_move_assign() || _S_always_equal(); } 159 160 static constexpr bool _S_nothrow_swap() 161 { 162 using std::swap; 163 return !_S_propagate_on_swap() 164 || noexcept(swap(std::declval<_Alloc&>(), std::declval<_Alloc&>())); 165 } 166 167 template<typename _Tp> 168 struct rebind 169 { typedef typename _Base_type::template rebind_alloc<_Tp> other; }; 170 #else 171 172 typedef typename _Alloc::pointer pointer; 173 typedef typename _Alloc::const_pointer const_pointer; 174 typedef typename _Alloc::value_type value_type; 175 typedef typename _Alloc::reference reference; 176 typedef typename _Alloc::const_reference const_reference; 177 typedef typename _Alloc::size_type size_type; 178 typedef typename _Alloc::difference_type difference_type; 179 180 static pointer 181 allocate(_Alloc& __a, size_type __n) 182 { return __a.allocate(__n); } 183 184 static void deallocate(_Alloc& __a, pointer __p, size_type __n) 185 { __a.deallocate(__p, __n); } 186 187 template<typename _Tp> 188 static void construct(_Alloc& __a, pointer __p, const _Tp& __arg) 189 { __a.construct(__p, __arg); } 190 191 static void destroy(_Alloc& __a, pointer __p) 192 { __a.destroy(__p); } 193 194 static size_type max_size(const _Alloc& __a) 195 { return __a.max_size(); } 196 197 static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; } 198 199 static void _S_on_swap(_Alloc& __a, _Alloc& __b) 200 { 201 // _GLIBCXX_RESOLVE_LIB_DEFECTS 202 // 431. Swapping containers with unequal allocators. 203 std::__alloc_swap<_Alloc>::_S_do_it(__a, __b); 204 } 205 206 template<typename _Tp> 207 struct rebind 208 { typedef typename _Alloc::template rebind<_Tp>::other other; }; 209 #endif 210 }; 211 212 _GLIBCXX_END_NAMESPACE_VERSION 213 } // namespace std 214 215 #endif 216