Warning: This file is not a C or C++ file. It does not have highlighting.
| 1 | // -*- C++ -*- |
|---|---|
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___CXX03___MEMORY_ALLOCATOR_TRAITS_H |
| 11 | #define _LIBCPP___CXX03___MEMORY_ALLOCATOR_TRAITS_H |
| 12 | |
| 13 | #include <__cxx03/__config> |
| 14 | #include <__cxx03/__memory/construct_at.h> |
| 15 | #include <__cxx03/__memory/pointer_traits.h> |
| 16 | #include <__cxx03/__type_traits/enable_if.h> |
| 17 | #include <__cxx03/__type_traits/is_constructible.h> |
| 18 | #include <__cxx03/__type_traits/is_empty.h> |
| 19 | #include <__cxx03/__type_traits/is_same.h> |
| 20 | #include <__cxx03/__type_traits/make_unsigned.h> |
| 21 | #include <__cxx03/__type_traits/remove_reference.h> |
| 22 | #include <__cxx03/__type_traits/void_t.h> |
| 23 | #include <__cxx03/__utility/declval.h> |
| 24 | #include <__cxx03/__utility/forward.h> |
| 25 | #include <__cxx03/cstddef> |
| 26 | #include <__cxx03/limits> |
| 27 | |
| 28 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 29 | # pragma GCC system_header |
| 30 | #endif |
| 31 | |
| 32 | _LIBCPP_PUSH_MACROS |
| 33 | #include <__cxx03/__undef_macros> |
| 34 | |
| 35 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 36 | |
| 37 | #define _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(NAME, PROPERTY) \ |
| 38 | template <class _Tp, class = void> \ |
| 39 | struct NAME : false_type {}; \ |
| 40 | template <class _Tp> \ |
| 41 | struct NAME<_Tp, __void_t<typename _Tp::PROPERTY > > : true_type {} |
| 42 | |
| 43 | // __pointer |
| 44 | template <class _Tp, |
| 45 | class _Alloc, |
| 46 | class _RawAlloc = __libcpp_remove_reference_t<_Alloc>, |
| 47 | bool = __has_pointer<_RawAlloc>::value> |
| 48 | struct __pointer { |
| 49 | using type _LIBCPP_NODEBUG = typename _RawAlloc::pointer; |
| 50 | }; |
| 51 | template <class _Tp, class _Alloc, class _RawAlloc> |
| 52 | struct __pointer<_Tp, _Alloc, _RawAlloc, false> { |
| 53 | using type _LIBCPP_NODEBUG = _Tp*; |
| 54 | }; |
| 55 | |
| 56 | // __const_pointer |
| 57 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_pointer, const_pointer); |
| 58 | template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value> |
| 59 | struct __const_pointer { |
| 60 | using type _LIBCPP_NODEBUG = typename _Alloc::const_pointer; |
| 61 | }; |
| 62 | template <class _Tp, class _Ptr, class _Alloc> |
| 63 | struct __const_pointer<_Tp, _Ptr, _Alloc, false> { |
| 64 | using type = typename pointer_traits<_Ptr>::template rebind<const _Tp>::other; |
| 65 | }; |
| 66 | |
| 67 | // __void_pointer |
| 68 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_void_pointer, void_pointer); |
| 69 | template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value> |
| 70 | struct __void_pointer { |
| 71 | using type _LIBCPP_NODEBUG = typename _Alloc::void_pointer; |
| 72 | }; |
| 73 | template <class _Ptr, class _Alloc> |
| 74 | struct __void_pointer<_Ptr, _Alloc, false> { |
| 75 | using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<void>::other; |
| 76 | }; |
| 77 | |
| 78 | // __const_void_pointer |
| 79 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_void_pointer, const_void_pointer); |
| 80 | template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value> |
| 81 | struct __const_void_pointer { |
| 82 | using type _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer; |
| 83 | }; |
| 84 | template <class _Ptr, class _Alloc> |
| 85 | struct __const_void_pointer<_Ptr, _Alloc, false> { |
| 86 | using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const void>::other; |
| 87 | }; |
| 88 | |
| 89 | // __size_type |
| 90 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_size_type, size_type); |
| 91 | template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value> |
| 92 | struct __size_type : make_unsigned<_DiffType> {}; |
| 93 | template <class _Alloc, class _DiffType> |
| 94 | struct __size_type<_Alloc, _DiffType, true> { |
| 95 | using type _LIBCPP_NODEBUG = typename _Alloc::size_type; |
| 96 | }; |
| 97 | |
| 98 | // __alloc_traits_difference_type |
| 99 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_alloc_traits_difference_type, difference_type); |
| 100 | template <class _Alloc, class _Ptr, bool = __has_alloc_traits_difference_type<_Alloc>::value> |
| 101 | struct __alloc_traits_difference_type { |
| 102 | using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::difference_type; |
| 103 | }; |
| 104 | template <class _Alloc, class _Ptr> |
| 105 | struct __alloc_traits_difference_type<_Alloc, _Ptr, true> { |
| 106 | using type _LIBCPP_NODEBUG = typename _Alloc::difference_type; |
| 107 | }; |
| 108 | |
| 109 | // __propagate_on_container_copy_assignment |
| 110 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_copy_assignment, propagate_on_container_copy_assignment); |
| 111 | template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value> |
| 112 | struct __propagate_on_container_copy_assignment : false_type {}; |
| 113 | template <class _Alloc> |
| 114 | struct __propagate_on_container_copy_assignment<_Alloc, true> { |
| 115 | using type _LIBCPP_NODEBUG = typename _Alloc::propagate_on_container_copy_assignment; |
| 116 | }; |
| 117 | |
| 118 | // __propagate_on_container_move_assignment |
| 119 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_move_assignment, propagate_on_container_move_assignment); |
| 120 | template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value> |
| 121 | struct __propagate_on_container_move_assignment : false_type {}; |
| 122 | template <class _Alloc> |
| 123 | struct __propagate_on_container_move_assignment<_Alloc, true> { |
| 124 | using type _LIBCPP_NODEBUG = typename _Alloc::propagate_on_container_move_assignment; |
| 125 | }; |
| 126 | |
| 127 | // __propagate_on_container_swap |
| 128 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_swap, propagate_on_container_swap); |
| 129 | template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value> |
| 130 | struct __propagate_on_container_swap : false_type {}; |
| 131 | template <class _Alloc> |
| 132 | struct __propagate_on_container_swap<_Alloc, true> { |
| 133 | using type _LIBCPP_NODEBUG = typename _Alloc::propagate_on_container_swap; |
| 134 | }; |
| 135 | |
| 136 | // __is_always_equal |
| 137 | _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_is_always_equal, is_always_equal); |
| 138 | template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value> |
| 139 | struct __is_always_equal : is_empty<_Alloc> {}; |
| 140 | template <class _Alloc> |
| 141 | struct __is_always_equal<_Alloc, true> { |
| 142 | using type _LIBCPP_NODEBUG = typename _Alloc::is_always_equal; |
| 143 | }; |
| 144 | |
| 145 | // __allocator_traits_rebind |
| 146 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 147 | template <class _Tp, class _Up, class = void> |
| 148 | struct __has_rebind_other : false_type {}; |
| 149 | template <class _Tp, class _Up> |
| 150 | struct __has_rebind_other<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other> > : true_type {}; |
| 151 | |
| 152 | template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value> |
| 153 | struct __allocator_traits_rebind { |
| 154 | static_assert(__has_rebind_other<_Tp, _Up>::value, "This allocator has to implement rebind"); |
| 155 | using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other; |
| 156 | }; |
| 157 | template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up> |
| 158 | struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true> { |
| 159 | using type _LIBCPP_NODEBUG = typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other; |
| 160 | }; |
| 161 | template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up> |
| 162 | struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false> { |
| 163 | using type _LIBCPP_NODEBUG = _Alloc<_Up, _Args...>; |
| 164 | }; |
| 165 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
| 166 | |
| 167 | template <class _Alloc, class _Tp> |
| 168 | using __allocator_traits_rebind_t = typename __allocator_traits_rebind<_Alloc, _Tp>::type; |
| 169 | |
| 170 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 171 | |
| 172 | // __has_allocate_hint |
| 173 | template <class _Alloc, class _SizeType, class _ConstVoidPtr, class = void> |
| 174 | struct __has_allocate_hint : false_type {}; |
| 175 | |
| 176 | template <class _Alloc, class _SizeType, class _ConstVoidPtr> |
| 177 | struct __has_allocate_hint< |
| 178 | _Alloc, |
| 179 | _SizeType, |
| 180 | _ConstVoidPtr, |
| 181 | decltype((void)std::declval<_Alloc>().allocate(std::declval<_SizeType>(), std::declval<_ConstVoidPtr>()))> |
| 182 | : true_type {}; |
| 183 | |
| 184 | // __has_construct |
| 185 | template <class, class _Alloc, class... _Args> |
| 186 | struct __has_construct_impl : false_type {}; |
| 187 | |
| 188 | template <class _Alloc, class... _Args> |
| 189 | struct __has_construct_impl<decltype((void)std::declval<_Alloc>().construct(std::declval<_Args>()...)), |
| 190 | _Alloc, |
| 191 | _Args...> : true_type {}; |
| 192 | |
| 193 | template <class _Alloc, class... _Args> |
| 194 | struct __has_construct : __has_construct_impl<void, _Alloc, _Args...> {}; |
| 195 | |
| 196 | // __has_destroy |
| 197 | template <class _Alloc, class _Pointer, class = void> |
| 198 | struct __has_destroy : false_type {}; |
| 199 | |
| 200 | template <class _Alloc, class _Pointer> |
| 201 | struct __has_destroy<_Alloc, _Pointer, decltype((void)std::declval<_Alloc>().destroy(std::declval<_Pointer>()))> |
| 202 | : true_type {}; |
| 203 | |
| 204 | // __has_max_size |
| 205 | template <class _Alloc, class = void> |
| 206 | struct __has_max_size : false_type {}; |
| 207 | |
| 208 | template <class _Alloc> |
| 209 | struct __has_max_size<_Alloc, decltype((void)std::declval<_Alloc&>().max_size())> : true_type {}; |
| 210 | |
| 211 | // __has_select_on_container_copy_construction |
| 212 | template <class _Alloc, class = void> |
| 213 | struct __has_select_on_container_copy_construction : false_type {}; |
| 214 | |
| 215 | template <class _Alloc> |
| 216 | struct __has_select_on_container_copy_construction< |
| 217 | _Alloc, |
| 218 | decltype((void)std::declval<_Alloc>().select_on_container_copy_construction())> : true_type {}; |
| 219 | |
| 220 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
| 221 | |
| 222 | template <class _Alloc> |
| 223 | struct _LIBCPP_TEMPLATE_VIS allocator_traits { |
| 224 | using allocator_type = _Alloc; |
| 225 | using value_type = typename allocator_type::value_type; |
| 226 | using pointer = typename __pointer<value_type, allocator_type>::type; |
| 227 | using const_pointer = typename __const_pointer<value_type, pointer, allocator_type>::type; |
| 228 | using void_pointer = typename __void_pointer<pointer, allocator_type>::type; |
| 229 | using const_void_pointer = typename __const_void_pointer<pointer, allocator_type>::type; |
| 230 | using difference_type = typename __alloc_traits_difference_type<allocator_type, pointer>::type; |
| 231 | using size_type = typename __size_type<allocator_type, difference_type>::type; |
| 232 | using propagate_on_container_copy_assignment = |
| 233 | typename __propagate_on_container_copy_assignment<allocator_type>::type; |
| 234 | using propagate_on_container_move_assignment = |
| 235 | typename __propagate_on_container_move_assignment<allocator_type>::type; |
| 236 | using propagate_on_container_swap = typename __propagate_on_container_swap<allocator_type>::type; |
| 237 | using is_always_equal = typename __is_always_equal<allocator_type>::type; |
| 238 | |
| 239 | template <class _Tp> |
| 240 | struct rebind_alloc { |
| 241 | using other = __allocator_traits_rebind_t<allocator_type, _Tp>; |
| 242 | }; |
| 243 | template <class _Tp> |
| 244 | struct rebind_traits { |
| 245 | using other = allocator_traits<typename rebind_alloc<_Tp>::other>; |
| 246 | }; |
| 247 | |
| 248 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI static pointer allocate(allocator_type& __a, size_type __n) { |
| 249 | return __a.allocate(__n); |
| 250 | } |
| 251 | |
| 252 | template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0> |
| 253 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI static pointer |
| 254 | allocate(allocator_type& __a, size_type __n, const_void_pointer __hint) { |
| 255 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 256 | return __a.allocate(__n, __hint); |
| 257 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
| 258 | } |
| 259 | template <class _Ap = _Alloc, |
| 260 | class = void, |
| 261 | __enable_if_t<!__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0> |
| 262 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI static pointer |
| 263 | allocate(allocator_type& __a, size_type __n, const_void_pointer) { |
| 264 | return __a.allocate(__n); |
| 265 | } |
| 266 | |
| 267 | _LIBCPP_HIDE_FROM_ABI static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT { |
| 268 | __a.deallocate(__p, __n); |
| 269 | } |
| 270 | |
| 271 | template <class _Tp, class... _Args, __enable_if_t<__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0> |
| 272 | _LIBCPP_HIDE_FROM_ABI static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args) { |
| 273 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 274 | __a.construct(__p, std::forward<_Args>(__args)...); |
| 275 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
| 276 | } |
| 277 | template <class _Tp, |
| 278 | class... _Args, |
| 279 | class = void, |
| 280 | __enable_if_t<!__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0> |
| 281 | _LIBCPP_HIDE_FROM_ABI static void construct(allocator_type&, _Tp* __p, _Args&&... __args) { |
| 282 | std::__construct_at(__p, std::forward<_Args>(__args)...); |
| 283 | } |
| 284 | |
| 285 | template <class _Tp, __enable_if_t<__has_destroy<allocator_type, _Tp*>::value, int> = 0> |
| 286 | _LIBCPP_HIDE_FROM_ABI static void destroy(allocator_type& __a, _Tp* __p) { |
| 287 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 288 | __a.destroy(__p); |
| 289 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
| 290 | } |
| 291 | template <class _Tp, class = void, __enable_if_t<!__has_destroy<allocator_type, _Tp*>::value, int> = 0> |
| 292 | _LIBCPP_HIDE_FROM_ABI static void destroy(allocator_type&, _Tp* __p) { |
| 293 | std::__destroy_at(__p); |
| 294 | } |
| 295 | |
| 296 | template <class _Ap = _Alloc, __enable_if_t<__has_max_size<const _Ap>::value, int> = 0> |
| 297 | _LIBCPP_HIDE_FROM_ABI static size_type max_size(const allocator_type& __a) _NOEXCEPT { |
| 298 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 299 | return __a.max_size(); |
| 300 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
| 301 | } |
| 302 | template <class _Ap = _Alloc, class = void, __enable_if_t<!__has_max_size<const _Ap>::value, int> = 0> |
| 303 | _LIBCPP_HIDE_FROM_ABI static size_type max_size(const allocator_type&) _NOEXCEPT { |
| 304 | return numeric_limits<size_type>::max() / sizeof(value_type); |
| 305 | } |
| 306 | |
| 307 | template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction<const _Ap>::value, int> = 0> |
| 308 | _LIBCPP_HIDE_FROM_ABI static allocator_type select_on_container_copy_construction(const allocator_type& __a) { |
| 309 | return __a.select_on_container_copy_construction(); |
| 310 | } |
| 311 | template <class _Ap = _Alloc, |
| 312 | class = void, |
| 313 | __enable_if_t<!__has_select_on_container_copy_construction<const _Ap>::value, int> = 0> |
| 314 | _LIBCPP_HIDE_FROM_ABI static allocator_type select_on_container_copy_construction(const allocator_type& __a) { |
| 315 | return __a; |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | template <class _Traits, class _Tp> |
| 320 | using __rebind_alloc = typename _Traits::template rebind_alloc<_Tp>::other; |
| 321 | |
| 322 | template <class _Alloc> |
| 323 | struct __check_valid_allocator : true_type { |
| 324 | using _Traits = std::allocator_traits<_Alloc>; |
| 325 | static_assert(is_same<_Alloc, __rebind_alloc<_Traits, typename _Traits::value_type> >::value, |
| 326 | "[allocator.requirements] states that rebinding an allocator to the same type should result in the " |
| 327 | "original allocator"); |
| 328 | }; |
| 329 | |
| 330 | // __is_default_allocator |
| 331 | template <class _Tp> |
| 332 | struct __is_default_allocator : false_type {}; |
| 333 | |
| 334 | template <class> |
| 335 | class allocator; |
| 336 | |
| 337 | template <class _Tp> |
| 338 | struct __is_default_allocator<allocator<_Tp> > : true_type {}; |
| 339 | |
| 340 | // __is_cpp17_move_insertable |
| 341 | template <class _Alloc, class = void> |
| 342 | struct __is_cpp17_move_insertable : is_move_constructible<typename _Alloc::value_type> {}; |
| 343 | |
| 344 | template <class _Alloc> |
| 345 | struct __is_cpp17_move_insertable< |
| 346 | _Alloc, |
| 347 | __enable_if_t< !__is_default_allocator<_Alloc>::value && |
| 348 | __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value > > |
| 349 | : true_type {}; |
| 350 | |
| 351 | // __is_cpp17_copy_insertable |
| 352 | template <class _Alloc, class = void> |
| 353 | struct __is_cpp17_copy_insertable |
| 354 | : integral_constant<bool, |
| 355 | is_copy_constructible<typename _Alloc::value_type>::value && |
| 356 | __is_cpp17_move_insertable<_Alloc>::value > {}; |
| 357 | |
| 358 | template <class _Alloc> |
| 359 | struct __is_cpp17_copy_insertable< |
| 360 | _Alloc, |
| 361 | __enable_if_t< !__is_default_allocator<_Alloc>::value && |
| 362 | __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value > > |
| 363 | : __is_cpp17_move_insertable<_Alloc> {}; |
| 364 | |
| 365 | #undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX |
| 366 | |
| 367 | _LIBCPP_END_NAMESPACE_STD |
| 368 | |
| 369 | _LIBCPP_POP_MACROS |
| 370 | |
| 371 | #endif // _LIBCPP___CXX03___MEMORY_ALLOCATOR_TRAITS_H |
| 372 |
Warning: This file is not a C or C++ file. It does not have highlighting.
