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