| 1 | // Uses-allocator Construction -*- C++ -*- | 
|---|
| 2 |  | 
|---|
| 3 | // Copyright (C) 2010-2019 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 | #ifndef _USES_ALLOCATOR_H | 
|---|
| 26 | #define _USES_ALLOCATOR_H 1 | 
|---|
| 27 |  | 
|---|
| 28 | #if __cplusplus < 201103L | 
|---|
| 29 | # include <bits/c++0x_warning.h> | 
|---|
| 30 | #else | 
|---|
| 31 |  | 
|---|
| 32 | #include <type_traits> | 
|---|
| 33 | #include <bits/move.h> | 
|---|
| 34 |  | 
|---|
| 35 | namespace std _GLIBCXX_VISIBILITY(default) | 
|---|
| 36 | { | 
|---|
| 37 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | 
|---|
| 38 |  | 
|---|
| 39 | // This is used for std::experimental::erased_type from Library Fundamentals. | 
|---|
| 40 | struct __erased_type { }; | 
|---|
| 41 |  | 
|---|
| 42 | // This also supports the "type-erased allocator" protocol from the | 
|---|
| 43 | // Library Fundamentals TS, where allocator_type is erased_type. | 
|---|
| 44 | // The second condition will always be false for types not using the TS. | 
|---|
| 45 | template<typename _Alloc, typename _Tp> | 
|---|
| 46 | using __is_erased_or_convertible | 
|---|
| 47 | = __or_<is_convertible<_Alloc, _Tp>, is_same<_Tp, __erased_type>>; | 
|---|
| 48 |  | 
|---|
| 49 | /// [allocator.tag] | 
|---|
| 50 | struct allocator_arg_t { explicit allocator_arg_t() = default; }; | 
|---|
| 51 |  | 
|---|
| 52 | _GLIBCXX17_INLINE constexpr allocator_arg_t allocator_arg = | 
|---|
| 53 | allocator_arg_t(); | 
|---|
| 54 |  | 
|---|
| 55 | template<typename _Tp, typename _Alloc, typename = __void_t<>> | 
|---|
| 56 | struct __uses_allocator_helper | 
|---|
| 57 | : false_type { }; | 
|---|
| 58 |  | 
|---|
| 59 | template<typename _Tp, typename _Alloc> | 
|---|
| 60 | struct __uses_allocator_helper<_Tp, _Alloc, | 
|---|
| 61 | __void_t<typename _Tp::allocator_type>> | 
|---|
| 62 | : __is_erased_or_convertible<_Alloc, typename _Tp::allocator_type>::type | 
|---|
| 63 | { }; | 
|---|
| 64 |  | 
|---|
| 65 | /// [allocator.uses.trait] | 
|---|
| 66 | template<typename _Tp, typename _Alloc> | 
|---|
| 67 | struct uses_allocator | 
|---|
| 68 | : __uses_allocator_helper<_Tp, _Alloc>::type | 
|---|
| 69 | { }; | 
|---|
| 70 |  | 
|---|
| 71 | struct __uses_alloc_base { }; | 
|---|
| 72 |  | 
|---|
| 73 | struct __uses_alloc0 : __uses_alloc_base | 
|---|
| 74 | { | 
|---|
| 75 | struct _Sink { void operator=(const void*) { } } _M_a; | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 | template<typename _Alloc> | 
|---|
| 79 | struct __uses_alloc1 : __uses_alloc_base { const _Alloc* _M_a; }; | 
|---|
| 80 |  | 
|---|
| 81 | template<typename _Alloc> | 
|---|
| 82 | struct __uses_alloc2 : __uses_alloc_base { const _Alloc* _M_a; }; | 
|---|
| 83 |  | 
|---|
| 84 | template<bool, typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 85 | struct __uses_alloc; | 
|---|
| 86 |  | 
|---|
| 87 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 88 | struct __uses_alloc<true, _Tp, _Alloc, _Args...> | 
|---|
| 89 | : conditional< | 
|---|
| 90 | is_constructible<_Tp, allocator_arg_t, const _Alloc&, _Args...>::value, | 
|---|
| 91 | __uses_alloc1<_Alloc>, | 
|---|
| 92 | __uses_alloc2<_Alloc>>::type | 
|---|
| 93 | { | 
|---|
| 94 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | 
|---|
| 95 | // 2586. Wrong value category used in scoped_allocator_adaptor::construct | 
|---|
| 96 | static_assert(__or_< | 
|---|
| 97 | is_constructible<_Tp, allocator_arg_t, const _Alloc&, _Args...>, | 
|---|
| 98 | is_constructible<_Tp, _Args..., const _Alloc&>>::value, | 
|---|
| 99 | "construction with an allocator must be possible" | 
|---|
| 100 | " if uses_allocator is true"); | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 104 | struct __uses_alloc<false, _Tp, _Alloc, _Args...> | 
|---|
| 105 | : __uses_alloc0 { }; | 
|---|
| 106 |  | 
|---|
| 107 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 108 | using __uses_alloc_t = | 
|---|
| 109 | __uses_alloc<uses_allocator<_Tp, _Alloc>::value, _Tp, _Alloc, _Args...>; | 
|---|
| 110 |  | 
|---|
| 111 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 112 | inline __uses_alloc_t<_Tp, _Alloc, _Args...> | 
|---|
| 113 | __use_alloc(const _Alloc& __a) | 
|---|
| 114 | { | 
|---|
| 115 | __uses_alloc_t<_Tp, _Alloc, _Args...> __ret; | 
|---|
| 116 | __ret._M_a = std::__addressof(__a); | 
|---|
| 117 | return __ret; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 121 | void | 
|---|
| 122 | __use_alloc(const _Alloc&&) = delete; | 
|---|
| 123 |  | 
|---|
| 124 | #if __cplusplus > 201402L | 
|---|
| 125 | template <typename _Tp, typename _Alloc> | 
|---|
| 126 | inline constexpr bool uses_allocator_v = | 
|---|
| 127 | uses_allocator<_Tp, _Alloc>::value; | 
|---|
| 128 | #endif // C++17 | 
|---|
| 129 |  | 
|---|
| 130 | template<template<typename...> class _Predicate, | 
|---|
| 131 | typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 132 | struct __is_uses_allocator_predicate | 
|---|
| 133 | : conditional<uses_allocator<_Tp, _Alloc>::value, | 
|---|
| 134 | __or_<_Predicate<_Tp, allocator_arg_t, _Alloc, _Args...>, | 
|---|
| 135 | _Predicate<_Tp, _Args..., _Alloc>>, | 
|---|
| 136 | _Predicate<_Tp, _Args...>>::type { }; | 
|---|
| 137 |  | 
|---|
| 138 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 139 | struct __is_uses_allocator_constructible | 
|---|
| 140 | : __is_uses_allocator_predicate<is_constructible, _Tp, _Alloc, _Args...> | 
|---|
| 141 | { }; | 
|---|
| 142 |  | 
|---|
| 143 | #if __cplusplus >= 201402L | 
|---|
| 144 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 145 | _GLIBCXX17_INLINE constexpr bool __is_uses_allocator_constructible_v = | 
|---|
| 146 | __is_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value; | 
|---|
| 147 | #endif // C++14 | 
|---|
| 148 |  | 
|---|
| 149 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 150 | struct __is_nothrow_uses_allocator_constructible | 
|---|
| 151 | : __is_uses_allocator_predicate<is_nothrow_constructible, | 
|---|
| 152 | _Tp, _Alloc, _Args...> | 
|---|
| 153 | { }; | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 | #if __cplusplus >= 201402L | 
|---|
| 157 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 158 | _GLIBCXX17_INLINE constexpr bool | 
|---|
| 159 | __is_nothrow_uses_allocator_constructible_v = | 
|---|
| 160 | __is_nothrow_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value; | 
|---|
| 161 | #endif // C++14 | 
|---|
| 162 |  | 
|---|
| 163 | template<typename _Tp, typename... _Args> | 
|---|
| 164 | void __uses_allocator_construct_impl(__uses_alloc0 __a, _Tp* __ptr, | 
|---|
| 165 | _Args&&... __args) | 
|---|
| 166 | { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)...); } | 
|---|
| 167 |  | 
|---|
| 168 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 169 | void __uses_allocator_construct_impl(__uses_alloc1<_Alloc> __a, _Tp* __ptr, | 
|---|
| 170 | _Args&&... __args) | 
|---|
| 171 | { | 
|---|
| 172 | ::new ((void*)__ptr) _Tp(allocator_arg, *__a._M_a, | 
|---|
| 173 | std::forward<_Args>(__args)...); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 177 | void __uses_allocator_construct_impl(__uses_alloc2<_Alloc> __a, _Tp* __ptr, | 
|---|
| 178 | _Args&&... __args) | 
|---|
| 179 | { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)..., *__a._M_a); } | 
|---|
| 180 |  | 
|---|
| 181 | template<typename _Tp, typename _Alloc, typename... _Args> | 
|---|
| 182 | void __uses_allocator_construct(const _Alloc& __a, _Tp* __ptr, | 
|---|
| 183 | _Args&&... __args) | 
|---|
| 184 | { | 
|---|
| 185 | std::__uses_allocator_construct_impl( | 
|---|
| 186 | std::__use_alloc<_Tp, _Alloc, _Args...>(__a), __ptr, | 
|---|
| 187 | std::forward<_Args>(__args)...); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | _GLIBCXX_END_NAMESPACE_VERSION | 
|---|
| 191 | } // namespace std | 
|---|
| 192 |  | 
|---|
| 193 | #endif | 
|---|
| 194 | #endif | 
|---|
| 195 |  | 
|---|