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